-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbasic.stories.tsx
161 lines (152 loc) · 4.81 KB
/
basic.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
import React from "react";
import { Form, Input, Button, InputNumber, Select, Col, Card } from "antd";
import z from "zod";
import { createSchemaFieldRule } from "../src";
import { CloseOutlined } from "@ant-design/icons";
const VALUES = ["Male", "Female"] as const;
const Genders = z.enum(VALUES);
const Cities = z.enum(["New York", "Peking", "Paris", "London"]);
const Child = z.object({
name: z
.string({
invalid_type_error: "must be string",
required_error: "required",
})
.min(1),
toys: z
.object({
name: z
.string({
invalid_type_error: "must be string",
required_error: "required",
})
.min(2),
})
.array()
.min(2),
});
const BasicSchema = z.object({
name: z.string().refine((value) => value.length > 2, {
message: "Must have more than 2 chars",
}),
height: z.number(),
gender: Genders.optional(),
address: z.object({
city: Cities,
}),
children: Child.array().min(2),
});
const rule = createSchemaFieldRule(BasicSchema);
const BasicForm = () => {
return (
<Form>
<Form.Item label="Enter name" name="name" rules={[rule]}>
<Input />
</Form.Item>
<Form.Item label="Enter height" name="height" rules={[rule]}>
<InputNumber />
</Form.Item>
<Form.Item label="Select gender" name="gender" rules={[rule]}>
<Select
options={[
{ value: "Male" },
{ value: "Female" },
{ value: "Invalid option" },
]}
/>
</Form.Item>
<Form.Item label="Select city" name={["address", "city"]} rules={[rule]}>
<Select
options={[
{ value: "New York" },
{ value: "Peking" },
{ value: "Paris" },
{ value: "London" },
{ value: "Invalid city" },
]}
/>
</Form.Item>
<Form.Item name="children" rules={[rule]}>
<Form.List name="children">
{(childrenFields, { add, remove }, { errors }) => {
return (
<div>
{childrenFields.map((child) => (
<Card
size="small"
title={`Item ${child.name + 1}`}
key={child.key}
extra={
<CloseOutlined
onClick={() => {
remove(child.name);
}}
/>
}
>
<Form.Item
label="Child"
initialValue={""}
name={[child.name, "name"]}
rules={[rule]}
>
<Input />
</Form.Item>
<Form.Item>
<Form.List name={[child.name, "toys"]}>
{(
toyFields,
{ add: addNested, remove: removeNested },
) => {
return (
<>
{toyFields.map((toy) => (
<Col offset={1} key={toy.key}>
<Form.Item
label="Toy"
initialValue={""}
name={[toy.name, "name"]}
rules={[rule]}
>
<Input
suffix={
<CloseOutlined
onClick={() => {
removeNested(toy.name);
}}
/>
}
/>
</Form.Item>
</Col>
))}
<Button onClick={() => addNested()}>
Add Toy
</Button>
</>
);
}}
</Form.List>
</Form.Item>
</Card>
))}
<div style={{ padding: "10px 0 20px" }}>
<Button onClick={() => add()}>Add Child</Button>
</div>
<Form.ErrorList errors={errors} />
</div>
);
}}
</Form.List>
</Form.Item>
<Button htmlType="submit">Submit</Button>
</Form>
);
};
export default {
component: BasicForm,
title: "Antd Zod Validation",
};
export const Basic = {
args: {},
};