Skip to content

Commit 8d15cff

Browse files
authored
docs(form): add async pattern validation (#5408)
1 parent f28d479 commit 8d15cff

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { StoryFn } from '@storybook/react-vite'
2+
import { Stack } from '@ultraviolet/ui'
3+
import type { ComponentProps } from 'react'
4+
import { Submit } from '../../Submit'
5+
import { SubmitErrorAlert } from '../../SubmitErrorAlert'
6+
import { TextInputField } from '..'
7+
import { Template } from './Template.stories'
8+
9+
const emailValidate = {
10+
pattern: async (value: string) => {
11+
const res = await new Promise<string | true>(resolve => {
12+
setTimeout(() => {
13+
resolve(value.includes('@') ? true : `Email must have a @. ${value}`)
14+
}, 500)
15+
})
16+
17+
return res
18+
},
19+
}
20+
21+
export const AsyncValidation: StoryFn<
22+
ComponentProps<typeof TextInputField>
23+
> = args => (
24+
<Stack gap={1}>
25+
<TextInputField {...args} validate={emailValidate} />
26+
<SubmitErrorAlert />
27+
<Submit>Submit</Submit>
28+
</Stack>
29+
)
30+
31+
AsyncValidation.args = {
32+
...Template.args,
33+
required: true,
34+
}

packages/form/src/components/TextInputField/__stories__/index.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ export default {
88
component: TextInputField,
99
decorators: [
1010
ChildStory => {
11-
const methods = useForm()
11+
const methods = useForm({
12+
mode: 'onChange',
13+
})
14+
1215
const {
1316
errors,
1417
isDirty,
@@ -69,3 +72,4 @@ export default {
6972

7073
export { Playground } from './Playground.stories'
7174
export { Required } from './Required.stories'
75+
export { AsyncValidation } from './AsyncValidation.stories'

0 commit comments

Comments
 (0)