@tiny-codes/react-easy 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/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +194 -0
- package/README.zh-CN.md +190 -0
- package/es/components/ConfigProvider/context.d.ts +49 -0
- package/es/components/ConfigProvider/context.js +5 -0
- package/es/components/ConfigProvider/context.js.map +1 -0
- package/es/components/ConfigProvider/index.d.ts +12 -0
- package/es/components/ConfigProvider/index.js +24 -0
- package/es/components/ConfigProvider/index.js.map +1 -0
- package/es/components/ConfirmAction/index.d.ts +178 -0
- package/es/components/ConfirmAction/index.js +150 -0
- package/es/components/ConfirmAction/index.js.map +1 -0
- package/es/components/DeleteConfirmAction/index.d.ts +8 -0
- package/es/components/DeleteConfirmAction/index.js +55 -0
- package/es/components/DeleteConfirmAction/index.js.map +1 -0
- package/es/components/ModalAction/index.d.ts +237 -0
- package/es/components/ModalAction/index.js +259 -0
- package/es/components/ModalAction/index.js.map +1 -0
- package/es/components/index.d.ts +9 -0
- package/es/components/index.js +9 -0
- package/es/components/index.js.map +1 -0
- package/es/hooks/index.d.ts +1 -0
- package/es/hooks/index.js +2 -0
- package/es/hooks/index.js.map +1 -0
- package/es/hooks/useLocalizedText.d.ts +11 -0
- package/es/hooks/useLocalizedText.js +16 -0
- package/es/hooks/useLocalizedText.js.map +1 -0
- package/es/hooks/useRefFunction.d.ts +13 -0
- package/es/hooks/useRefFunction.js +22 -0
- package/es/hooks/useRefFunction.js.map +1 -0
- package/es/hooks/useValidateContext.d.ts +2 -0
- package/es/hooks/useValidateContext.js +16 -0
- package/es/hooks/useValidateContext.js.map +1 -0
- package/es/index.d.ts +2 -0
- package/es/index.js +3 -0
- package/es/index.js.map +1 -0
- package/lib/components/ConfigProvider/context.d.ts +49 -0
- package/lib/components/ConfigProvider/context.js +5 -0
- package/lib/components/ConfigProvider/context.js.map +1 -0
- package/lib/components/ConfigProvider/index.d.ts +12 -0
- package/lib/components/ConfigProvider/index.js +24 -0
- package/lib/components/ConfigProvider/index.js.map +1 -0
- package/lib/components/ConfirmAction/index.d.ts +178 -0
- package/lib/components/ConfirmAction/index.js +150 -0
- package/lib/components/ConfirmAction/index.js.map +1 -0
- package/lib/components/DeleteConfirmAction/index.d.ts +8 -0
- package/lib/components/DeleteConfirmAction/index.js +55 -0
- package/lib/components/DeleteConfirmAction/index.js.map +1 -0
- package/lib/components/ModalAction/index.d.ts +237 -0
- package/lib/components/ModalAction/index.js +259 -0
- package/lib/components/ModalAction/index.js.map +1 -0
- package/lib/components/index.d.ts +9 -0
- package/lib/components/index.js +9 -0
- package/lib/components/index.js.map +1 -0
- package/lib/hooks/index.d.ts +1 -0
- package/lib/hooks/index.js +2 -0
- package/lib/hooks/index.js.map +1 -0
- package/lib/hooks/useLocalizedText.d.ts +11 -0
- package/lib/hooks/useLocalizedText.js +16 -0
- package/lib/hooks/useLocalizedText.js.map +1 -0
- package/lib/hooks/useRefFunction.d.ts +13 -0
- package/lib/hooks/useRefFunction.js +22 -0
- package/lib/hooks/useRefFunction.js.map +1 -0
- package/lib/hooks/useValidateContext.d.ts +2 -0
- package/lib/hooks/useValidateContext.js +16 -0
- package/lib/hooks/useValidateContext.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -0
- package/package.json +61 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<!-- markdownlint-disable MD009, MD024 -->
|
|
2
|
+
|
|
3
|
+
# Changelog
|
|
4
|
+
|
|
5
|
+
- ## 1.0.0
|
|
6
|
+
|
|
7
|
+
2025-2-7
|
|
8
|
+
|
|
9
|
+
The first release ships the following contents
|
|
10
|
+
|
|
11
|
+
- **Components**
|
|
12
|
+
|
|
13
|
+
- `ConfirmAction` component that renders a button that triggers a confirmation modal before executing the action
|
|
14
|
+
- `DeleteConfirmAction` component that renders a button that triggers a deletion confirmation modal before deleting action
|
|
15
|
+
- `ModalAction` component that renders a button that triggers a modal with a form to collect user input
|
|
16
|
+
- `ConfigProvider` component that provides a global configuration for the components
|
|
17
|
+
|
|
18
|
+
- **Hooks**
|
|
19
|
+
|
|
20
|
+
- `useRefFunction` hook that wraps a function to an immutable function
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 李凤宝(Leo)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# @tiny-codes/react-easy
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
> Helps you use React and AntDesign more easily
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@tiny-codes/react-easy)
|
|
8
|
+
[](https://bundlephobia.com/result?p=@tiny-codes/react-easy)
|
|
9
|
+
[](https://www.npmjs.com/package/@tiny-codes/react-easy)
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
⬇️ [Introduction](#introduction) | [Installation](#installation) | [Usage](#usage) | [Compatibility](#compatibility) ⬇️
|
|
13
|
+
|
|
14
|
+
## Introduction
|
|
15
|
+
|
|
16
|
+
Includes a series of React components, some of which are secondary encapsulations of AntDesign, helping you use the AntDesign component library more easily. In addition, it also includes some common Hooks and utility functions.
|
|
17
|
+
|
|
18
|
+
> The library is shipped in ECMAScript version `ES2016`
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Install using npm:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @tiny-codes/react-easy
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Install using pnpm:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add @tiny-codes/react-easy
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Install using bun:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
bun add @tiny-codes/react-easy
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Or using yarn:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
yarn add @tiny-codes/react-easy
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### ConfigProvider
|
|
49
|
+
|
|
50
|
+
You can use `ConfigProvider` to provide a global configuration for the components.
|
|
51
|
+
|
|
52
|
+
```jsx
|
|
53
|
+
import { ConfigProvider } from '@tiny-codes/react-easy';
|
|
54
|
+
import { useTranslation } from 'react-i18next';
|
|
55
|
+
|
|
56
|
+
const { t } = useTranslation();
|
|
57
|
+
|
|
58
|
+
<ConfigProvider
|
|
59
|
+
localize={t}
|
|
60
|
+
defaultConfirmTitle="common.confirm"
|
|
61
|
+
defaultConfirmContent="common.confirm.content"
|
|
62
|
+
defaultDeletionConfirmTitle="common.confirm"
|
|
63
|
+
defaultDeletionConfirmContent="common.confirmDeleteValue"
|
|
64
|
+
>
|
|
65
|
+
<App />
|
|
66
|
+
</ConfigProvider>;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
In the example above, the `localize` function uses `react-i18next`, you can also use other internationalization libraries, or directly pass in a custom function that satisfies your internationalization solution.
|
|
70
|
+
|
|
71
|
+
`defaultConfirmTitle` and `defaultConfirmContent` are the default confirmation box title and content, `defaultDeletionConfirmTitle` and `defaultDeletionConfirmContent` are the default deletion confirmation box title and content. You can use the key of the internationalization resource, or use plain text if you don't consider internationalization.
|
|
72
|
+
|
|
73
|
+
### ConfirmAction (Confirm box)
|
|
74
|
+
|
|
75
|
+
```jsx
|
|
76
|
+
import { ConfirmAction } from '@tiny-codes/react-easy';
|
|
77
|
+
|
|
78
|
+
<ConfirmAction.Button title="Are you sure?" content="This action cannot be undone!" onOk={handleTurnOff}>
|
|
79
|
+
Turn off
|
|
80
|
+
</ConfirmAction.Button>;
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
With the default values of `ConfigProvider`, you can simplify the code further:
|
|
84
|
+
|
|
85
|
+
```jsx
|
|
86
|
+
<ConfirmAction.Button onOk={handleTurnOff}>Turn off</ConfirmAction.Button>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### DeleteConfirmAction (Deletion confirm box)
|
|
90
|
+
|
|
91
|
+
```jsx
|
|
92
|
+
import { DeleteConfirmAction } from '@tiny-codes/react-easy';
|
|
93
|
+
|
|
94
|
+
<DeleteConfirmAction.Button title="Are you sure?" content="This action cannot be undone!" onOk={handleDelete}>
|
|
95
|
+
Delete
|
|
96
|
+
</DeleteConfirmAction.Button>;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
With the default values of `ConfigProvider`, you can simplify the code further:
|
|
100
|
+
|
|
101
|
+
```jsx
|
|
102
|
+
<DeleteConfirmAction.Button onOk={handleDelete}>Delete</DeleteConfirmAction.Button>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### ModalAction (Modal box)
|
|
106
|
+
|
|
107
|
+
```jsx
|
|
108
|
+
import { ModalAction } from '@tiny-codes/react-easy';
|
|
109
|
+
|
|
110
|
+
<ModalAction.Button title="Edit" onOk={handleEdit}>
|
|
111
|
+
Edit
|
|
112
|
+
</ModalAction.Button>;
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### withModalAction
|
|
116
|
+
|
|
117
|
+
This is a higher-order component that wraps a form component into a modal, and when you click the button to display the modal, the content of the modal is the form component, which is used to edit data.
|
|
118
|
+
|
|
119
|
+
_form.tsx_
|
|
120
|
+
|
|
121
|
+
```jsx
|
|
122
|
+
import { withModalAction, FormCompPropsConstraint } from '@tiny-codes/react-easy';
|
|
123
|
+
|
|
124
|
+
type FormProps = { data?: FormData }; // Form component properties
|
|
125
|
+
type FormData = { name: string; age: number; } // Form binding data
|
|
126
|
+
|
|
127
|
+
const EditForm: React.FC<FormProps & FormCompPropsConstraint<FormData>> = (props) => {
|
|
128
|
+
// The form instance is automatically injected by withModalAction, do not create the form instance yourself
|
|
129
|
+
// onSave is to pass the submission function to withModalAction, which is called when the user clicks the OK button
|
|
130
|
+
const { data, form, onSave } = props;
|
|
131
|
+
|
|
132
|
+
// 3. Click the OK button to save the form data
|
|
133
|
+
const handleSubmit = useRefFunction(async (values: FormData) => {
|
|
134
|
+
await axios.put('/api/edit', values);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// 1. Register save event
|
|
138
|
+
useEffect(() => { onSave(handleSubmit); }, [onSave, handleSubmit]);
|
|
139
|
+
|
|
140
|
+
// 2. Bind form data
|
|
141
|
+
return (
|
|
142
|
+
<Form form={form} initialValues={data}>
|
|
143
|
+
<Form.Item name="name" label="Name"> <Input /> </Form.Item>
|
|
144
|
+
<Form.Item name="age" label="Age"> <InputNumber /> </Form.Item>
|
|
145
|
+
</Form>
|
|
146
|
+
);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export default withModalAction(EditForm);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
_app.tsx_
|
|
153
|
+
|
|
154
|
+
```jsx
|
|
155
|
+
<EditModalAction>Edit</EditModalAction>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### useRefFunction
|
|
159
|
+
|
|
160
|
+
`useRefFunction` is used to wrap a function into an immutable function, which is suitable for scenarios that need to be used in `useEffect`, avoiding repeated execution of useEffect due to changes in function references. Another common scenario is that multiple variables are used in useEffect, but only one variable needs to be monitored in practice. We may have to use multiple `useRef` to save those other variables so that they do not appear in the dependency array of useEffect. At this time, we can use `useRefFunction` to solve this problem.
|
|
161
|
+
|
|
162
|
+
`useRefFunction` returns an immutable function, the reference of this function is immutable throughout the lifecycle of the component, but the variables used inside are real-time changes.
|
|
163
|
+
|
|
164
|
+
```jsx
|
|
165
|
+
import { useRefFunction } from '@tiny-codes/react-easy';
|
|
166
|
+
|
|
167
|
+
const Foo: React.FC<{ value: string; }> = (props) => {
|
|
168
|
+
const printValue = useRefFunction(() => {
|
|
169
|
+
// The value here changes in real time, but the reference of printValue is stable and unchanged
|
|
170
|
+
console.log(props.value);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
const timer = setInterval(() => { printValue(); }, 1000);
|
|
175
|
+
return () => { clearInterval(timer); };
|
|
176
|
+
}, [printValue])
|
|
177
|
+
|
|
178
|
+
return null;
|
|
179
|
+
};
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Compatibility
|
|
183
|
+
|
|
184
|
+
- `react` >= 16.8.0
|
|
185
|
+
- `react-is` >= 16.8.0 _(Should be consistent with react)_
|
|
186
|
+
- `antd` >= 5.1.0
|
|
187
|
+
|
|
188
|
+
> To support different versions of npm dependencies, we use `peerDependencies` in the package.json declaration instead of `dependencies`, which requires you to explicitly install these dependencies in your project and ensure that they meet the version requirements. If these dependencies are not installed in the project, it may cause the installation of `@tiny-codes/react-easy` to fail.
|
|
189
|
+
>
|
|
190
|
+
> The ECMAScript version of shipped codes is `ES2016`, please ensure that your bundler tool supports this version
|
|
191
|
+
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
```
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# @tiny-codes/react-easy
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
> 让使用 React 和 AntDesign 变得更简单
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@tiny-codes/react-easy)
|
|
8
|
+
[](https://bundlephobia.com/result?p=@tiny-codes/react-easy)
|
|
9
|
+
[](https://www.npmjs.com/package/@tiny-codes/react-easy)
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
⬇️ [介绍](#介绍) | [安装](#安装) | [使用](#使用) | [兼容性](#兼容性) ⬇️
|
|
13
|
+
|
|
14
|
+
## 介绍
|
|
15
|
+
|
|
16
|
+
包含一系列的React组件,其中一些是对AntDesign的二次封装,帮助你更方便地使用AntDesign组件库。除此之外还包含一些常用的Hooks和工具函数。
|
|
17
|
+
|
|
18
|
+
> 该库发布的ECMAScript版本为 `ES2016`
|
|
19
|
+
|
|
20
|
+
## 安装
|
|
21
|
+
|
|
22
|
+
使用 npm 安装:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @tiny-codes/react-easy
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
使用 pnpm 安装:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add @tiny-codes/react-easy
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
使用 bun 安装:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
bun add @tiny-codes/react-easy
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
或者使用 yarn 安装:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
yarn add @tiny-codes/react-easy
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## 使用
|
|
47
|
+
|
|
48
|
+
### ConfigProvider
|
|
49
|
+
|
|
50
|
+
你可以使用 `ConfigProvider` 为组件提供全局配置
|
|
51
|
+
|
|
52
|
+
```jsx
|
|
53
|
+
import { ConfigProvider } from '@tiny-codes/react-easy';
|
|
54
|
+
import { useTranslation } from 'react-i18next';
|
|
55
|
+
|
|
56
|
+
const { t } = useTranslation();
|
|
57
|
+
|
|
58
|
+
<ConfigProvider
|
|
59
|
+
localize={t}
|
|
60
|
+
defaultConfirmTitle="common.confirm"
|
|
61
|
+
defaultConfirmContent="common.confirm.content"
|
|
62
|
+
defaultDeletionConfirmTitle="common.confirm"
|
|
63
|
+
defaultDeletionConfirmContent="common.confirmDeleteValue"
|
|
64
|
+
>
|
|
65
|
+
<App />
|
|
66
|
+
</ConfigProvider>;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
在上面的例子中,`localize` 函数使用了`react-i18next`,你也可以使用其它的国际化库,或者直接传入一个自定义函数,使用适合你的国际化方案。
|
|
70
|
+
|
|
71
|
+
`defaultConfirmTitle` 和 `defaultConfirmContent` 是默认的确认框标题和内容,`defaultDeletionConfirmTitle` 和 `defaultDeletionConfirmContent` 是默认的删除确认框标题和内容。你可以使用国际化资源的键值,也可以使用普通文本,如果不考虑国际化的话。
|
|
72
|
+
|
|
73
|
+
### ConfirmAction (确认框)
|
|
74
|
+
|
|
75
|
+
```jsx
|
|
76
|
+
import { ConfirmAction } from '@tiny-codes/react-easy';
|
|
77
|
+
|
|
78
|
+
<ConfirmAction.Button title="是否确认?" content="这个操作无法撤销!" onOk={handleTurnOff}>
|
|
79
|
+
关闭
|
|
80
|
+
</ConfirmAction.Button>;
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
借助 `ConfigProvider` 的默认值,你可以将代码进一步简化为:
|
|
84
|
+
|
|
85
|
+
```jsx
|
|
86
|
+
<ConfirmAction.Button onOk={handleTurnOff}>关闭</ConfirmAction.Button>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### DeleteConfirmAction(删除确认框)
|
|
90
|
+
|
|
91
|
+
```jsx
|
|
92
|
+
import { DeleteConfirmAction } from '@tiny-codes/react-easy';
|
|
93
|
+
|
|
94
|
+
<DeleteConfirmAction.Button title="确定删除吗?" content="删除动作无法撤销!" onOk={handleDelete}>
|
|
95
|
+
删除
|
|
96
|
+
</DeleteConfirmAction.Button>;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
借助 `ConfigProvider` 的默认值,你可以将代码进一步简化为:
|
|
100
|
+
|
|
101
|
+
```jsx
|
|
102
|
+
<DeleteConfirmAction.Button onOk={handleDelete}>删除</DeleteConfirmAction.Button>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### ModalAction (模态框)
|
|
106
|
+
|
|
107
|
+
```jsx
|
|
108
|
+
import { ModalAction } from '@tiny-codes/react-easy';
|
|
109
|
+
|
|
110
|
+
<ModalAction.Button title="Edit" onOk={handleEdit}>
|
|
111
|
+
Edit
|
|
112
|
+
</ModalAction.Button>;
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### withModalAction
|
|
116
|
+
|
|
117
|
+
这是一个高阶组件,用于将一个表单组件包装成一个模态框,当点击按钮显示模态框时,模态框内容就是这个表单组件,用于编辑数据。
|
|
118
|
+
|
|
119
|
+
_form.tsx_
|
|
120
|
+
|
|
121
|
+
```jsx
|
|
122
|
+
import { withModalAction, FormCompPropsConstraint } from '@tiny-codes/react-easy';
|
|
123
|
+
|
|
124
|
+
type FormProps = { data?: FormData }; // 表单组件的属性
|
|
125
|
+
type FormData = { name: string; age: number; } // 表单绑定数据
|
|
126
|
+
|
|
127
|
+
const EditForm: React.FC<FormProps & FormCompPropsConstraint<FormData>> = (props) => {
|
|
128
|
+
// form实例是由 withModalAction 自动注入的,不要自己创建表单实例
|
|
129
|
+
// onSave的作用是将提交函数传递给 withModalAction,在用户点击确定按钮时调用
|
|
130
|
+
const { data, form, onSave } = props;
|
|
131
|
+
|
|
132
|
+
// 3. 点击确定按钮,保存表单数据
|
|
133
|
+
const handleSubmit = useRefFunction(async (values: FormData) => {
|
|
134
|
+
await axios.put('/api/edit', values);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// 1. 注册保存事件
|
|
138
|
+
useEffect(() => { onSave(handleSubmit); }, [onSave, handleSubmit]);
|
|
139
|
+
|
|
140
|
+
// 2. 绑定表单数据
|
|
141
|
+
return (
|
|
142
|
+
<Form form={form} initialValues={data}>
|
|
143
|
+
<Form.Item name="name" label="姓名"> <Input /> </Form.Item>
|
|
144
|
+
<Form.Item name="age" label="年龄"> <InputNumber /> </Form.Item>
|
|
145
|
+
</Form>
|
|
146
|
+
);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export default withModalAction(EditForm);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
_app.tsx_
|
|
153
|
+
|
|
154
|
+
```jsx
|
|
155
|
+
<EditModalAction>编辑</EditModalAction>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### useRefFunction
|
|
159
|
+
|
|
160
|
+
`useRefFunction` 用于将一个函数包装成不可变的函数, 适用于需要在 `useEffect` 中使用的场景,避免因为函数引用的变化导致 useEffect 重复执行。另外一个常见的场景是,在 useEffect 中使用了多个变量,但实际只需要监听某一个变量,我们可能不得不使用多个 `useRef` 来保存其它那些变量,以不让他们出现在 useEffect 的依赖数组中,这时候我们可以使用 `useRefFunction` 来解决这个问题。
|
|
161
|
+
|
|
162
|
+
`useRefFunction` 会返回一个不可变的函数,这个函数的引用在组件的整个生命周期中都是不变的,但在其内部使用的变量是实时变化的。
|
|
163
|
+
|
|
164
|
+
```jsx
|
|
165
|
+
import { useRefFunction } from '@tiny-codes/react-easy';
|
|
166
|
+
|
|
167
|
+
const Foo: React.FC<{ value: string; }> = (props) => {
|
|
168
|
+
const printValue = useRefFunction(() => {
|
|
169
|
+
// 这里的 value 是实时变化的,但 printValue 的引用是稳定不变的
|
|
170
|
+
console.log(props.value);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
const timer = setInterval(() => { printValue(); }, 1000);
|
|
175
|
+
return () => { clearInterval(timer); };
|
|
176
|
+
}, [printValue])
|
|
177
|
+
|
|
178
|
+
return null;
|
|
179
|
+
};
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## 兼容性
|
|
183
|
+
|
|
184
|
+
- `react` >= 16.8.0
|
|
185
|
+
- `react-is` >= 16.8.0 _(需要与 react 版本一致)_
|
|
186
|
+
- `antd` >= 5.1.0
|
|
187
|
+
|
|
188
|
+
> 为了支持不同版本的 npm 依赖库,我们在 package.json 声明中使用了`peerDependencies`,而不是`dependencies`,这需要在你的项目中显式安装这些依赖库,并且确保它们符合版本要求。如果项目中没有安装这些依赖库,可能会导致 `@tiny-codes/react-easy` 安装失败。
|
|
189
|
+
>
|
|
190
|
+
> npm 包输出的 ECMAScript 版本为 `ES2016`,请确保你的打包工具支持这个版本
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
export interface AntHelperContextProps {
|
|
3
|
+
/**
|
|
4
|
+
* **EN:** Default title of the confirm dialog, used for global configuration, can be normal text
|
|
5
|
+
* or the key of localized resources, the key of localized resources will be converted into
|
|
6
|
+
* localized text through the `localize` method
|
|
7
|
+
*
|
|
8
|
+
* **CN:** 确认弹框的默认标题,用于全局配置,可以是普通文本或本地化资源的键值,本地化资源的键值会通过`localize`方法转换成本地化文本
|
|
9
|
+
*/
|
|
10
|
+
defaultConfirmTitle?: ReactNode;
|
|
11
|
+
/**
|
|
12
|
+
* **EN:** Default content of the confirm dialog, used for global configuration, can be normal
|
|
13
|
+
* text or the key of localized resources, the key of localized resources will be converted into
|
|
14
|
+
* localized text through the `localize` method
|
|
15
|
+
*
|
|
16
|
+
* **CN:** 确认弹框的默认内容,用于全局配置,可以是普通文本或本地化资源的键值,本地化资源的键值会通过`localize`方法转换成本地化文本
|
|
17
|
+
*/
|
|
18
|
+
defaultConfirmContent?: ReactNode;
|
|
19
|
+
/**
|
|
20
|
+
* **EN:** Default title of the deletion confirmation dialog, used for global configuration, can
|
|
21
|
+
* be normal text or the key of localized resources, the key of localized resources will be
|
|
22
|
+
* converted into localized text through the `localize` method
|
|
23
|
+
*
|
|
24
|
+
* **CN:** 删除确认弹框的默认标题,用于全局配置,可以是普通文本或本地化资源的键值,本地化资源的键值会通过`localize`方法转换成本地化文本
|
|
25
|
+
*/
|
|
26
|
+
defaultDeletionConfirmTitle?: ReactNode;
|
|
27
|
+
/**
|
|
28
|
+
* **EN:** Default content of the deletion confirmation dialog, used for global configuration, can
|
|
29
|
+
* be normal text or the key of localized resources, the key of localized resources will be
|
|
30
|
+
* converted into localized text through the `localize` method
|
|
31
|
+
*
|
|
32
|
+
* **CN:** 删除确认弹框的默认内容,用于全局配置,可以是普通文本或本地化资源的键值,本地化资源的键值会通过`localize`方法转换成本地化文本
|
|
33
|
+
*/
|
|
34
|
+
defaultDeletionConfirmContent?: ReactNode;
|
|
35
|
+
/**
|
|
36
|
+
* **EN:** Get localized text
|
|
37
|
+
*
|
|
38
|
+
* **CN:** 获取本地化文本
|
|
39
|
+
*
|
|
40
|
+
* @param key Normal text or key of local resource | 普通文本或本地资源的键值
|
|
41
|
+
* @param args Parameters of the localized text | 本地化文本的参数
|
|
42
|
+
*
|
|
43
|
+
* @returns Localized text | 本地化文本
|
|
44
|
+
*/
|
|
45
|
+
localize?: <T>(key: T, args?: Record<string, unknown>) => ReactNode;
|
|
46
|
+
}
|
|
47
|
+
export declare const defaultContextValue: AntHelperContextProps;
|
|
48
|
+
declare const AntHelperContext: import("react").Context<AntHelperContextProps>;
|
|
49
|
+
export default AntHelperContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/components/ConfigProvider/context.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAkB,MAAM,OAAO,CAAC;AAoDtD,MAAM,CAAC,MAAM,mBAAmB,GAA0B,EAAE,CAAC;AAE7D,MAAM,gBAAgB,GAAG,aAAa,CAAwB,mBAAmB,CAAC,CAAC;AAEnF,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FC, ReactNode } from 'react';
|
|
2
|
+
import { type AntHelperContextProps } from './context';
|
|
3
|
+
export interface ConfigProviderProps extends AntHelperContextProps {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* **EN:** Provide global configuration for AntdHelper
|
|
8
|
+
*
|
|
9
|
+
* **CN:** 提供AntdHelper的全局配置
|
|
10
|
+
*/
|
|
11
|
+
declare const ConfigProvider: FC<ConfigProviderProps>;
|
|
12
|
+
export default ConfigProvider;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
import AntHelperContext from './context';
|
|
14
|
+
/**
|
|
15
|
+
* **EN:** Provide global configuration for AntdHelper
|
|
16
|
+
*
|
|
17
|
+
* **CN:** 提供AntdHelper的全局配置
|
|
18
|
+
*/
|
|
19
|
+
const ConfigProvider = (props) => {
|
|
20
|
+
const { children } = props, restProps = __rest(props, ["children"]);
|
|
21
|
+
return _jsx(AntHelperContext.Provider, { value: restProps, children: children });
|
|
22
|
+
};
|
|
23
|
+
export default ConfigProvider;
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/ConfigProvider/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,gBAAgD,MAAM,WAAW,CAAC;AAMzE;;;;GAIG;AACH,MAAM,cAAc,GAA4B,CAAC,KAAK,EAAE,EAAE;IACxD,MAAM,EAAE,QAAQ,KAAmB,KAAK,EAAnB,SAAS,UAAK,KAAK,EAAlC,YAA0B,CAAQ,CAAC;IACzC,OAAO,KAAC,gBAAgB,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,YAAG,QAAQ,GAA6B,CAAC;AAC7F,CAAC,CAAC;AAEF,eAAe,cAAc,CAAC"}
|