1
1
import { StoryFn } from '@storybook/react' ;
2
2
import { linkTo } from '@storybook/addon-links' ;
3
+ import { userEvent , waitFor , within } from '@storybook/testing-library' ;
4
+ import { expect } from '@storybook/jest' ;
3
5
4
6
import {
7
+ Alert ,
5
8
Block ,
6
9
Checkbox ,
7
10
CheckboxGroup ,
@@ -11,11 +14,12 @@ import {
11
14
Item ,
12
15
PasswordInput ,
13
16
Radio ,
17
+ RangeSlider ,
14
18
Select ,
15
19
Submit ,
20
+ SubmitError ,
16
21
Switch ,
17
22
TextInput ,
18
- RangeSlider ,
19
23
} from '../../../index' ;
20
24
import { NumberInput } from '../NumberInput/NumberInput' ;
21
25
import { baseProps } from '../../../stories/lists/baseProps' ;
@@ -28,6 +32,56 @@ export default {
28
32
parameters : { controls : { exclude : baseProps } } ,
29
33
} ;
30
34
35
+ const CustomSubmitErrorTemplate : StoryFn < typeof Form > = ( args ) => {
36
+ const [ form ] = Form . useForm ( ) ;
37
+
38
+ return (
39
+ < Form
40
+ form = { form }
41
+ { ...args }
42
+ onSubmit = { ( v ) => {
43
+ console . log ( 'onSubmit:' , v ) ;
44
+
45
+ throw < > Submission failed. Sorry for that :/</ > ;
46
+ } }
47
+ onValuesChange = { ( v ) => {
48
+ console . log ( 'onChange' , v ) ;
49
+ } }
50
+ >
51
+ < Field name = "text" label = "Text input" >
52
+ < TextInput />
53
+ </ Field >
54
+ < Submit > Submit</ Submit >
55
+ { form . submitError ? < Alert > { form . submitError } </ Alert > : null }
56
+ </ Form >
57
+ ) ;
58
+ } ;
59
+
60
+ const SubmitErrorTemplate : StoryFn < typeof Form > = ( args ) => {
61
+ const [ form ] = Form . useForm ( ) ;
62
+
63
+ return (
64
+ < Form
65
+ form = { form }
66
+ { ...args }
67
+ onSubmit = { ( v ) => {
68
+ console . log ( 'onSubmit:' , v ) ;
69
+
70
+ throw < > Submission failed. Sorry for that :/</ > ;
71
+ } }
72
+ onValuesChange = { ( v ) => {
73
+ console . log ( 'onChange' , v ) ;
74
+ } }
75
+ >
76
+ < Field name = "text" label = "Text input" >
77
+ < TextInput />
78
+ </ Field >
79
+ < Submit > Submit</ Submit >
80
+ < SubmitError />
81
+ </ Form >
82
+ ) ;
83
+ } ;
84
+
31
85
const AsyncValidationTemplate : StoryFn < typeof Form > = ( args ) => {
32
86
const [ form ] = Form . useForm ( ) ;
33
87
@@ -277,3 +331,16 @@ export const Default = Template.bind({});
277
331
export const ComplexErrorMessage = ComplexErrorTemplate . bind ( { } ) ;
278
332
279
333
export const AsyncValidation = AsyncValidationTemplate . bind ( { } ) ;
334
+
335
+ export const ErrorMessage = SubmitErrorTemplate . bind ( { } ) ;
336
+
337
+ export const CustomErrorMessage = CustomSubmitErrorTemplate . bind ( { } ) ;
338
+
339
+ ErrorMessage . play = CustomErrorMessage . play = async ( { canvasElement } ) => {
340
+ const canvas = within ( canvasElement ) ;
341
+ const button = await canvas . getByRole ( 'button' ) ;
342
+
343
+ await userEvent . click ( button ) ;
344
+
345
+ await waitFor ( ( ) => expect ( canvas . getByRole ( 'alert' ) ) . toBeInTheDocument ( ) ) ;
346
+ } ;
0 commit comments