@fe-free/core 2.6.1 → 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,22 @@
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
+
13
+ ## 2.6.2
14
+
15
+ ### Patch Changes
16
+
17
+ - feat: crud
18
+ - @fe-free/tool@2.6.2
19
+
3
20
  ## 2.6.1
4
21
 
5
22
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fe-free/core",
3
- "version": "2.6.1",
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.1"
44
+ "@fe-free/tool": "2.7.0"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "@ant-design/pro-components": "2.8.9",
@@ -51,7 +51,7 @@ export const Basic: Story = {
51
51
 
52
52
  return (
53
53
  <CRUDOfPure
54
- actions={['delete']}
54
+ actions={['create', 'delete']}
55
55
  tableProps={{
56
56
  columns,
57
57
  request: fakeRequest,
@@ -201,3 +201,57 @@ export const NoSearch: Story = {
201
201
  );
202
202
  },
203
203
  };
204
+
205
+ export const SpecialToolbar: Story = {
206
+ render: () => {
207
+ const columns = [
208
+ {
209
+ title: 'id',
210
+ dataIndex: 'id',
211
+ search: true,
212
+ },
213
+ {
214
+ title: '名字(省略)',
215
+ dataIndex: 'name',
216
+ search: true,
217
+ ellipsis: true,
218
+ },
219
+ {
220
+ title: 'city',
221
+ dataIndex: 'city',
222
+ },
223
+ {
224
+ title: 'area',
225
+ dataIndex: 'area',
226
+ },
227
+ ];
228
+
229
+ return (
230
+ <CRUDOfPure
231
+ actions={['create', 'delete']}
232
+ tableProps={{
233
+ columns,
234
+ request: fakeRequest,
235
+ pagination: false,
236
+ }}
237
+ requestDeleteByRecord={fakeDeleteByRecord}
238
+ deleteProps={{
239
+ nameIndex: 'id',
240
+ }}
241
+ detailForm={() => (
242
+ <>
243
+ <ProFormText
244
+ name="name"
245
+ label="名字"
246
+ required
247
+ rules={[{ required: true }]}
248
+ extra="extra extra extra extra"
249
+ />
250
+ </>
251
+ )}
252
+ requestCreateByValues={fakeCreate}
253
+ specialToolbar
254
+ />
255
+ );
256
+ },
257
+ };
@@ -9,7 +9,8 @@ interface CRUDOfPureProps<
9
9
  DataSource extends Record<string, any> = any,
10
10
  Key extends string | number = string,
11
11
  > extends CRUDProps<DataSource, Key> {
12
- // nothing
12
+ /** 特殊位置的 toolbar */
13
+ specialToolbar?: boolean;
13
14
  }
14
15
 
15
16
  function CRUDOfPureComponent(props: CRUDOfPureProps, ref: React.ForwardedRef<CRUDMethods>) {
@@ -43,6 +44,7 @@ function CRUDOfPureComponent(props: CRUDOfPureProps, ref: React.ForwardedRef<CRU
43
44
  'fec-crud-of-pure-no-search': noSearch,
44
45
  // 先这样实现
45
46
  'fec-crud-of-pure-no-toolbar': !props.actions.includes('create'),
47
+ 'fec-crud-of-pure-special-toolbar': props.specialToolbar,
46
48
  })}
47
49
  >
48
50
  <CRUD
@@ -54,4 +54,15 @@
54
54
  padding-block: 0;
55
55
  }
56
56
  }
57
+
58
+ &.fec-crud-of-pure-special-toolbar {
59
+ .ant-pro-table-list-toolbar {
60
+ margin-top: -48px;
61
+ pointer-events: none;
62
+
63
+ .ant-pro-table-list-toolbar-right > div {
64
+ pointer-events: auto;
65
+ }
66
+ }
67
+ }
57
68
  }
@@ -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 };