Skip to content

Commit d3efb43

Browse files
committed
docs: Fix complex form example
1 parent e35588b commit d3efb43

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

docs/examples/basic_form.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ const MyChatBot = () => {
6969
component: () => {
7070
const form = formRef.current;
7171
return (
72-
<div style={formStyle}>
73-
<p>Name: {form.name}</p>
74-
<p>Age: {form.age}</p>
75-
<p>Pet Ownership: {form.pet_ownership}</p>
76-
<p>Pet Choices: {form.pet_choices}</p>
77-
<p>Num Work Days: {form.num_work_days}</p>
78-
</div>
72+
<div style={formStyle}>
73+
<p>Name: {form.name}</p>
74+
<p>Age: {form.age}</p>
75+
<p>Pet Ownership: {form.pet_ownership}</p>
76+
<p>Pet Choices: {form.pet_choices}</p>
77+
<p>Num Work Days: {form.num_work_days}</p>
78+
</div>
7979
);
8080
},
8181
options: ["New Application"],

docs/examples/complex_form.md

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ The following example is a slightly more complex recruitment form for a pet stor
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: async (params) => {
3441
if (isNaN(Number(params.userInput))) {
3542
await params.injectMessage("Age needs to be a number!");
@@ -45,7 +52,7 @@ const MyChatBot = () => {
4552
// options: {items: ["Yes", "No"], sendOutput: false}
4653
options: ["Yes", "No"],
4754
chatDisabled: true,
48-
function: (params) => setForm({...form, pet_ownership: params.userInput}),
55+
function: (params) => updateForm({ pet_ownership: params.userInput }),
4956
path: "ask_choice"
5057
},
5158
ask_choice: {
@@ -55,12 +62,12 @@ const MyChatBot = () => {
5562
// checkboxes: ["Dog", "Cat", "Rabbit", "Hamster", "Bird"]
5663
checkboxes: {items: ["Dog", "Cat", "Rabbit", "Hamster", "Bird"], min: 2, max: 4},
5764
chatDisabled: true,
58-
function: (params) => setForm({...form, pet_choices: params.userInput}),
65+
function: (params) => updateForm({ pet_choices: params.userInput }),
5966
path: "ask_work_days"
6067
},
6168
ask_work_days: {
6269
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 }),
6471
path: async (params) => {
6572
if (isNaN(Number(params.userInput))) {
6673
await params.injectMessage("Number of work day(s) need to be a number!");
@@ -71,15 +78,18 @@ const MyChatBot = () => {
7178
},
7279
end: {
7380
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+
},
8393
options: ["New Application"],
8494
chatDisabled: true,
8595
path: "start"

0 commit comments

Comments
 (0)