react-form-manage 1.0.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 +193 -0
- package/dist/index.cjs +1951 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.esm.js +1940 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +60 -0
- package/src/types/components.d.ts +134 -0
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# react-form-manage
|
|
2
|
+
|
|
3
|
+
Thư viện form nhẹ dành cho React — hỗ trợ form, field, list (array fields), và cơ chế listener để giảm re-render.
|
|
4
|
+
|
|
5
|
+
Phiên bản này xuất phát từ mã nguồn JS. Typings (.d.ts) được cung cấp trong `src/types/components.d.ts`.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Cài đặt
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install react-form-manage
|
|
13
|
+
# hoặc nếu bạn dùng yarn
|
|
14
|
+
yarn add react-form-manage
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**Lưu ý:** `react` và `react-dom` phải là `peerDependencies` của project sử dụng thư viện này. Ngoài ra `antd` chỉ cần khi bạn dùng component `AntInput`.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Export chính
|
|
22
|
+
|
|
23
|
+
Thư viện cung cấp (named/default exports):
|
|
24
|
+
|
|
25
|
+
- `default` export: `Form` (component provider)
|
|
26
|
+
- `FormItem` (component)
|
|
27
|
+
- `FormList` (component)
|
|
28
|
+
- `Input` (basic HTML input wrapper)
|
|
29
|
+
- `AntInput` (antd Input wrapper)
|
|
30
|
+
- `InputWrapper` (UI wrapper để hiển thị lỗi)
|
|
31
|
+
- Hooks: `useForm`, `useWatch`, `useSubmitDataWatch`, `useFormStateWatch`, `useFormItemControl`, `useFormListControl`
|
|
32
|
+
|
|
33
|
+
Ví dụ import:
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
import Form, { FormItem, FormList, Input, InputWrapper } from 'react-form-manage';
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
Ví dụ form đơn giản:
|
|
44
|
+
|
|
45
|
+
```jsx
|
|
46
|
+
import React from 'react';
|
|
47
|
+
import Form, { FormItem } from 'react-form-manage';
|
|
48
|
+
import Input from './path-to-package/Input';
|
|
49
|
+
|
|
50
|
+
function Example() {
|
|
51
|
+
return (
|
|
52
|
+
<Form formName="myForm" initialValues={{ name: '' }} onFinish={(values) => console.log('submit', values)}>
|
|
53
|
+
<FormItem name="name">
|
|
54
|
+
<Input />
|
|
55
|
+
</FormItem>
|
|
56
|
+
<button type="submit">Submit</button>
|
|
57
|
+
</Form>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default Example;
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`Form` sẽ đăng ký form instance trong store nội bộ và inject `value` / `onChange` vào các children của `FormItem`.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Form API / Programmatic usage
|
|
69
|
+
|
|
70
|
+
Bạn có thể lấy instance form để gọi phương thức chương trình như `submit`, `resetFields`, `setFieldValue`…
|
|
71
|
+
|
|
72
|
+
Ví dụ sử dụng hook attached trên `Form`:
|
|
73
|
+
|
|
74
|
+
```jsx
|
|
75
|
+
import React from 'react';
|
|
76
|
+
import Form from 'react-form-manage';
|
|
77
|
+
|
|
78
|
+
function CtrlExample() {
|
|
79
|
+
const [form] = Form.useForm('myForm'); // hoặc useForm('myForm')
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<div>
|
|
83
|
+
<button onClick={() => form?.submit?.()}>Submit programmatically</button>
|
|
84
|
+
<button onClick={() => form?.resetFields?.()}>Reset</button>
|
|
85
|
+
</div>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Methods available on the form instance (populated by `Form` provider):
|
|
91
|
+
|
|
92
|
+
- `submit()` / `submitAsync()`
|
|
93
|
+
- `resetFields()`
|
|
94
|
+
- `setFieldValue(name, value)`
|
|
95
|
+
- `setFieldValues(values)`
|
|
96
|
+
- `getFieldValue(name)` / `getFieldValues(names)`
|
|
97
|
+
- `setFieldFocus(name)`
|
|
98
|
+
- `getFieldErrors(names)`
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## FormList (Array field)
|
|
103
|
+
|
|
104
|
+
`FormList` là render-prop component cung cấp `listFields` và các action `add`, `remove`, `move`.
|
|
105
|
+
|
|
106
|
+
Ví dụ:
|
|
107
|
+
|
|
108
|
+
```jsx
|
|
109
|
+
import React from 'react';
|
|
110
|
+
import Form, { FormList, FormItem } from 'react-form-manage';
|
|
111
|
+
import Input from './Input';
|
|
112
|
+
|
|
113
|
+
function ListExample() {
|
|
114
|
+
return (
|
|
115
|
+
<Form formName="listForm">
|
|
116
|
+
<FormList name="items">{(fields, { add, remove }) => (
|
|
117
|
+
<div>
|
|
118
|
+
{fields.map((f, i) => (
|
|
119
|
+
<div key={f.key}>
|
|
120
|
+
<FormItem name={f.name}>
|
|
121
|
+
<Input />
|
|
122
|
+
</FormItem>
|
|
123
|
+
<button onClick={() => remove({ index: i })}>Remove</button>
|
|
124
|
+
</div>
|
|
125
|
+
))}
|
|
126
|
+
<button onClick={() => add()}>Add</button>
|
|
127
|
+
</div>
|
|
128
|
+
)}</FormList>
|
|
129
|
+
</Form>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Lưu ý: `FormList` quản lý các `key` nội bộ và push các mục cần cleanup vào `cleanUpStack` khi xóa phần tử — thư viện đã cung cấp cơ chế cleanup trong `Form` provider.
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Hooks (tóm tắt)
|
|
139
|
+
|
|
140
|
+
- `useForm(formNameOrFormInstance)` — trả về form instance đã đăng ký.
|
|
141
|
+
- `useWatch(name, formNameOrFormInstance)` — subscribe giá trị field.
|
|
142
|
+
- `useSubmitDataWatch` — watch dữ liệu submit cuối cùng.
|
|
143
|
+
- `useFormStateWatch` — watch state của form (isInitied, submitState…).
|
|
144
|
+
- `useFormItemControl` — internal hook dùng bởi `FormItem` (trả về `{ value, onChange, state, errors, onFocus, isDirty }`).
|
|
145
|
+
- `useFormListControl` — internal hook dùng bởi `FormList` (trả về `{ listFields, add, remove, move }`).
|
|
146
|
+
|
|
147
|
+
Bạn hãy xem `src/types/components.d.ts` nếu muốn biết các kiểu TypeScript sẵn có.
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## TypeScript
|
|
152
|
+
|
|
153
|
+
Thư viện đi kèm file typing (hand-written) tại `src/types/components.d.ts`.
|
|
154
|
+
|
|
155
|
+
Nếu bạn muốn tự sinh `.d.ts` từ JS, có thể thêm `tsconfig.types.json` với `allowJs` + `declaration` và chạy `tsc --emitDeclarationOnly`.
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Lưu ý khi publish
|
|
160
|
+
|
|
161
|
+
- Đặt `react` và `react-dom` vào `peerDependencies`.
|
|
162
|
+
- Đảm bảo `types` trong `package.json` trỏ tới file `.d.ts` (ví dụ `src/types/components.d.ts` hoặc `dist/index.d.ts`).
|
|
163
|
+
- Kiểm tra `npm pack` trước khi `npm publish`.
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## Contributing
|
|
168
|
+
|
|
169
|
+
1. Fork repository
|
|
170
|
+
2. Tạo branch cho tính năng/fix
|
|
171
|
+
3. Mở PR với mô tả thay đổi
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
Mặc định chưa thêm license — nếu muốn public, hãy thêm `LICENSE` (ví dụ MIT).
|
|
178
|
+
# React + Vite
|
|
179
|
+
|
|
180
|
+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
|
|
181
|
+
|
|
182
|
+
Currently, two official plugins are available:
|
|
183
|
+
|
|
184
|
+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) (or [oxc](https://oxc.rs) when used in [rolldown-vite](https://vite.dev/guide/rolldown)) for Fast Refresh
|
|
185
|
+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
|
|
186
|
+
|
|
187
|
+
## React Compiler
|
|
188
|
+
|
|
189
|
+
The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).
|
|
190
|
+
|
|
191
|
+
## Expanding the ESLint configuration
|
|
192
|
+
|
|
193
|
+
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.
|