# Form 表单
### 介绍
用于数据录入、校验,支持输入框、单选框、复选框、文件上传等类型。
### 引入
```tsx
import { Form } from "@taroify/core"
```
## 代码演示
### 基础用法
在表单中,每个 `Form.Item` 代表一个表单项,使用 Field 的 `rules` 属性定义校验规则。
```tsx
function BasicForm() {
const onSubmit = (event: BaseEventOrig<FormProps.onSubmitEventDetail>) => {
Toast.open(JSON.stringify(event.detail.value))
}
return (
<Form onSubmit={onSubmit}>
<Toast id="toast" />
<Cell.Group inset>
<Form.Item name="username" rules={[{ required: true, message: "请填写用户名" }]}>
<Form.Label>用户名</Form.Label>
<Form.Control>
<Input placeholder="用户名" />
</Form.Control>
</Form.Item>
<Form.Item name="password" rules={[{ required: true, message: "请填写密码" }]}>
<Form.Label>密码</Form.Label>
<Form.Control>
<Input password placeholder="密码" />
</Form.Control>
</Form.Item>
<Field
name="text"
label={{ align: "left", children: "文本" }}
rules={[{ required: true, message: "请填写文本" }]}
>
<Input placeholder="请输入文本" />
</Field>
</Cell.Group>
<View style={{ margin: "16px" }}>
<Button shape="round" block color="primary" formType="submit">
提交
</Button>
</View>
</Form>
)
}
```
> Tips: 推荐可以使用`Field`组件,可以减少代码量,提高可读性
### 校验规则
通过 `rules` 定义表单校验规则,所有可用字段见[下方表格](#rule-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84)。
```tsx
function FormWithRules() {
const asyncValidator = (val: any) =>
new Promise<boolean>((resolve) => {
Toast.loading("验证中...")
setTimeout(() => {
Toast.close("toast")
resolve(/\d{6}/.test(val))
}, 1000)
})
function onValidate(errors: FormValidError[]) {
Toast.open({
style: {
textAlign: "left",
},
message: JSON.stringify(errors, undefined, 2),
})
}
return (
<Form defaultValues={{ validatorMessage: "abc" }} onValidate={onValidate}>
<Toast id="toast" />
<Cell.Group inset>
<Form.Item name="pattern" rules={[{ pattern: /\d{6}/, message: "请输入正确内容" }]}>
<Form.Label>文本</Form.Label>
<Form.Control>
<Input placeholder="正则校验" />
</Form.Control>
</Form.Item>
<Form.Item
name="validator"
rules={[{ validator: (val) => /1\d{10}/.test(val), message: "请输入正确内容" }]}
>
<Form.Label>文本</Form.Label>
<Form.Control>
<Input placeholder="函数校验" />
</Form.Control>
</Form.Item>
<Form.Item
name="validatorMessage"
rules={[{ validator: (val) => `${val ?? ""} 不合法,请重新输入` }]}
>
<Form.Label>文本</Form.Label>
<Form.Control>
<Input placeholder="校验函数返回错误提示" />
</Form.Control>
</Form.Item>
<Form.Item
name="asyncValidator"
rules={[{ validator: asyncValidator, message: "请输入正确内容" }]}
>
<Form.Label>文本</Form.Label>
<Form.Control>
<Input placeholder="异步函数校验" />
</Form.Control>
</Form.Item>
</Cell.Group>
<View style={{ margin: "16px" }}>
<Button shape="round" block color="primary" formType="submit">
提交
</Button>
</View>
</Form>
)
}
```
### 表单项类型 - 开关
在表单中使用 [Switch 组件](/components/switch)。
```tsx
<Form.Item name="switch">
<Form.Label>开关</Form.Label>
<Form.Control>
<Switch size={20} />
</Form.Control>
</Form.Item>
```
### 表单项类型 - 复选框
在表单中使用 [Checkbox 组件](/components/checkbox)。
```tsx
<Form.Item name="checkbox">
<Form.Label>复选框</Form.Label>
<Form.Control>
<Checkbox shape="square" />
</Form.Control>
</Form.Item>
<Form.Item name="checkboxGroup">
<Form.Label>复选框</Form.Label>
<Form.Control>
<Checkbox.Group direction="horizontal">
<Checkbox name="1" shape="square">
复选框 1
</Checkbox>
<Checkbox name="2" shape="square">
复选框 2
</Checkbox>
</Checkbox.Group>
</Form.Control>
</Form.Item>
```
### 表单项类型 - 单选框
在表单中使用 [Radio 组件](/components/radio)。
```tsx
<Form.Item name="radio">
<Form.Label>单选框</Form.Label>
<Form.Control>
<Radio.Group direction="horizontal">
<Radio name="1">单选框 1</Radio>
<Radio name="2">单选框 2</Radio>
</Radio.Group>
</Form.Control>
</Form.Item>
```
### 表单项类型 - 步进器
在表单中使用 [Stepper 组件](/components/stepper)。
```tsx
<Form.Item name="stepper">
<Form.Label>步进器</Form.Label>
<Form.Control>
<Stepper />
</Form.Control>
</Form.Item>
```
### 表单项类型 - 评分
在表单中使用 [Rate 组件](/components/rate)。
```tsx
<Form.Item name="rate">
<Form.Label>评分</Form.Label>
<Form.Control>
<Rate />
</Form.Control>
</Form.Item>
```
### 表单项类型 - 滑块
在表单中使用 [Slider 组件](/components/slider)。
```tsx
<Form.Item name="slider">
<Form.Label>滑块</Form.Label>
<Form.Control>
<Slider />
</Form.Control>
</Form.Item>
```
### 表单项类型 - 文件上传
在表单中使用 [Uploader 组件](/components/uploader)。
```tsx
function UploaderField() {
const itemRef = useRef<FormItemInstance>()
function onUpload() {
chooseImage({
count: 1,
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
}).then(({ tempFiles }) => {
itemRef.current?.setValue([
...(itemRef.current?.getValue() ? [...itemRef.current?.getValue()] : []),
{
url: tempFiles[0].path,
type: tempFiles[0].type,
name: tempFiles[0].originalFileObj?.name,
},
])
})
}
return (
<Form.Item ref={itemRef} name="uploader">
<Form.Label>文件上传</Form.Label>
<Form.Control>
<Uploader multiple maxFiles={2} onUpload={onUpload} />
</Form.Control>
</Form.Item>
)
}
```
### 表单项类型 - 选择器
在表单中使用 [Picker 组件](/components/picker)。
```tsx
function PickerField() {
const itemRef = useRef<FormItemInstance>()
const [open, setOpen] = useState(false)
return (
<>
<Form.Item ref={itemRef} name="picker" isLink>
<Form.Label>选择器</Form.Label>
<Form.Control>
<Input readonly placeholder="点击选择城市" onClick={() => setOpen(true)} />
</Form.Control>
</Form.Item>
<Popup mountOnEnter={false} open={open} rounded placement="bottom" onClose={setOpen}>
<Picker
onCancel={() => setOpen(false)}
onConfirm={(newValue) => {
itemRef.current?.setValue(newValue)
setOpen(false)
}}
>
<Picker.Toolbar>
<Picker.Button>取消</Picker.Button>
<Picker.Button>确认</Picker.Button>
</Picker.Toolbar>
<Picker.Column>
<Picker.Option>杭州</Picker.Option>
<Picker.Option>宁波</Picker.Option>
<Picker.Option>温州</Picker.Option>
<Picker.Option>嘉兴</Picker.Option>
<Picker.Option>湖州</Picker.Option>
</Picker.Column>
</Picker>
</Popup>
</>
)
}
```
### 表单项类型 - 时间选择器
在表单中使用 [DatetimePicker 组件](/components/datetime-picker)。
```tsx
function DatetimePickerField() {
const itemRef = useRef<FormItemInstance>()
cons
没有合适的资源?快使用搜索试试~ 我知道了~
Taroify 是移动端组件库 Vant 的 Taro 版本,两者基于相同的视觉规范.zip
共1531个文件
tsx:548个
ts:441个
scss:357个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 148 浏览量
2024-02-09
13:07:27
上传
评论
收藏 1.24MB ZIP 举报
温馨提示
快应用开发
资源推荐
资源详情
资源评论
收起资源包目录
Taroify 是移动端组件库 Vant 的 Taro 版本,两者基于相同的视觉规范.zip (1531个子文件)
.editorconfig 214B
.editorconfig 197B
.editorconfig 197B
number-keyboard.eot 2KB
.eslintignore 46B
.eslintrc 311B
.eslintrc 311B
.eslintrc 97B
.gitignore 686B
index.html 2KB
index.html 900B
subpackages.js 8KB
names.js 4KB
dev.js 4KB
dev.js 4KB
typescript.js 4KB
scss.js 3KB
gatsby-config.js 3KB
index.js 2KB
index.js 2KB
index.js 2KB
bundle.js 2KB
generate.js 1KB
404.js 1KB
www.js 960B
createPages.js 906B
index.js 894B
babel.config.js 869B
readme.js 855B
serve.js 743B
jest.config.js 710B
babel.config.js 703B
onCreateNode.js 663B
index.js 570B
prod.js 561B
prod.js 561B
stylelint.js 501B
version.js 424B
font.js 413B
gatsby-browser.js 299B
index.js 259B
wrapPageElement.js 258B
scss.test.js 153B
bundle.test.js 147B
gatsby-node.js 110B
gatsby-ssr.js 70B
babel.config.js 59B
gulpfile.js 42B
package.json 3KB
package.json 3KB
package.json 2KB
package.json 2KB
project.config.json 2KB
project.config.json 2KB
project.qq.json 1KB
project.qq.json 1KB
package.json 1KB
package.json 1015B
tsconfig.json 789B
package.json 761B
package.json 719B
tsconfig.json 701B
tsconfig.json 675B
package.json 608B
package.json 582B
package.json 579B
package.json 560B
tsconfig.json 557B
package.json 410B
project.swan.json 260B
project.swan.json 252B
project.lark.json 243B
lerna.json 171B
project.tt.json 155B
tsconfig.d.json 148B
project.tt.json 147B
LICENSE 1KB
index.md 60KB
README.md 19KB
README.md 17KB
README.md 12KB
README.md 11KB
README.md 11KB
README.md 11KB
README.md 11KB
README.md 10KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 9KB
README.md 8KB
README.md 8KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 7KB
README.md 6KB
README.md 6KB
共 1531 条
- 1
- 2
- 3
- 4
- 5
- 6
- 16
资源评论
博士僧小星
- 粉丝: 1923
- 资源: 5884
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 微信小程序 - 图书管理系统源码.zip
- 微信小程序 - 图片自适应 ,富文本解析源码.zip
- 微信小程序 - 同乐居商城:购物车合算源码
- 1、根据输入的三条边值判断能组成何种三角形,并设计测试数据进行判定覆盖测试 三条边为变量a、b、c,范围为1≤边值≤10,不在范
- SQL server 练习题目8道(小白教学).zip
- Python 手写实现 iD3 决策树算法-根据信息增益公式.zip
- 411675952289057车联助手-小窗版(三星)3.5.1.apk
- 三种快速排序方法合并在一个文件中以便直接运行的Python代码示例
- 937712277954201实习5.word
- 2程序语言基础知识pdf1_1716337722703.jpeg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功