-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathSimpleForm.stories.tsx
171 lines (159 loc) · 4.49 KB
/
SimpleForm.stories.tsx
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as React from 'react';
import {
maxValue,
required,
ResourceContextProvider,
testDataProvider,
} from 'ra-core';
import { AdminContext } from '../AdminContext';
import { Edit } from '../detail';
import { NumberInput, TextInput } from '../input';
import { SimpleForm } from './SimpleForm';
import { Stack } from '@mui/material';
export default { title: 'ra-ui-materialui/forms/SimpleForm' };
const data = {
id: 1,
title: 'War and Peace',
author: 'Leo Tolstoy',
year: 1869,
};
const Wrapper = ({
children,
i18nProvider = {
translate: (x, options) => options?._ ?? x,
changeLocale: () => Promise.resolve(),
getLocale: () => 'en',
},
}) => (
<AdminContext
i18nProvider={i18nProvider}
dataProvider={testDataProvider({
getOne: () => Promise.resolve({ data }),
})}
>
<ResourceContextProvider value="books">
<Edit id={1} sx={{ width: 600 }}>
{children}
</Edit>
</ResourceContextProvider>
</AdminContext>
);
export const Basic = () => (
<Wrapper>
<SimpleForm>
<TextInput source="title" fullWidth />
<TextInput source="author" />
<NumberInput source="year" />
</SimpleForm>
</Wrapper>
);
export const CustomLayout = () => (
<Wrapper>
<SimpleForm>
<TextInput source="title" fullWidth />
<Stack direction="row" gap={1} width="100%">
<TextInput source="author" sx={{ width: '50%' }} />
<NumberInput source="year" sx={{ width: '50%' }} />
</Stack>
</SimpleForm>
</Wrapper>
);
export const StackProps = () => (
<Wrapper>
<SimpleForm spacing={3} alignItems="center">
<TextInput source="title" fullWidth />
<TextInput source="author" />
<NumberInput source="year" />
</SimpleForm>
</Wrapper>
);
export const NoToolbar = () => (
<Wrapper>
<SimpleForm toolbar={false}>
<TextInput source="title" fullWidth />
<TextInput source="author" />
<NumberInput source="year" />
</SimpleForm>
</Wrapper>
);
const translate = (x, options) => {
switch (x) {
case 'resources.books.name':
return 'Books';
case 'ra.page.edit':
return 'Edit';
case 'resources.books.fields.title':
return 'Title';
case 'resources.books.fields.author':
return 'Author';
case 'resources.books.fields.year':
return 'Year';
case 'ra.action.save':
return 'Save';
case 'ra.action.delete':
return 'Delete';
case 'ra.validation.required.author':
return 'The author is required';
case 'ra.validation.maxValue':
return `The year must be less than ${options.max}`;
default:
console.warn(`Missing translation for key '${x}'`);
return options?._ ?? x;
}
};
const validate = values => {
const errors = {} as any;
if (!values.title) {
errors.title = 'The title is required';
}
if (!values.author) {
errors.author = 'ra.validation.required.author';
}
if (values.year > 2000) {
errors.year = {
message: 'ra.validation.maxValue',
args: { max: 2000 },
};
}
return errors;
};
export const GlobalValidation = () => (
<Wrapper
i18nProvider={{
translate,
changeLocale: () => Promise.resolve(),
getLocale: () => 'en',
}}
>
<SimpleForm validate={validate}>
<TextInput source="title" fullWidth />
<TextInput source="author" />
<NumberInput source="year" />
</SimpleForm>
</Wrapper>
);
export const InputBasedValidation = () => (
<Wrapper
i18nProvider={{
translate,
changeLocale: () => Promise.resolve(),
getLocale: () => 'en',
}}
>
<SimpleForm>
<TextInput
source="title"
fullWidth
validate={required('The title is required')}
/>
<TextInput
source="author"
validate={required('ra.validation.required.author')}
/>
<NumberInput
source="year"
validate={maxValue(2000, 'ra.validation.maxValue')}
/>
</SimpleForm>
</Wrapper>
);