@fe-free/core 2.6.2 → 2.7.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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # @fe-free/core
2
2
 
3
+ ## 2.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - feat: record
8
+
9
+ ### Patch Changes
10
+
11
+ - @fe-free/tool@2.7.0
12
+
3
13
  ## 2.6.2
4
14
 
5
15
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fe-free/core",
3
- "version": "2.6.2",
3
+ "version": "2.7.0",
4
4
  "description": "",
5
5
  "main": "./src/index.ts",
6
6
  "author": "",
@@ -41,7 +41,7 @@
41
41
  "remark-gfm": "^4.0.1",
42
42
  "vanilla-jsoneditor": "^0.23.1",
43
43
  "zustand": "^4.5.4",
44
- "@fe-free/tool": "2.6.2"
44
+ "@fe-free/tool": "2.7.0"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "@ant-design/pro-components": "2.8.9",
@@ -7,6 +7,8 @@ import {
7
7
  ProFormJavascript,
8
8
  ProFormListNumber,
9
9
  ProFormListText,
10
+ ProFormRecord,
11
+ ProFormRecordArray,
10
12
  ProFormSwitchNumber,
11
13
  ProFormUpload,
12
14
  ProFormUploadDragger,
@@ -234,3 +236,39 @@ export const ProFormImageUploadComponent: Story = {
234
236
  </ProFormBase>
235
237
  ),
236
238
  };
239
+
240
+ export const ProFormRecordComponent: Story = {
241
+ render: () => (
242
+ <ProFormBase>
243
+ <ProFormRecord
244
+ name="record1"
245
+ label="record1"
246
+ fieldProps={{
247
+ defaultItems: [
248
+ { key: 'username', label: '用户名' },
249
+ { key: 'password', label: '密码' },
250
+ ],
251
+ }}
252
+ />
253
+ <ProFormRecord name="record2" label="record2" />
254
+ </ProFormBase>
255
+ ),
256
+ };
257
+
258
+ export const ProFormRecordArrayComponent: Story = {
259
+ render: () => (
260
+ <ProFormBase>
261
+ <ProFormRecordArray
262
+ name="recordArray1"
263
+ label="recordArray1"
264
+ fieldProps={{
265
+ defaultItems: [
266
+ { key: 'username', label: '用户名' },
267
+ { key: 'password', label: '密码' },
268
+ ],
269
+ }}
270
+ />
271
+ <ProFormRecordArray name="recordArray2" label="recordArray2" />
272
+ </ProFormBase>
273
+ ),
274
+ };
@@ -4,6 +4,7 @@ export { ProFormListModalHelper } from './form_list/form_list_modal_helper';
4
4
  export { ProFormEditor } from './pro_form_editor';
5
5
  export { ProFormJavascript } from './pro_form_javascript';
6
6
  export { ProFormJSON } from './pro_form_json';
