taro-form-react 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +124 -0
- package/package.json +6 -3
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Taro 3 表单组件
|
|
2
|
+
|
|
3
|
+
适用于 Taro 3.x 的简单表单组件,不包含任何输入组件,只提供简单的表单布局/数据收集/联动/校验功能。**仅支持 React。**
|
|
4
|
+
|
|
5
|
+
# 特点
|
|
6
|
+
|
|
7
|
+
- [x] 完全响应式。你说用户体验?用户体验有我开发体验重要吗。
|
|
8
|
+
- [x] 强劲的动态表单支持,支持动态增删 Form.Item,暴打 @antmjs/vantui 的表单组件。
|
|
9
|
+
- [x] 性能跟屎一样,但是我肯定不会爆炸。
|
|
10
|
+
|
|
11
|
+
# 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add taro-form-react
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
# 使用
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import From from "taro-form-react";
|
|
21
|
+
// 样式只需要在入口文件引入一次
|
|
22
|
+
// 包含 Label 和一些简单布局样式
|
|
23
|
+
import "taro-form-react/dist/styles/index.scss";
|
|
24
|
+
|
|
25
|
+
import { Input, Button } from "@tarojs/components";
|
|
26
|
+
|
|
27
|
+
import type { FormActions } from "taro-form-react";
|
|
28
|
+
|
|
29
|
+
const Component = () => {
|
|
30
|
+
const formRef = useRef<FormActions>();
|
|
31
|
+
|
|
32
|
+
const handleSubmit = () => {
|
|
33
|
+
formRef.current?.submit();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const handleReset = () => {
|
|
37
|
+
formRef.current?.reset();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const handleFinish = (values) => {
|
|
41
|
+
console.log(values);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const handleFinishedFailed = (errors) => {
|
|
45
|
+
console.log(errors);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<Form
|
|
50
|
+
// ref 用于触发表单校验、设置值、获取值等操作
|
|
51
|
+
// 具体支持的操作可参考后续文档
|
|
52
|
+
ref={formRef}
|
|
53
|
+
// 丢弃值为 undefined 和 null 的字段
|
|
54
|
+
// 默认为 true,如需保留请设置为 false
|
|
55
|
+
omitNil={true}
|
|
56
|
+
// 提交成功时触发
|
|
57
|
+
onFinish={handleFinish}
|
|
58
|
+
// 提交失败时触发(有表单元素校验失败)
|
|
59
|
+
onFinishFailed={handleFinishedFailed}
|
|
60
|
+
>
|
|
61
|
+
{/* 表单元素示例 */}
|
|
62
|
+
|
|
63
|
+
{/* 基础控件 */}
|
|
64
|
+
<Form.Item
|
|
65
|
+
// Form.Item 必须存在 name 属性
|
|
66
|
+
// 仅支持数组形式,如 ["field", "array" 0, "name"]
|
|
67
|
+
name={["name"]}
|
|
68
|
+
// Form.Item 应该劫持的输入事件,默认为 onChange
|
|
69
|
+
trigger="onInput"
|
|
70
|
+
// Form.Item 应该劫持的 value,默认为 value
|
|
71
|
+
valuePropName="value"
|
|
72
|
+
// 从事件中获取值的方法,默认为 (...args) => args[0]
|
|
73
|
+
getValueFromEvent={e => e.detail.value}
|
|
74
|
+
>
|
|
75
|
+
{/* 必须存在单个子元素,不能是 React.Fragment */}
|
|
76
|
+
{/* Form.Item 会劫持该元素的输入事件 */}
|
|
77
|
+
<Input placeholder="请输入你的姓名" />
|
|
78
|
+
</Form.Item>
|
|
79
|
+
|
|
80
|
+
{/* 保留控件 */}
|
|
81
|
+
{/* Form 不会保留不存在对应控件的值 */}
|
|
82
|
+
{/* 即使你调用了 setFieldValue 设置了它 */}
|
|
83
|
+
{/* 如果需要保留某些值,但不需要展示给用户编辑 */}
|
|
84
|
+
{/* 可以使用 Form.Keep */}
|
|
85
|
+
<Form.Keep
|
|
86
|
+
fields={[
|
|
87
|
+
["field", "array", 0, "gender"],
|
|
88
|
+
["field", "array", 1, "gender"],
|
|
89
|
+
]}
|
|
90
|
+
/>
|
|
91
|
+
|
|
92
|
+
{/* 上下文控件 */}
|
|
93
|
+
{/* 用于实现复杂的表单联动 */}
|
|
94
|
+
{/* 你可以在这个控件里完全操控表单的所有数据 */}
|
|
95
|
+
{/* 且所有操作都是响应式的 */}
|
|
96
|
+
<Form.Provider>
|
|
97
|
+
{({
|
|
98
|
+
data,
|
|
99
|
+
setFieldValue,
|
|
100
|
+
}) => {
|
|
101
|
+
return (
|
|
102
|
+
<>
|
|
103
|
+
<Text>你好,{data.name}</Text>
|
|
104
|
+
<Button
|
|
105
|
+
onClick={() => {
|
|
106
|
+
setFieldValue(["name"], "");
|
|
107
|
+
}}
|
|
108
|
+
>
|
|
109
|
+
清空姓名
|
|
110
|
+
</Button>
|
|
111
|
+
</>
|
|
112
|
+
);
|
|
113
|
+
}}
|
|
114
|
+
</Form.Provider>
|
|
115
|
+
<Button onClick={handleSubmit}>提交</Button>
|
|
116
|
+
<Button onClick={handleReset}>重置</Button>
|
|
117
|
+
</Form>
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
# API
|
|
123
|
+
|
|
124
|
+
参考 TS 类型定义吧,以后再写。
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "taro-form-react",
|
|
3
3
|
"description": "A form component for Taro 3.x based on React",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"browser": "dist/index.umd.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
7
7
|
"main": "dist/index.js",
|
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "rollup -c",
|
|
16
|
-
"
|
|
16
|
+
"prepublishOnly": "npm run build",
|
|
17
|
+
"publish:patch": "npm version patch && npm publish",
|
|
18
|
+
"publish:minor": "npm version minor && npm publish",
|
|
19
|
+
"publish:major": "npm version major && npm publish"
|
|
17
20
|
},
|
|
18
21
|
"files": [
|
|
19
22
|
"dist",
|
|
@@ -57,4 +60,4 @@
|
|
|
57
60
|
"react": "^18",
|
|
58
61
|
"react-dom": "^18"
|
|
59
62
|
}
|
|
60
|
-
}
|
|
63
|
+
}
|