This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
117 lines (114 loc) · 3.76 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<!-- Don't use this in production: -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<link
href="https://unpkg.com/antd@5.2.3/dist/reset.css"
rel="stylesheet"
type="text/css"
/>
<script src="https://unpkg.com/dayjs@1.11.7/dayjs.min.js"></script>
<script src="https://unpkg.com/antd@5.2.3/dist/antd.min.js"></script>
<script src="https://unpkg.com/@idler8/observer@1.1.6"></script>
<script src="https://unpkg.com/@idler8/form-react@1.1.6"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const {
FormValidator,
useFieldState,
useFieldReader,
useFieldTrigger,
useFormValidator,
} = ReactObserver;
const { Space, Card, Input, Button } = antd;
//这是个验证函数,验证当前字段值是Success
function isSuccess(values, keys) {
const value = keys.reduce(function (prev, key) {
return prev?.[key];
}, values);
if (value !== "Success") return keys.join(".") + " not Success";
}
// 自己造一个UI样式
function CustomInput({ name, label, rule }) {
const [value, onChange, errResponse] = useFieldState(name, rule);
return (
<>
<div>{label}</div>
<Input
value={value || ""}
onChange={(e) => onChange(e.target.value)}
/>
{errResponse && <div style={{ color: "red" }}>{errResponse}</div>}
</>
);
}
// 尝试一下列表
function Classes() {
const classes = useFieldReader("classes");
const onChange = useFieldTrigger("classes");
return (
<>
{classes?.map(function (cls, index) {
return (
<CustomInput
key={index}
name={["classes", index, "name"]}
label={"班级" + (index + 1)}
rule={isSuccess}
/>
);
})}
<Button onClick={() => onChange((classes) => [...classes, {}])}>
增加班级
</Button>
</>
);
}
//这里拿一个表单
function MyForm() {
const values = useFieldReader();
const onChange = useFieldTrigger();
const validator = useFormValidator();
const handleSubmit = () => {
validator()
.then((values) => {
console.log("表单数据:", JSON.stringify(values));
alert(JSON.stringify(values));
})
.catch((errors) => {
console.log("出错了,错误信息是:", JSON.stringify(errors));
});
};
React.useEffect(function () {
onChange({ school: "SchoolName", classes: [{ name: "ClassName" }] });
}, []);
return (
<Space direction="vertical" style={{ margin: 20 }}>
<Card title="DEBUG">{JSON.stringify(values)}</Card>
<Card title="学校">
<CustomInput name="school" label="学校名称" />
<Classes />
<Button onClick={handleSubmit}>验证数据</Button>
</Card>
</Space>
);
}
//给表单一个上下文
function MyApp() {
return (
<FormValidator>
<MyForm />
</FormValidator>
);
}
const container = document.getElementById("root");
const root = ReactDOM.createRoot(container);
root.render(<MyApp />);
</script>
</body>
</html>