7
+ export { ProFormRecord, ProFormRecordArray } from './pro_form_record';
7
8
  export {
8
9
  ProFormSwitchNumber,
9
10
  SwitchNumber,
@@ -0,0 +1,26 @@
1
+ import type { ProFormItemProps } from '@ant-design/pro-components';
2
+ import { ProForm } from '@ant-design/pro-components';
3
+ import type { RecordArrayProps, RecordProps } from '../record';
4
+ import { Record, RecordArray } from '../record';
5
+
6
+ function ProFormRecord(props: ProFormItemProps<RecordProps>) {
7
+ const { fieldProps, ...rest } = props;
8
+
9
+ return (
10
+ <ProForm.Item {...rest}>
11
+ <Record {...(fieldProps as RecordProps)} />
12
+ </ProForm.Item>
13
+ );
14
+ }
15
+
16
+ function ProFormRecordArray(props: ProFormItemProps<RecordArrayProps>) {
17
+ const { fieldProps, ...rest } = props;
18
+
19
+ return (
20
+ <ProForm.Item {...rest}>
21
+ <RecordArray {...(fieldProps as RecordArrayProps)} />
22
+ </ProForm.Item>
23
+ );
24
+ }
25
+
26
+ export { ProFormRecord, ProFormRecordArray };
package/src/index.ts CHANGED
@@ -31,6 +31,8 @@ export {
31
31
  ProFormListModalHelper,
32
32
  ProFormListNumber,
33
33
  ProFormListText,
34
+ ProFormRecord,
35
+ ProFormRecordArray,
34
36
  ProFormSwitchNumber,
35
37
  ProFormUpload,
36
38
  ProFormUploadDragger,
@@ -43,6 +45,7 @@ export type { InfiniteListProps } from './infinite_list';
43
45
  export { Markdown } from './markdown';
44
46
  export { PageLayout, PageLayoutTabs } from './page_layout';
45
47
  export type { PageLayoutProps, PageLayoutTabsProps } from './page_layout';
48
+ export type { RecordArrayProps, RecordProps } from './record';
46
49
  export { routeTool } from './route';
47
50
  export { NumberSlider, PercentageSlider } from './slider';
48
51
  export type { NumberSliderProps, PercentageSliderProps } from './slider';
@@ -0,0 +1,158 @@
1
+ import { Button, Divider, Input } from 'antd';
2
+ import classNames from 'classnames';
3
+ import { Fragment, useCallback, useMemo, useState } from 'react';
4
+
5
+ function Item({
6
+ value,
7
+ label,
8
+ onChange,
9
+ }: {
10
+ value?: string;
11
+ label: string;
12
+ onChange: (value: string) => void;
13
+ }) {
14
+ return (
15
+ <div className="flex flex-col gap-2">
16
+ <div className="flex items-center gap-1">
17
+ <span className="text-red-500">*</span>
18
+ {label}
19
+ </div>
20
+ <Input
21
+ value={value}
22
+ placeholder={'请输入'}
23
+ onChange={(e) => {
24
+ onChange(e.target.value);
25
+ }}
26
+ />
27
+ </div>
28
+ );
29
+ }
30
+
31
+ interface RecordItem {
32
+ value?: { key?: string; value?: string };
33
+ onChange: (value: { key?: string; value?: string }) => void;
34
+ item: { key?: string; label?: string; placeholder?: string };
35
+ }
36
+
37
+ function RecordItem(props: RecordItem) {
38
+ const { value, onChange, item } = props;
39
+
40
+ // 如果提供了 key
41
+ if (item.key) {
42
+ return (
43
+ <Item
44
+ value={value?.value}
45
+ label={item.label || item.key}
46
+ onChange={(v) => {
47
+ onChange({ key: item.key, value: v });
48
+ }}
49
+ />
50
+ );
51
+ }
52
+
53
+ return (
54
+ <div className="flex flex-col gap-1">
55
+ <Item
56
+ value={value?.key}
57
+ onChange={(v) => {
58
+ onChange({ key: v, value: value?.value });
59
+ }}
60
+ label="Key"
61
+ />
62
+ <Item
63
+ value={value?.value}
64
+ onChange={(v) => {
65
+ onChange({ key: value?.key, value: v });
66
+ }}
67
+ label="Value"
68
+ />
69
+ </div>
70
+ );
71
+ }
72
+
73
+ interface RecordArrayProps {
74
+ value?: { key?: string; value?: string }[];
75
+ onChange: (value: { key?: string; value?: string }[]) => void;
76
+ defaultItems?: { key: string; label: string }[];
77
+ className?: string;
78
+ }
79
+
80
+ function RecordArray(props: RecordArrayProps) {
81
+ const { value, onChange, defaultItems, className } = props;
82
+ const [items, setItems] = useState<{ name?: string; label?: string }[]>(defaultItems || [{}]);
83
+
84
+ return (
85
+ <div className={classNames('c-bg flex flex-col gap-2 rounded p-2', className)}>
86
+ {(items || [{}])?.map((item, index) => {
87
+ const v = value?.[index] || {};
88
+ return (
89
+ <Fragment key={index}>
90
+ <RecordItem
91
+ item={item}
92
+ value={v}
93
+ onChange={(v) => {
94
+ const newValues = [...(value || [])];
95
+ newValues[index] = v || {};
96
+ onChange(newValues);
97
+ }}
98
+ />
99
+ {!defaultItems && <Divider className="my-2" />}
100
+ </Fragment>
101
+ );
102
+ })}
103
+ {!defaultItems && (
104
+ <div className="flex flex-col items-center">
105
+ <Button
106
+ onClick={() => {
107
+ setItems((v) => [...v, {}]);
108
+ }}
109
+ >
110
+ 添加
111
+ </Button>
112
+ </div>
113
+ )}
114
+ </div>
115
+ );
116
+ }
117
+
118
+ interface RecordProps extends Omit<RecordArrayProps, 'value' | 'onChange'> {
119
+ value?: Record<string, any>;
120
+ onChange: (value?: Record<string, any>) => void;
121
+ }
122
+
123
+ function Record(props: RecordProps) {
124
+ const { value, onChange, defaultItems, className } = props;
125
+
126
+ const newValue = useMemo(() => {
127
+ return Object.keys(value || {}).map((key) => ({
128
+ key,
129
+ value: value?.[key],
130
+ }));
131
+ }, [value]);
132
+
133
+ const newOnChange = useCallback(
134
+ (newV: { key?: string; value?: string }[]) => {
135
+ const newValues = {};
136
+ newV.forEach((item) => {
137
+ if (item.key) {
138
+ newValues[item.key] = item.value;
139
+ }
140
+ });
141
+
142
+ onChange(newValues);
143
+ },
144
+ [onChange],
145
+ );
146
+
147
+ return (
148
+ <RecordArray
149
+ value={newValue}
150
+ onChange={newOnChange}
151
+ defaultItems={defaultItems}
152
+ className={className}
153
+ />
154
+ );
155
+ }
156
+
157
+ export { Record, RecordArray };
158
+ export type { RecordArrayProps, RecordProps };