Skip to content

Commit e35588b

Browse files
committed
docs: Fix basic form example
1 parent 89e0925 commit e35588b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

docs/examples/basic_form.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ The following example is a basic recruitment form for a pet store. Note that the
1111

1212
```jsx live noInline title=MyChatBot.js
1313
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+
1521
const formStyle = {
22+
color: "black",
1623
marginTop: 10,
1724
marginLeft: 20,
1825
border: "1px solid #491d8d",
@@ -24,12 +31,12 @@ const MyChatBot = () => {
2431
const flow={
2532
start: {
2633
message: "Hello there! What is your name?",
27-
function: (params) => setForm({...form, name: params.userInput}),
34+
function: (params) => updateForm({ name: params.userInput }),
2835
path: "ask_age"
2936
},
3037
ask_age: {
3138
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 }),
3340
path: "ask_pet"
3441
},
3542
ask_pet: {
@@ -39,7 +46,7 @@ const MyChatBot = () => {
3946
// options: {items: ["Yes", "No"], sendOutput: false}
4047
options: ["Yes", "No"],
4148
chatDisabled: true,
42-
function: (params) => setForm({...form, pet_ownership: params.userInput}),
49+
function: (params) => updateForm({ pet_ownership: params.userInput }),
4350
path: "ask_choice"
4451
},
4552
ask_choice: {
@@ -49,25 +56,28 @@ const MyChatBot = () => {
4956
// checkboxes: ["Dog", "Cat", "Rabbit", "Hamster"]
5057
checkboxes: {items: ["Dog", "Cat", "Rabbit", "Hamster"], min: 2},
5158
chatDisabled: true,
52-
function: (params) => setForm({...form, pet_choices: params.userInput}),
59+
function: (params) => updateForm({ pet_choices: params.userInput }),
5360
path: "ask_work_days"
5461
},
5562
ask_work_days: {
5663
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 }),
5865
path: "end"
5966
},
6067
end: {
6168
message: "Thank you for your interest, we will get back to you shortly!",
62-
component: (
69+
component: () => {
70+
const form = formRef.current;
71+
return (
6372
<div style={formStyle}>
6473
<p>Name: {form.name}</p>
6574
<p>Age: {form.age}</p>
6675
<p>Pet Ownership: {form.pet_ownership}</p>
6776
<p>Pet Choices: {form.pet_choices}</p>
6877
<p>Num Work Days: {form.num_work_days}</p>
6978
</div>
70-
),
79+
);
80+
},
7181
options: ["New Application"],
7282
chatDisabled: true,
7383
path: "start"

0 commit comments

Comments
 (0)