@hw-component/form 0.0.1 → 0.0.3
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/LICENSE +201 -0
- package/es/CheckboxGroup/CheckBox/index.d.ts +7 -0
- package/es/CheckboxGroup/CheckBox/index.js +36 -0
- package/es/CheckboxGroup/hooks.d.ts +5 -0
- package/es/CheckboxGroup/hooks.js +124 -0
- package/es/CheckboxGroup/index.d.ts +4 -0
- package/es/CheckboxGroup/index.js +49 -0
- package/es/DatePicker/RangePicker.d.ts +4 -0
- package/es/DatePicker/RangePicker.js +23 -0
- package/es/DatePicker/index.d.ts +4 -0
- package/es/DatePicker/index.js +39 -0
- package/es/Input/ButtonInput.d.ts +4 -0
- package/es/Input/ButtonInput.js +80 -0
- package/es/Input/InputNumber.d.ts +4 -0
- package/es/Input/InputNumber.js +20 -0
- package/es/Input/SelectInput.d.ts +8 -0
- package/es/Input/SelectInput.js +67 -0
- package/es/Input/defaultConfig.d.ts +7 -0
- package/es/Input/defaultConfig.js +11 -0
- package/es/Input/index.d.ts +4 -0
- package/es/Input/index.js +13 -0
- package/es/RadioGroup/index.d.ts +9 -0
- package/es/RadioGroup/index.js +44 -0
- package/es/Select/components/AllSelect.d.ts +2 -1
- package/es/Select/components/AllSelect.js +9 -5
- package/es/Select/defaultConfig.d.ts +0 -1
- package/es/Select/defaultConfig.js +1 -5
- package/es/Select/hooks/changeHooks.d.ts +1 -1
- package/es/Select/hooks/changeHooks.js +8 -3
- package/es/Select/hooks/norHooks.d.ts +2 -5
- package/es/Select/hooks/norHooks.js +12 -34
- package/es/Select/index.js +12 -7
- package/es/Select/modal.d.ts +1 -2
- package/es/Select/utils.d.ts +2 -0
- package/es/Select/utils.js +39 -0
- package/es/Switch/index.d.ts +10 -0
- package/es/Switch/index.js +41 -0
- package/es/TimePicker/index.d.ts +2 -0
- package/es/config.d.ts +15 -0
- package/es/config.js +19 -0
- package/es/hooks/useChangeOptions.d.ts +10 -0
- package/es/hooks/useChangeOptions.js +25 -0
- package/es/hooks/useClassName.d.ts +1 -0
- package/es/hooks/useClassName.js +14 -0
- package/es/index.css +1 -1
- package/es/index.d.ts +11 -1
- package/es/index.js +10 -0
- package/lib/CheckboxGroup/CheckBox/index.d.ts +7 -0
- package/lib/CheckboxGroup/CheckBox/index.js +39 -0
- package/lib/CheckboxGroup/hooks.d.ts +5 -0
- package/lib/CheckboxGroup/hooks.js +125 -0
- package/lib/CheckboxGroup/index.d.ts +4 -0
- package/lib/CheckboxGroup/index.js +52 -0
- package/lib/DatePicker/RangePicker.d.ts +4 -0
- package/lib/DatePicker/RangePicker.js +26 -0
- package/lib/DatePicker/index.d.ts +4 -0
- package/lib/DatePicker/index.js +42 -0
- package/lib/Input/ButtonInput.d.ts +4 -0
- package/lib/Input/ButtonInput.js +83 -0
- package/lib/Input/InputNumber.d.ts +4 -0
- package/lib/Input/InputNumber.js +23 -0
- package/lib/Input/SelectInput.d.ts +8 -0
- package/lib/Input/SelectInput.js +70 -0
- package/lib/Input/defaultConfig.d.ts +7 -0
- package/lib/Input/defaultConfig.js +13 -0
- package/lib/Input/index.d.ts +4 -0
- package/lib/Input/index.js +16 -0
- package/lib/RadioGroup/index.d.ts +9 -0
- package/lib/RadioGroup/index.js +47 -0
- package/lib/Select/components/AllSelect.d.ts +2 -1
- package/lib/Select/components/AllSelect.js +9 -5
- package/lib/Select/defaultConfig.d.ts +0 -1
- package/lib/Select/defaultConfig.js +0 -5
- package/lib/Select/hooks/changeHooks.d.ts +1 -1
- package/lib/Select/hooks/changeHooks.js +8 -3
- package/lib/Select/hooks/norHooks.d.ts +2 -5
- package/lib/Select/hooks/norHooks.js +12 -34
- package/lib/Select/index.js +11 -6
- package/lib/Select/modal.d.ts +1 -2
- package/lib/Select/utils.d.ts +2 -0
- package/lib/Select/utils.js +41 -0
- package/lib/Switch/index.d.ts +10 -0
- package/lib/Switch/index.js +44 -0
- package/lib/TimePicker/index.d.ts +2 -0
- package/lib/config.d.ts +15 -0
- package/lib/config.js +20 -0
- package/lib/hooks/useChangeOptions.d.ts +10 -0
- package/lib/hooks/useChangeOptions.js +26 -0
- package/lib/hooks/useClassName.d.ts +1 -0
- package/lib/hooks/useClassName.js +15 -0
- package/lib/index.css +1 -1
- package/lib/index.d.ts +11 -1
- package/lib/index.js +20 -0
- package/package.json +2 -2
- package/scripts/rollup.config.js +1 -4
- package/src/components/CheckboxGroup/CheckBox/index.tsx +38 -0
- package/src/components/CheckboxGroup/hooks.ts +103 -0
- package/src/components/CheckboxGroup/index.tsx +46 -0
- package/src/components/CheckboxGroup/modal.d.ts +12 -0
- package/src/components/DatePicker/RangePicker.tsx +7 -0
- package/src/components/DatePicker/index.tsx +38 -0
- package/src/components/DatePicker/modal.d.ts +16 -0
- package/src/components/Input/ButtonInput.tsx +41 -0
- package/src/components/Input/InputNumber.tsx +5 -0
- package/src/components/Input/SelectInput.tsx +45 -0
- package/src/components/Input/defaultConfig.ts +8 -0
- package/src/components/Input/index.tsx +6 -0
- package/src/components/Input/modal.d.ts +26 -0
- package/src/components/RadioGroup/index.tsx +41 -0
- package/src/components/Select/components/AllSelect.tsx +9 -4
- package/src/components/Select/defaultConfig.tsx +0 -5
- package/src/components/Select/hooks/changeHooks.tsx +15 -4
- package/src/components/Select/hooks/norHooks.ts +2 -18
- package/src/components/Select/index.less +2 -1
- package/src/components/Select/index.tsx +14 -10
- package/src/components/Select/modal.ts +1 -2
- package/src/components/Select/utils.ts +18 -0
- package/src/components/Switch/index.tsx +47 -0
- package/src/components/TimePicker/index.tsx +1 -0
- package/src/components/config.less +1 -0
- package/src/components/config.ts +15 -0
- package/src/components/hooks/useChangeOptions.ts +22 -0
- package/src/components/hooks/useClassName.ts +8 -0
- package/src/components/index.tsx +12 -1
- package/src/components/typings.d.ts +5 -0
- package/src/pages/Checkbox/index.tsx +16 -0
- package/src/pages/DatePicker/index.tsx +15 -0
- package/src/pages/Input/index.tsx +22 -0
- package/src/pages/Radio/index.tsx +18 -0
- package/src/pages/Select/index.tsx +1 -2
- package/src/pages/Switch/index.tsx +18 -0
- package/src/routes.tsx +33 -12
- package/es/Select/index.less.js +0 -5
- package/lib/Select/index.less.js +0 -8
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from "react";
|
|
2
|
+
import type { HCheckboxProps } from "@/components/CheckboxGroup/modal";
|
|
3
|
+
import { baseConfig } from "../config";
|
|
4
|
+
|
|
5
|
+
const changValProvider = ({
|
|
6
|
+
value,
|
|
7
|
+
options,
|
|
8
|
+
valueCheckMap = baseConfig.defaultValueCheckMap,
|
|
9
|
+
}: HCheckboxProps) => {
|
|
10
|
+
const newVal = { ...value };
|
|
11
|
+
const { checked, noChecked } = valueCheckMap;
|
|
12
|
+
options?.forEach((item) => {
|
|
13
|
+
const { value: itemVal } = item;
|
|
14
|
+
if (newVal[itemVal] !== checked) {
|
|
15
|
+
newVal[itemVal] = noChecked;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return newVal;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const useValueProvider = ({
|
|
22
|
+
value,
|
|
23
|
+
valueCheckMap = baseConfig.defaultValueCheckMap,
|
|
24
|
+
keyInValue = true,
|
|
25
|
+
onChange,
|
|
26
|
+
options,
|
|
27
|
+
}: HCheckboxProps) => {
|
|
28
|
+
const [val, setVal] = useState<Record<any, any>>({});
|
|
29
|
+
const oldValueCheckMap = useMemo(() => {
|
|
30
|
+
return {
|
|
31
|
+
valueCheckMap,
|
|
32
|
+
};
|
|
33
|
+
}, []);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
setVal((oldVal) => {
|
|
36
|
+
return changValProvider({
|
|
37
|
+
value: oldVal,
|
|
38
|
+
options,
|
|
39
|
+
valueCheckMap: oldValueCheckMap.valueCheckMap,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}, [options]); //初始化数据
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
const { checked: oldChecked } = oldValueCheckMap.valueCheckMap;
|
|
45
|
+
const { checked, noChecked } = valueCheckMap;
|
|
46
|
+
setVal((oldVal) => {
|
|
47
|
+
const newVal = { ...oldVal };
|
|
48
|
+
const keys = Object.keys(newVal);
|
|
49
|
+
keys.forEach((key) => {
|
|
50
|
+
if (newVal[key] === oldChecked) {
|
|
51
|
+
newVal[key] = checked;
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
newVal[key] = noChecked;
|
|
55
|
+
});
|
|
56
|
+
oldValueCheckMap.valueCheckMap = valueCheckMap;
|
|
57
|
+
return newVal;
|
|
58
|
+
});
|
|
59
|
+
}, [valueCheckMap]);
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const { checked } = oldValueCheckMap.valueCheckMap;
|
|
63
|
+
if (!value) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!Array.isArray(value)) {
|
|
67
|
+
setVal(value);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
setVal((oldVal) => {
|
|
71
|
+
const newVal = { ...oldVal };
|
|
72
|
+
value.forEach((itemVal) => {
|
|
73
|
+
newVal[itemVal] = checked;
|
|
74
|
+
});
|
|
75
|
+
return newVal;
|
|
76
|
+
});
|
|
77
|
+
}, [value]);
|
|
78
|
+
|
|
79
|
+
const change = (key: any, changeVal: any) => {
|
|
80
|
+
const { checked, noChecked } = valueCheckMap;
|
|
81
|
+
const newVal = { ...val };
|
|
82
|
+
newVal[key] = noChecked;
|
|
83
|
+
if (changeVal === checked) {
|
|
84
|
+
newVal[key] = checked;
|
|
85
|
+
}
|
|
86
|
+
if (!onChange) {
|
|
87
|
+
setVal(newVal);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (keyInValue) {
|
|
91
|
+
onChange(newVal);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const subVal = Object.keys(newVal).filter((valKey) => {
|
|
95
|
+
return newVal[valKey] === checked;
|
|
96
|
+
});
|
|
97
|
+
onChange(subVal);
|
|
98
|
+
};
|
|
99
|
+
return {
|
|
100
|
+
val,
|
|
101
|
+
change,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Space } from "antd";
|
|
2
|
+
import { useChangeOptions } from "../hooks/useChangeOptions";
|
|
3
|
+
import type { HCheckboxProps } from "@/components/CheckboxGroup/modal";
|
|
4
|
+
import { useValueProvider } from "./hooks";
|
|
5
|
+
import Checkbox from "./CheckBox";
|
|
6
|
+
|
|
7
|
+
export default ({
|
|
8
|
+
options,
|
|
9
|
+
fieldNames,
|
|
10
|
+
onChange,
|
|
11
|
+
valueCheckMap,
|
|
12
|
+
value,
|
|
13
|
+
disabled,
|
|
14
|
+
keyInValue = true,
|
|
15
|
+
}: HCheckboxProps) => {
|
|
16
|
+
const checkOptions = useChangeOptions({ options, fieldNames });
|
|
17
|
+
const { val, change } = useValueProvider({
|
|
18
|
+
options: checkOptions,
|
|
19
|
+
onChange,
|
|
20
|
+
valueCheckMap,
|
|
21
|
+
keyInValue,
|
|
22
|
+
value,
|
|
23
|
+
});
|
|
24
|
+
console.log(val);
|
|
25
|
+
return (
|
|
26
|
+
<Space>
|
|
27
|
+
{checkOptions?.map((item) => {
|
|
28
|
+
const { label, value: itemVal } = item;
|
|
29
|
+
const checked = val[itemVal];
|
|
30
|
+
return (
|
|
31
|
+
<Checkbox
|
|
32
|
+
value={checked}
|
|
33
|
+
key={itemVal}
|
|
34
|
+
onChange={(changeVal) => {
|
|
35
|
+
change(itemVal, changeVal);
|
|
36
|
+
}}
|
|
37
|
+
valueCheckMap={valueCheckMap}
|
|
38
|
+
disabled={disabled}
|
|
39
|
+
>
|
|
40
|
+
{label}
|
|
41
|
+
</Checkbox>
|
|
42
|
+
);
|
|
43
|
+
})}
|
|
44
|
+
</Space>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CheckboxProps } from "antd/lib/checkbox/Checkbox";
|
|
2
|
+
import type { SelectProps } from "antd";
|
|
3
|
+
|
|
4
|
+
export interface HCheckboxProps
|
|
5
|
+
extends Omit<CheckboxProps, "value" | "onChange"> {
|
|
6
|
+
options?: Record<string, any>[];
|
|
7
|
+
fieldNames?: SelectProps["fieldNames"];
|
|
8
|
+
valueCheckMap?: ValueCheckMapModal;
|
|
9
|
+
keyInValue?: boolean;
|
|
10
|
+
value?: Record<string, any> | any[];
|
|
11
|
+
onChange?: (value: Record<string, any> | any[]) => void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import {DatePicker} from 'antd';
|
|
2
|
+
import {IRangePickerProps} from "@/components/DatePicker/modal";
|
|
3
|
+
const { RangePicker } = DatePicker;
|
|
4
|
+
|
|
5
|
+
export default ({value,onChange,showTime=true,...props}:IRangePickerProps)=>{
|
|
6
|
+
return <RangePicker value={value} onChange={value} showTime={showTime} {...props}/>
|
|
7
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { DatePickerProps } from "antd";
|
|
2
|
+
import type { SharedTimeProps } from "rc-picker/lib/panels/TimePanel";
|
|
3
|
+
import { DatePicker } from "antd";
|
|
4
|
+
import { useMemo } from "react";
|
|
5
|
+
import type { Moment } from "moment";
|
|
6
|
+
import moment from "moment";
|
|
7
|
+
import {IDatePickerProps} from "@/components/DatePicker/modal";
|
|
8
|
+
|
|
9
|
+
export default ({
|
|
10
|
+
value,
|
|
11
|
+
onChange,
|
|
12
|
+
showTime = true,
|
|
13
|
+
format = "X",
|
|
14
|
+
...props
|
|
15
|
+
}: IDatePickerProps) => {
|
|
16
|
+
const timeVal = useMemo(() => {
|
|
17
|
+
if (!value || !format) {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
return moment(value, format);
|
|
21
|
+
}, [value, format]);
|
|
22
|
+
const change = (val) => {
|
|
23
|
+
if (!format) {
|
|
24
|
+
onChange?.(val);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const subVal = val?.format(format);
|
|
28
|
+
onChange?.(subVal);
|
|
29
|
+
};
|
|
30
|
+
return (
|
|
31
|
+
<DatePicker
|
|
32
|
+
value={timeVal}
|
|
33
|
+
onChange={change}
|
|
34
|
+
showTime={showTime}
|
|
35
|
+
{...props}
|
|
36
|
+
/>
|
|
37
|
+
);
|
|
38
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {RangePickerProps} from "antd/es/date-picker";
|
|
2
|
+
import {SharedTimeProps} from "rc-picker/lib/panels/TimePanel";
|
|
3
|
+
import {Moment} from "moment";
|
|
4
|
+
import {DatePickerProps} from "antd";
|
|
5
|
+
|
|
6
|
+
export interface IDatePickerProps extends Omit<DatePickerProps, "onChange" | "format"> {
|
|
7
|
+
onChange?: (time?: Moment | string | number) => void;
|
|
8
|
+
format?: string;
|
|
9
|
+
showTime?: boolean | SharedTimeProps<Moment>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface IRangePickerProps extends RangePickerProps{
|
|
13
|
+
value?:any;
|
|
14
|
+
onChange?:(val:any)=>void;
|
|
15
|
+
showTime?: boolean | { defaultValue?: Moment[]};
|
|
16
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Input, Button } from "antd";
|
|
2
|
+
import type { HButtonPropsProps } from "./modal";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { useRequest } from "ahooks";
|
|
5
|
+
|
|
6
|
+
const Index: React.FC<HButtonPropsProps> = ({
|
|
7
|
+
buttonProps = {},
|
|
8
|
+
value,
|
|
9
|
+
onChange,
|
|
10
|
+
children,
|
|
11
|
+
request,
|
|
12
|
+
...props
|
|
13
|
+
}) => {
|
|
14
|
+
const { onClick, type = "primary", ...oProps } = buttonProps;
|
|
15
|
+
const { run, loading } = useRequest(
|
|
16
|
+
(val) => {
|
|
17
|
+
return request?.(val);
|
|
18
|
+
},
|
|
19
|
+
{ manual: true }
|
|
20
|
+
);
|
|
21
|
+
const change = (e) => {
|
|
22
|
+
onChange?.(e.target.value);
|
|
23
|
+
};
|
|
24
|
+
const click = async (e) => {
|
|
25
|
+
let result = value;
|
|
26
|
+
if (request) {
|
|
27
|
+
result = await run(value);
|
|
28
|
+
}
|
|
29
|
+
onClick?.(result, e);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<Input.Group compact style={{ display: "flex" }}>
|
|
34
|
+
<Input {...props} style={{ flex: 1 }} value={value} onChange={change} />
|
|
35
|
+
<Button {...oProps} type={type} onClick={click} loading={loading}>
|
|
36
|
+
{children}
|
|
37
|
+
</Button>
|
|
38
|
+
</Input.Group>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
export default Index;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Input } from "antd";
|
|
2
|
+
import type { HSelectInputProps } from "./modal";
|
|
3
|
+
import HSelect from "../Select";
|
|
4
|
+
import { defaultSelectStyle, defaultValueName } from "./defaultConfig";
|
|
5
|
+
export enum SelectInputType {
|
|
6
|
+
input,
|
|
7
|
+
select,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default ({
|
|
11
|
+
selectProps = {},
|
|
12
|
+
value = {},
|
|
13
|
+
onChange,
|
|
14
|
+
valueName = defaultValueName,
|
|
15
|
+
...props
|
|
16
|
+
}: HSelectInputProps) => {
|
|
17
|
+
const { input = "", select = "" } = valueName;
|
|
18
|
+
const { [input]: inputVal, [select]: selectVal } = value;
|
|
19
|
+
const { style = defaultSelectStyle, ...sProps } = selectProps;
|
|
20
|
+
const change = (val, key) => {
|
|
21
|
+
const newVal = { ...value };
|
|
22
|
+
newVal[key] = val;
|
|
23
|
+
const type = key === input ? SelectInputType.input : SelectInputType.select;
|
|
24
|
+
onChange?.(newVal, type);
|
|
25
|
+
};
|
|
26
|
+
return (
|
|
27
|
+
<Input
|
|
28
|
+
value={inputVal}
|
|
29
|
+
onChange={(e) => {
|
|
30
|
+
change(e.target.value, input);
|
|
31
|
+
}}
|
|
32
|
+
addonBefore={
|
|
33
|
+
<HSelect
|
|
34
|
+
{...sProps}
|
|
35
|
+
style={style}
|
|
36
|
+
value={selectVal}
|
|
37
|
+
onChange={(val) => {
|
|
38
|
+
change(val, select);
|
|
39
|
+
}}
|
|
40
|
+
/>
|
|
41
|
+
}
|
|
42
|
+
{...props}
|
|
43
|
+
/>
|
|
44
|
+
);
|
|
45
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ButtonProps, InputProps } from "antd";
|
|
2
|
+
import type { HSelectProps } from "../Select/modal";
|
|
3
|
+
import type { SelectInputType } from "./SelectInput";
|
|
4
|
+
|
|
5
|
+
type HInputProps = InputProps;
|
|
6
|
+
interface ValueNameModal {
|
|
7
|
+
input?: string;
|
|
8
|
+
select?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface HSelectInputProps extends Omit<HInputProps, "value" | "onChange"> {
|
|
12
|
+
selectProps?: HSelectProps;
|
|
13
|
+
value?: Record<string, any>;
|
|
14
|
+
onChange?: (value: Record<string, any>, type: SelectInputType) => void;
|
|
15
|
+
valueName?: ValueNameModal;
|
|
16
|
+
}
|
|
17
|
+
interface HButtonProps extends Omit<ButtonProps, "onClick"> {
|
|
18
|
+
onClick: (
|
|
19
|
+
value: string | readonly string[] | number | undefined,
|
|
20
|
+
e: HTMLElement
|
|
21
|
+
) => void;
|
|
22
|
+
}
|
|
23
|
+
interface HButtonPropsProps extends HInputProps {
|
|
24
|
+
buttonProps?: HButtonProps;
|
|
25
|
+
request?: PromiseFnResult;
|
|
26
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Radio } from "antd";
|
|
2
|
+
import type { SelectProps, RadioGroupProps } from "antd/es";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { baseConfig } from "../config";
|
|
5
|
+
|
|
6
|
+
interface IProps extends Omit<RadioGroupProps, "onChange" | "options"> {
|
|
7
|
+
onChange?: (value: any) => void;
|
|
8
|
+
fieldNames?: SelectProps["fieldNames"];
|
|
9
|
+
options?: (Record<string, any> | string | number)[];
|
|
10
|
+
}
|
|
11
|
+
export default ({
|
|
12
|
+
value,
|
|
13
|
+
options,
|
|
14
|
+
onChange,
|
|
15
|
+
fieldNames = baseConfig.defaultFieldNames,
|
|
16
|
+
...props
|
|
17
|
+
}: IProps) => {
|
|
18
|
+
const relOptions = useMemo(() => {
|
|
19
|
+
return options?.map((item) => {
|
|
20
|
+
const type = typeof item;
|
|
21
|
+
if (type === "string" || type === "number") {
|
|
22
|
+
return item;
|
|
23
|
+
}
|
|
24
|
+
const { label: labelKey = "", value: valueKey = "" } = fieldNames;
|
|
25
|
+
return {
|
|
26
|
+
label: item[labelKey],
|
|
27
|
+
value: item[valueKey],
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}, [options, fieldNames]);
|
|
31
|
+
return (
|
|
32
|
+
<Radio.Group
|
|
33
|
+
options={relOptions as RadioGroupProps["options"]}
|
|
34
|
+
value={value}
|
|
35
|
+
onChange={(e) => {
|
|
36
|
+
onChange?.(e.target.value);
|
|
37
|
+
}}
|
|
38
|
+
{...props}
|
|
39
|
+
/>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { HSelectProps } from "@/components/Select/modal";
|
|
2
2
|
import { Checkbox, Space } from "antd";
|
|
3
|
-
import
|
|
3
|
+
import "../index.less"
|
|
4
|
+
import { itemOpProvider } from "../utils";
|
|
5
|
+
import {useClassName} from "@/components/hooks/useClassName";
|
|
4
6
|
interface IProps extends HSelectProps {
|
|
5
7
|
checked?: boolean;
|
|
6
8
|
}
|
|
@@ -11,22 +13,25 @@ export default ({
|
|
|
11
13
|
value,
|
|
12
14
|
serviceSearch,
|
|
13
15
|
options,
|
|
16
|
+
fieldNames,
|
|
14
17
|
}: IProps) => {
|
|
15
18
|
if (!allSelect || mode !== "multiple" || serviceSearch) {
|
|
16
19
|
return null;
|
|
17
20
|
}
|
|
18
21
|
const opLen = options?.length || 0;
|
|
19
22
|
const checked = (value as any[])?.length >= opLen;
|
|
23
|
+
const optionsClassName=useClassName("option");
|
|
20
24
|
const change = () => {
|
|
21
25
|
if (!checked) {
|
|
22
|
-
|
|
26
|
+
const subItemOps = itemOpProvider(options, fieldNames);
|
|
27
|
+
onChange?.(options, subItemOps);
|
|
23
28
|
return;
|
|
24
29
|
}
|
|
25
|
-
onChange?.([]);
|
|
30
|
+
onChange?.([], []);
|
|
26
31
|
};
|
|
27
32
|
return (
|
|
28
33
|
<div
|
|
29
|
-
className={`ant-select-item ant-select-item-option ${
|
|
34
|
+
className={`ant-select-item ant-select-item-option ${optionsClassName}`}
|
|
30
35
|
onClick={change}
|
|
31
36
|
>
|
|
32
37
|
<Space>
|
|
@@ -16,11 +16,6 @@ export const defaultModeConfig: HSelectProps["modeConfig"] = {
|
|
|
16
16
|
},
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export const defaultFieldNames: HSelectProps["fieldNames"] = {
|
|
20
|
-
label: "label",
|
|
21
|
-
value: "value",
|
|
22
|
-
};
|
|
23
|
-
|
|
24
19
|
export const defaultSelectConfig = {
|
|
25
20
|
noMatchItemRender: () => {
|
|
26
21
|
return <NoFindItem label={"选项被删除,请重新选择"} />;
|
|
@@ -4,6 +4,8 @@ import type {
|
|
|
4
4
|
} from "@/components/Select/modal";
|
|
5
5
|
import { useEffect, useState } from "react";
|
|
6
6
|
import type { FilterDataModal } from "@/components/Select/modal";
|
|
7
|
+
import { itemOpProvider } from "../utils";
|
|
8
|
+
import { baseConfig } from "../../config";
|
|
7
9
|
|
|
8
10
|
const single = ({ options, value, noMatchItemRender }: PartialHSelectProps) => {
|
|
9
11
|
const newOptions = options || [];
|
|
@@ -108,10 +110,18 @@ const matchNotFind = (
|
|
|
108
110
|
return tag({ options, value }, oldVale);
|
|
109
111
|
}; //不匹配
|
|
110
112
|
export const useValueChange = (params: PartialHSelectProps) => {
|
|
111
|
-
const {
|
|
112
|
-
|
|
113
|
+
const {
|
|
114
|
+
labelInValue,
|
|
115
|
+
onChange,
|
|
116
|
+
value,
|
|
117
|
+
options,
|
|
118
|
+
mode,
|
|
119
|
+
noMatchItemRender,
|
|
120
|
+
fieldNames = baseConfig.defaultFieldNames,
|
|
121
|
+
} = params;
|
|
113
122
|
const [val, setVal] = useState<any>();
|
|
114
|
-
|
|
123
|
+
|
|
124
|
+
const change = (changeVal, itemOps) => {
|
|
115
125
|
if (!onChange) {
|
|
116
126
|
setVal(changeVal);
|
|
117
127
|
return;
|
|
@@ -125,7 +135,8 @@ export const useValueChange = (params: PartialHSelectProps) => {
|
|
|
125
135
|
return item.value;
|
|
126
136
|
});
|
|
127
137
|
}
|
|
128
|
-
|
|
138
|
+
const subItemOps = itemOpProvider(itemOps, fieldNames);
|
|
139
|
+
onChange(newChangeVal, subItemOps);
|
|
129
140
|
};
|
|
130
141
|
useEffect(() => {
|
|
131
142
|
if (options && value) {
|
|
@@ -1,26 +1,12 @@
|
|
|
1
1
|
import { useState } from "react";
|
|
2
2
|
import type {
|
|
3
|
-
HSelectProps,
|
|
4
3
|
OptionType,
|
|
5
4
|
PartialHSelectProps,
|
|
6
5
|
} from "@/components/Select/modal";
|
|
7
6
|
import { useRequest } from "ahooks";
|
|
8
7
|
|
|
9
|
-
const resultProvider = (
|
|
10
|
-
data?: any[],
|
|
11
|
-
fieldNames?: HSelectProps["fieldNames"]
|
|
12
|
-
) => {
|
|
13
|
-
return data?.map((item) => {
|
|
14
|
-
const { label = "label", value = "value" } = fieldNames || {};
|
|
15
|
-
return {
|
|
16
|
-
label: item[label],
|
|
17
|
-
value: item[value],
|
|
18
|
-
};
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
8
|
export const useOptionReq = ({
|
|
22
9
|
manual,
|
|
23
|
-
fieldNames,
|
|
24
10
|
request,
|
|
25
11
|
options,
|
|
26
12
|
serviceSearch,
|
|
@@ -32,11 +18,9 @@ export const useOptionReq = ({
|
|
|
32
18
|
setData(undefined);
|
|
33
19
|
}
|
|
34
20
|
if (request) {
|
|
35
|
-
|
|
36
|
-
return resultProvider(result, fieldNames);
|
|
21
|
+
return request(params);
|
|
37
22
|
}
|
|
38
|
-
|
|
39
|
-
return Promise.resolve(resultOpt);
|
|
23
|
+
return Promise.resolve(options);
|
|
40
24
|
},
|
|
41
25
|
{
|
|
42
26
|
manual,
|
|
@@ -3,14 +3,10 @@ import React from "react";
|
|
|
3
3
|
import { useFilterOption, useOptionReq } from "./hooks/norHooks";
|
|
4
4
|
import { useValueChange } from "./hooks/changeHooks";
|
|
5
5
|
import type { HSelectProps } from "./modal";
|
|
6
|
-
import {
|
|
7
|
-
defaultFieldNames,
|
|
8
|
-
defaultModeConfig,
|
|
9
|
-
defaultSelectConfig,
|
|
10
|
-
} from "./defaultConfig";
|
|
6
|
+
import { defaultModeConfig, defaultSelectConfig } from "./defaultConfig";
|
|
11
7
|
import DropdownComponent from "./components/DropdownComponent";
|
|
12
8
|
import AllSelect from "./components/AllSelect";
|
|
13
|
-
import {
|
|
9
|
+
import { useChangeOptions } from "../hooks/useChangeOptions";
|
|
14
10
|
const { Option } = Select;
|
|
15
11
|
|
|
16
12
|
export default ({
|
|
@@ -20,7 +16,7 @@ export default ({
|
|
|
20
16
|
modeConfig = defaultModeConfig,
|
|
21
17
|
value,
|
|
22
18
|
onChange,
|
|
23
|
-
fieldNames
|
|
19
|
+
fieldNames,
|
|
24
20
|
request,
|
|
25
21
|
manual,
|
|
26
22
|
optionLabelProp = "label",
|
|
@@ -37,13 +33,20 @@ export default ({
|
|
|
37
33
|
}: HSelectProps) => {
|
|
38
34
|
const { icon, render } = modeConfig?.[mode || ""] || {};
|
|
39
35
|
const selfFilterOption = useFilterOption({ filterOption, serviceSearch });
|
|
40
|
-
const {
|
|
36
|
+
const {
|
|
37
|
+
run,
|
|
38
|
+
loading,
|
|
39
|
+
data: resultData,
|
|
40
|
+
error,
|
|
41
|
+
onSearch,
|
|
42
|
+
} = useOptionReq({
|
|
41
43
|
options,
|
|
42
44
|
manual,
|
|
43
45
|
fieldNames,
|
|
44
46
|
request,
|
|
45
47
|
serviceSearch,
|
|
46
|
-
});
|
|
48
|
+
}); //options
|
|
49
|
+
const data = useChangeOptions({ options: resultData, fieldNames });
|
|
47
50
|
const { val, change } = useValueChange({
|
|
48
51
|
labelInValue,
|
|
49
52
|
onChange,
|
|
@@ -51,6 +54,7 @@ export default ({
|
|
|
51
54
|
options: data,
|
|
52
55
|
mode,
|
|
53
56
|
noMatchItemRender,
|
|
57
|
+
fieldNames,
|
|
54
58
|
});
|
|
55
59
|
return (
|
|
56
60
|
<Select
|
|
@@ -76,7 +80,7 @@ export default ({
|
|
|
76
80
|
onChange={change}
|
|
77
81
|
/>
|
|
78
82
|
{node}
|
|
79
|
-
</DropdownComponent>
|
|
83
|
+
</DropdownComponent> //全选
|
|
80
84
|
);
|
|
81
85
|
}}
|
|
82
86
|
optionLabelProp={optionLabelProp}
|
|
@@ -14,10 +14,9 @@ export interface ModeConfig {
|
|
|
14
14
|
}
|
|
15
15
|
export interface HSelectProps extends Omit<SelectProps, "options"> {
|
|
16
16
|
style?: React.CSSProperties;
|
|
17
|
-
request?:
|
|
17
|
+
request?: PromiseFnResult<any, OptionType[]>;
|
|
18
18
|
manual?: boolean;
|
|
19
19
|
modeConfig?: ModeConfig;
|
|
20
|
-
onChange?: (val: any) => void;
|
|
21
20
|
filterProvider?: (item: any) => string;
|
|
22
21
|
serviceSearch?: boolean;
|
|
23
22
|
options?: OptionType[];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const itemOpMaker = (itemOp, fieldNames) => {
|
|
2
|
+
const { label, value: itemVal, ...oItemOp } = itemOp;
|
|
3
|
+
const { label: labelKey = "", value: valKey = "" } = fieldNames;
|
|
4
|
+
return {
|
|
5
|
+
[labelKey]: label,
|
|
6
|
+
[valKey]: itemVal,
|
|
7
|
+
...oItemOp,
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const itemOpProvider = (itemOps, fieldNames) => {
|
|
12
|
+
if (Array.isArray(itemOps)) {
|
|
13
|
+
return itemOps.map((item) => {
|
|
14
|
+
return itemOpMaker(item, fieldNames);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return itemOpMaker(itemOps, fieldNames);
|
|
18
|
+
};
|