-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Form.stories.js
100 lines (93 loc) · 3.14 KB
/
Form.stories.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React from 'react';
import {View} from 'react-native';
import TextInput from '../components/TextInput';
import AddressSearch from '../components/AddressSearch';
import Form from '../components/Form';
import * as FormActions from '../libs/actions/FormActions';
import styles from '../styles/styles';
/**
* We use the Component Story Format for writing stories. Follow the docs here:
*
* https://storybook.js.org/docs/react/writing-stories/introduction#component-story-format
*/
const story = {
title: 'Components/Form',
component: Form,
subcomponents: {TextInput, AddressSearch},
};
const Template = (args) => {
// Form consumes data from Onyx, so we initialize Onyx with the necessary data here
FormActions.setIsSubmitting(args.formID, args.formState.isSubmitting);
FormActions.setServerErrorMessage(args.formID, args.formState.serverErrorMessage);
FormActions.setDraftValues(args.formID, args.draftValues);
return (
// eslint-disable-next-line react/jsx-props-no-spreading
<Form {...args}>
<View>
<TextInput
label="Routing number"
inputID="routingNumber"
isFormInput
shouldSaveDraft
/>
</View>
<TextInput
label="Account number"
inputID="accountNumber"
containerStyles={[styles.mt4]}
isFormInput
/>
<AddressSearch
label="Street"
inputID="street"
containerStyles={[styles.mt4]}
isFormInput
/>
</Form>
);
};
// Arguments can be passed to the component by binding
// See: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
const Default = Template.bind({});
const Loading = Template.bind({});
const ServerError = Template.bind({});
const InputError = Template.bind({});
const defaultArgs = {
formID: 'TestForm',
submitButtonText: 'Submit',
validate: (values) => {
const errors = {};
if (!values.routingNumber) {
errors.routingNumber = 'Please enter a routing number';
}
if (!values.accountNumber) {
errors.accountNumber = 'Please enter an account number';
}
return errors;
},
onSubmit: (values) => {
setTimeout(() => {
alert(`Form submitted!\n\nInput values: ${JSON.stringify(values, null, 4)}`);
FormActions.setIsSubmitting('TestForm', false);
}, 1000);
},
formState: {
isSubmitting: false,
serverErrorMessage: '',
},
draftValues: {
routingNumber: '00001',
accountNumber: '1111222233331111',
},
};
Default.args = defaultArgs;
Loading.args = {...defaultArgs, formState: {isSubmitting: true}};
ServerError.args = {...defaultArgs, formState: {isSubmitting: false, serverErrorMessage: 'There was an unexpected error. Please try again later.'}};
InputError.args = {...defaultArgs, draftValues: {routingNumber: '', accountNumber: ''}};
export default story;
export {
Default,
Loading,
ServerError,
InputError,
};