@@ -11,8 +11,15 @@ The following example is a basic recruitment form for a pet store. Note that the
11
11
12
12
``` jsx live noInline title=MyChatBot.js
13
13
const MyChatBot = () => {
14
- const [form , setForm ] = React .useState ({});
14
+ const formRef = React .useRef ({});
15
+
16
+ // simple helper to update form
17
+ const updateForm = (patch ) => {
18
+ Object .assign (formRef .current , patch);
19
+ };
20
+
15
21
const formStyle = {
22
+ color: " black" ,
16
23
marginTop: 10 ,
17
24
marginLeft: 20 ,
18
25
border: " 1px solid #491d8d" ,
@@ -24,12 +31,12 @@ const MyChatBot = () => {
24
31
const flow = {
25
32
start: {
26
33
message: " Hello there! What is your name?" ,
27
- function : (params ) => setForm ({ ... form, name: params .userInput }),
34
+ function : (params ) => updateForm ({ name: params .userInput }),
28
35
path: " ask_age"
29
36
},
30
37
ask_age: {
31
38
message : (params ) => ` Nice to meet you ${ params .userInput } , what is your age?` ,
32
- function : (params ) => setForm ({ ... form, age: params .userInput }),
39
+ function : (params ) => updateForm ({ age: params .userInput }),
33
40
path: " ask_pet"
34
41
},
35
42
ask_pet: {
@@ -39,7 +46,7 @@ const MyChatBot = () => {
39
46
// options: {items: ["Yes", "No"], sendOutput: false}
40
47
options: [" Yes" , " No" ],
41
48
chatDisabled: true ,
42
- function : (params ) => setForm ({ ... form, pet_ownership: params .userInput }),
49
+ function : (params ) => updateForm ({ pet_ownership: params .userInput }),
43
50
path: " ask_choice"
44
51
},
45
52
ask_choice: {
@@ -49,25 +56,28 @@ const MyChatBot = () => {
49
56
// checkboxes: ["Dog", "Cat", "Rabbit", "Hamster"]
50
57
checkboxes: {items: [" Dog" , " Cat" , " Rabbit" , " Hamster" ], min: 2 },
51
58
chatDisabled: true ,
52
- function : (params ) => setForm ({ ... form, pet_choices: params .userInput }),
59
+ function : (params ) => updateForm ({ pet_choices: params .userInput }),
53
60
path: " ask_work_days"
54
61
},
55
62
ask_work_days: {
56
63
message: " How many days can you work per week?" ,
57
- function : (params ) => setForm ({ ... form, num_work_days: params .userInput }),
64
+ function : (params ) => updateForm ({ num_work_days: params .userInput }),
58
65
path: " end"
59
66
},
60
67
end: {
61
68
message: " Thank you for your interest, we will get back to you shortly!" ,
62
- component: (
69
+ component : () => {
70
+ const form = formRef .current ;
71
+ return (
63
72
< div style= {formStyle}>
64
73
< p> Name: {form .name }< / p>
65
74
< p> Age: {form .age }< / p>
66
75
< p> Pet Ownership: {form .pet_ownership }< / p>
67
76
< p> Pet Choices: {form .pet_choices }< / p>
68
77
< p> Num Work Days: {form .num_work_days }< / p>
69
78
< / div>
70
- ),
79
+ );
80
+ },
71
81
options: [" New Application" ],
72
82
chatDisabled: true ,
73
83
path: " start"
0 commit comments