@@ -11,8 +11,15 @@ The following example is a slightly more complex recruitment form for a pet stor
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: async (params ) => {
34
41
if (isNaN (Number (params .userInput ))) {
35
42
await params .injectMessage (" Age needs to be a number!" );
@@ -45,7 +52,7 @@ const MyChatBot = () => {
45
52
// options: {items: ["Yes", "No"], sendOutput: false}
46
53
options: [" Yes" , " No" ],
47
54
chatDisabled: true ,
48
- function : (params ) => setForm ({ ... form, pet_ownership: params .userInput }),
55
+ function : (params ) => updateForm ({ pet_ownership: params .userInput }),
49
56
path: " ask_choice"
50
57
},
51
58
ask_choice: {
@@ -55,12 +62,12 @@ const MyChatBot = () => {
55
62
// checkboxes: ["Dog", "Cat", "Rabbit", "Hamster", "Bird"]
56
63
checkboxes: {items: [" Dog" , " Cat" , " Rabbit" , " Hamster" , " Bird" ], min: 2 , max: 4 },
57
64
chatDisabled: true ,
58
- function : (params ) => setForm ({ ... form, pet_choices: params .userInput }),
65
+ function : (params ) => updateForm ({ pet_choices: params .userInput }),
59
66
path: " ask_work_days"
60
67
},
61
68
ask_work_days: {
62
69
message: " How many days can you work per week?" ,
63
- function : (params ) => setForm ({ ... form, num_work_days: params .userInput }),
70
+ function : (params ) => updateForm ({ num_work_days: params .userInput }),
64
71
path: async (params ) => {
65
72
if (isNaN (Number (params .userInput ))) {
66
73
await params .injectMessage (" Number of work day(s) need to be a number!" );
@@ -71,15 +78,18 @@ const MyChatBot = () => {
71
78
},
72
79
end: {
73
80
message: " Thank you for your interest, we will get back to you shortly!" ,
74
- component: (
75
- < div style= {formStyle}>
76
- < p> Name: {form .name }< / p>
77
- < p> Age: {form .age }< / p>
78
- < p> Pet Ownership: {form .pet_ownership }< / p>
79
- < p> Pet Choices: {form .pet_choices }< / p>
80
- < p> Num Work Days: {form .num_work_days }< / p>
81
- < / div>
82
- ),
81
+ component : () => {
82
+ const form = formRef .current ;
83
+ return (
84
+ < div style= {formStyle}>
85
+ < p> Name: {form .name }< / p>
86
+ < p> Age: {form .age }< / p>
87
+ < p> Pet Ownership: {form .pet_ownership }< / p>
88
+ < p> Pet Choices: {form .pet_choices }< / p>
89
+ < p> Num Work Days: {form .num_work_days }< / p>
90
+ < / div>
91
+ );
92
+ },
83
93
options: [" New Application" ],
84
94
chatDisabled: true ,
85
95
path: " start"
0 commit comments