Skip to content

Commit d4f042e

Browse files
committed
update
1 parent 97ebc23 commit d4f042e

File tree

6 files changed

+54
-171
lines changed

6 files changed

+54
-171
lines changed

docs/.vitepress/config.mts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export default defineConfig({
9898
text: '表单',
9999
collapsed: true,
100100
items:[
101-
{ text:"关于",link:"/guide/form/form/about"},
102101
{ text:"创建",link:"/guide/form/form/create"},
103102
{ text:"useForm",link:"/guide/form/form/use-form"},
104103
{ text:"提交表单",link:"/guide/form/form/submit"},

docs/demos/form/form/multiForm.tsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,23 @@ const store = createStore({
2828
export default ()=>{
2929

3030
const [ state ] = store.useReactive()
31-
return (
32-
<Layout direction="column">
33-
<div>
34-
{state.orders.map((order,index)=>{
35-
const { Form,reset } = useForm(store,{entry:`orders.${index}`})
36-
return <Box key={index}>
37-
<Form>
38-
<ColorBlock name="Order Total">{order.total}</ColorBlock>
39-
<Input label="Name" name="name" />
40-
<Input label="Price" name="price" type="number" />
41-
<Input label="count" name="count" type="number" />
42-
<Button onClick={()=>{}}>Submit</Button>
43-
<Button onClick={()=>reset()}>Reset</Button>
44-
</Form>
45-
</Box>
46-
})}
47-
</div>
48-
<div>
31+
return (<div>
32+
{state.orders.map((order,index)=>{
33+
const { Form,reset } = useForm(store,{entry:`orders.${index}`})
34+
return <Box key={index}>
35+
<Form>
36+
<ColorBlock name="Order Total">{order.total}</ColorBlock>
37+
<Input label="Name" name="name" />
38+
<Input label="Price" name="price" type="number" />
39+
<Input label="count" name="count" type="number" />
40+
<Button onClick={()=>{}}>Submit</Button>
41+
<Button onClick={()=>reset()}>Reset</Button>
42+
</Form>
43+
</Box>
44+
})}
45+
<Box>
4946
<JsonView data={state}/>
50-
</div>
51-
</Layout>
47+
</Box>
48+
</div>
5249
)
5350
}

docs/guide/form/field/component.md

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -93,52 +93,7 @@ const { Form,Field } = useForm({
9393

9494
因为`validate`属性是用于创建计算属性,所以可以使用`computed`、`watch`等方法来动态声明`validate`属性。
9595

96-
97-
- **简单的同步计算校验**
98-
99-
```tsx
100-
<Field
101-
validate={(value)=>{
102-
return assert(value.length>3,"长度必须大于3")
103-
}}
104-
//...
105-
/>
106-
```
107-
108-
- **简单的异步计算校验**
109-
110-
```tsx
111-
<Field
112-
validate={async (value)=>{
113-
return assert(value.length>3,"长度必须大于3")
114-
}}
115-
//...
116-
/>
117-
```
118-
119-
- **异步计算校验**
120-
121-
```tsx
122-
import { computed } from "@autostorejs/react"
123-
<Field
124-
validate={computed(async (value)=>{
125-
return assert(value.length>3,"长度必须大于3")
126-
},[])}
127-
// ...
128-
/>
129-
```
130-
131-
- **监听校验**
132-
133-
```tsx
134-
import { watch } from "@autostorejs/react"
135-
<Field
136-
validate={watch(async ({path,value})=>{
137-
return assert(value.length>3,"长度必须大于3")
138-
},()=>{...})}
139-
// ...
140-
/>
141-
```
96+
14297

14398

14499
## 字段组件属性

docs/guide/form/field/render-field.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# 渲染字段
22

3+
上文我们介绍的`useField``useFields`创建的字段是受控字段。
4+
5+
36
字段渲染有`3`种方式:
47

58
## 简单字段
@@ -10,9 +13,10 @@
1013
<input name="user.firstName" />
1114
```
1215

16+
1317
## 封装字段
1418

15-
当然,实际中的输入字段我们一般会进行封装,以便可以进行更多的控制
19+
当然,实际中的`input`字段我们一般会进行封装,以便可以进行更多的外观控制
1620

1721
我们也可以在封装元素上通过`data-field-name='<状态路径>'`标识这是一个表单字段。
1822

docs/guide/form/form/create.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,48 @@ const { Form } = useForm({
2828

2929
### 第2步:使用`Form`组件
3030

31+
```tsx
32+
<Form
33+
onSubmit={(e)=>{
34+
// 提交表单
35+
}}
36+
>
37+
{/* 字段 */}
38+
</Form>
39+
```
40+
41+
### 第3步:`Field`组件
42+
43+
`Form`组件内部,可以使用三种方式来创建`Field`组件。
44+
3145
```tsx
3246
<Form>
47+
{/* 简单输入受控字段 */}
3348
<input name="user.firstName" />
34-
<input name="user.lastName" />
35-
<input name="user.age" />
36-
<input name="user.vip" />
49+
{/* 封装受控字段 */}
50+
<div data-field-name="user.lastName">
51+
<input/>
52+
</div>
53+
{/* 自定义字段 */}
54+
<Field name="user.age" render={({bind})=>{
55+
return <input {...bind}/>
56+
}}/>
3757
</Form>
3858
```
3959

60+
更多的`Field`组件的用法请参考[Field组件](/guide/form/field/component.md)
61+
62+
### 第4步:提交表单
63+
4064
就这么简单,轻松实现`表单``store.state`之间的双向绑定了,输入的数据会自动同步到`state`中,反之亦然。
4165

66+
### 小结
67+
4268
**下面是一个简单的示例:**
4369

4470
<demo react="form/form/base.tsx"/>
4571

4672

47-
4873
## 绑定表单
4974

5075
表单绑定的数据状态来源于:

docs/guide/form/form/use-form.md

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -36,111 +36,14 @@
3636
- **监听`state`的变化**
3737

3838
使用`store.watch`监听`state`的变化,当`state`变化时,将数据更新到`name=<path>`的表单元素上即可。
39-
40-
41-
## 快速入门
42-
43-
### 第1步: 创建表单
44-
45-
首先,我们需要使用`useForm`创建一个表单。
46-
47-
```tsx
48-
import { useForm } from "@autostorejs/react"
49-
50-
const { Form } = useForm({
51-
user:{
52-
firstName:"Zhang",
53-
lastName:"Fisher",
54-
age:18,
55-
vip:false
56-
}
57-
})
58-
```
59-
60-
61-
- `useForm`内部调用`createStore`来创建一个`AutoStore`,所以其本质上`useForm`是一个`useStore`超集。所以`useForm`返回的对象中包含了`useStore`返回的对象。
62-
63-
```tsx
64-
const { useReactive,watch,$,.... } = useForm({...})
65-
```
66-
- `useForm`返回值中最主要的是`Form`组件,该组件是对标准`form`的封装。
67-
68-
69-
### 第2步:声明字段
70-
71-
72-
73-
74-
### 第3步:提交表单
75-
76-
77-
78-
79-
## 基本用法
80-
81-
`useForm`返回一个`Form`组件,该组件是对标准`form`元素的封装。
82-
83-
```tsx
84-
import { useForm } from "@autostorejs/react"
85-
const { Form } = useForm({
86-
user:{
87-
firstName:"Zhang",
88-
lastName:"Fisher",
89-
age:18,
90-
vip:false
91-
}
92-
})
93-
<Form>
94-
<input name="user.firstName" />
95-
<input name="user.lastName" />
96-
<input name="user.age" />
97-
<input name="user.vip" />
98-
</Form>
99-
```
100-
101-
就这么简单,轻松实现`表单``store.state`之间的双向绑定了,输入的数据会自动同步到`state`中,反之亦然。
102-
103-
104-
**下面是一个简单的示例:**
105-
106-
<demo react="form/form/base.tsx"/>
107-
108-
109-
110-
:::info 提示
111-
配置`input`元素的`name=<状态数据路径>`即可。
112-
:::
113-
39+
40+
11441

11542

11643
## 表单字段
11744

11845

11946

120-
## Field组件
121-
122-
`Field`组件是`useForm`提供的一个用于封装表单字段的高级组件,可以用于更加灵活的表单字段封装。
123-
124-
`Field`组件的基本用法如下:
125-
126-
```tsx
127-
128-
const { Field, Form } = useForm({...})
129-
130-
<Form onSubmit={}>
131-
<Field
132-
name="user.firstName" // 使用绑定的状态路径
133-
validate={validate} // 自定义校验规则
134-
render={({value,timeout,....,bind})=>{
135-
// 在此渲染表单字段
136-
return <div>
137-
<label>First Name</label>
138-
<input {...bind} />
139-
</div>
140-
}}
141-
/>
142-
</Form>
143-
```
14447

14548

14649

0 commit comments

Comments
 (0)