x-ui-design 1.0.31-gamma.1 → 1.0.33
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/.github/workflows/x-ui-design.yml +14 -0
- package/README.md +22 -1
- package/compile.sh +15 -0
- package/dist/{components → esm/types/components}/DatePicker/DatePicker.d.ts +2 -1
- package/dist/{components → esm/types/components}/DatePicker/RangePicker/RangePicker.d.ts +2 -1
- package/dist/{components → esm/types/components}/Dropdown/Dropdown.d.ts +2 -1
- package/dist/{components → esm/types/components}/Empty/Empty.d.ts +2 -1
- package/dist/{components → esm/types/components}/Form/Item/Item.d.ts +2 -1
- package/dist/esm/types/components/Icons/Icons.d.ts +19 -0
- package/dist/{components → esm/types/components}/Input/Input.d.ts +2 -1
- package/dist/{components → esm/types/components}/Input/Textarea/Textarea.d.ts +2 -1
- package/dist/{components → esm/types/components}/Popover/Popover.d.ts +2 -1
- package/dist/{components → esm/types/components}/Radio/Button/Button.d.ts +2 -1
- package/dist/{components → esm/types/components}/Radio/Group/Group.d.ts +2 -1
- package/dist/{components → esm/types/components}/Radio/Radio.d.ts +2 -1
- package/dist/{components → esm/types/components}/Result/Result.d.ts +2 -1
- package/dist/{components → esm/types/components}/Switch/Switch.d.ts +2 -1
- package/dist/{components → esm/types/components}/Upload/Upload.d.ts +2 -1
- package/dist/esm/types/index.d.ts +50 -0
- package/dist/{types → esm/types/types}/input.d.ts +2 -2
- package/dist/index.d.ts +144 -1
- package/dist/index.esm.js +4233 -3792
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +4338 -3897
- package/dist/index.js.map +1 -1
- package/eslint.config.mjs +16 -0
- package/lib/components/Button/Button.tsx +136 -0
- package/lib/components/Button/index.ts +1 -0
- package/lib/components/Button/style.css +197 -0
- package/lib/components/Checkbox/Checkbox.tsx +131 -0
- package/lib/components/Checkbox/index.ts +1 -0
- package/lib/components/Checkbox/style.css +95 -0
- package/lib/components/ConditionalWrapper/index.tsx +12 -0
- package/lib/components/DatePicker/DatePicker.tsx +526 -0
- package/lib/components/DatePicker/RangePicker/RangePicker.tsx +500 -0
- package/lib/components/DatePicker/RangePicker/index.ts +1 -0
- package/lib/components/DatePicker/RangePicker/style.css +434 -0
- package/lib/components/DatePicker/TimePicker/TimePicker.tsx +497 -0
- package/lib/components/DatePicker/TimePicker/index.ts +1 -0
- package/lib/components/DatePicker/TimePicker/style.css +197 -0
- package/lib/components/DatePicker/index.ts +1 -0
- package/lib/components/DatePicker/style.css +318 -0
- package/lib/components/Dropdown/Dropdown.tsx +234 -0
- package/lib/components/Dropdown/index.ts +1 -0
- package/lib/components/Dropdown/style.css +124 -0
- package/lib/components/Empty/Empty.tsx +45 -0
- package/lib/components/Empty/index.ts +1 -0
- package/lib/components/Empty/style.css +13 -0
- package/lib/components/Form/Form.tsx +130 -0
- package/lib/components/Form/Item/Item.tsx +294 -0
- package/lib/components/Form/Item/index.ts +1 -0
- package/lib/components/Form/Item/style.css +61 -0
- package/lib/components/Form/index.ts +1 -0
- package/lib/components/Icons/Icons.tsx +433 -0
- package/lib/components/Icons/index.ts +15 -0
- package/lib/components/Input/Input.tsx +218 -0
- package/lib/components/Input/Textarea/Textarea.tsx +110 -0
- package/lib/components/Input/Textarea/index.ts +1 -0
- package/lib/components/Input/Textarea/style.css +104 -0
- package/lib/components/Input/index.ts +1 -0
- package/lib/components/Input/style.css +137 -0
- package/lib/components/Menu/Item/Item.tsx +65 -0
- package/lib/components/Menu/Menu.tsx +261 -0
- package/lib/components/Menu/SubMenu/SubMenu.tsx +68 -0
- package/lib/components/Menu/index.css +145 -0
- package/lib/components/Menu/index.ts +1 -0
- package/lib/components/Popover/Popover.tsx +135 -0
- package/lib/components/Popover/index.ts +1 -0
- package/lib/components/Popover/style.css +82 -0
- package/lib/components/Radio/Button/Button.tsx +42 -0
- package/lib/components/Radio/Button/index.ts +1 -0
- package/lib/components/Radio/Button/style.css +43 -0
- package/lib/components/Radio/Group/Group.tsx +105 -0
- package/lib/components/Radio/Group/index.ts +1 -0
- package/lib/components/Radio/Group/style.css +53 -0
- package/lib/components/Radio/Radio.tsx +83 -0
- package/lib/components/Radio/index.ts +1 -0
- package/lib/components/Radio/style.css +73 -0
- package/lib/components/Result/Result.tsx +39 -0
- package/lib/components/Result/index.ts +1 -0
- package/lib/components/Result/style.css +173 -0
- package/lib/components/Select/Option/Option.tsx +49 -0
- package/lib/components/Select/Option/index.ts +1 -0
- package/lib/components/Select/Option/style.css +50 -0
- package/lib/components/Select/Select.tsx +935 -0
- package/lib/components/Select/Tag/Tag.tsx +43 -0
- package/lib/components/Select/Tag/index.ts +1 -0
- package/lib/components/Select/Tag/style.css +87 -0
- package/lib/components/Select/index.ts +1 -0
- package/lib/components/Select/style.css +186 -0
- package/lib/components/Skeleton/Avatar/Avatar.tsx +61 -0
- package/lib/components/Skeleton/Avatar/index.ts +1 -0
- package/lib/components/Skeleton/Avatar/style.css +27 -0
- package/lib/components/Skeleton/Button/Button.tsx +44 -0
- package/lib/components/Skeleton/Button/index.ts +1 -0
- package/lib/components/Skeleton/Button/style.css +50 -0
- package/lib/components/Skeleton/Image/Image.tsx +45 -0
- package/lib/components/Skeleton/Image/index.ts +1 -0
- package/lib/components/Skeleton/Image/style.css +23 -0
- package/lib/components/Skeleton/Input/Input.tsx +42 -0
- package/lib/components/Skeleton/Input/index.ts +1 -0
- package/lib/components/Skeleton/Input/style.css +56 -0
- package/lib/components/Skeleton/Skeleton.tsx +97 -0
- package/lib/components/Skeleton/index.ts +1 -0
- package/lib/components/Skeleton/style.css +84 -0
- package/lib/components/Switch/Switch.tsx +68 -0
- package/lib/components/Switch/index.css +50 -0
- package/lib/components/Switch/index.ts +1 -0
- package/lib/components/Upload/Upload.tsx +291 -0
- package/lib/components/Upload/index.ts +1 -0
- package/lib/components/Upload/style.css +151 -0
- package/lib/global.d.ts +1 -0
- package/lib/helpers/flatten.ts +26 -0
- package/lib/helpers/index.ts +52 -0
- package/lib/helpers/mask.ts +52 -0
- package/lib/hooks/useForm.ts +548 -0
- package/lib/hooks/usePosition.ts +206 -0
- package/lib/hooks/useWatch.ts +41 -0
- package/lib/hooks/useWatchError.ts +20 -0
- package/lib/index.ts +184 -0
- package/lib/styles/global.css +57 -0
- package/lib/types/button.ts +83 -0
- package/lib/types/checkbox.ts +32 -0
- package/lib/types/datepicker.ts +165 -0
- package/lib/types/dropdown.ts +41 -0
- package/lib/types/empty.ts +8 -0
- package/lib/types/form.ts +179 -0
- package/lib/types/index.ts +38 -0
- package/lib/types/input.ts +72 -0
- package/lib/types/menu.ts +55 -0
- package/lib/types/popover.ts +16 -0
- package/lib/types/radio.ts +69 -0
- package/lib/types/result.ts +22 -0
- package/lib/types/select.ts +126 -0
- package/lib/types/skeleton.ts +62 -0
- package/lib/types/switch.ts +22 -0
- package/lib/types/upload.ts +67 -0
- package/lib/utils/index.ts +37 -0
- package/lib/utils/lazy.ts +17 -0
- package/next.config.ts +7 -0
- package/package.json +18 -20
- package/rollup.config.js +71 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/globals.css +48 -0
- package/src/app/layout.d.ts +5 -0
- package/src/app/layout.tsx +16 -0
- package/src/app/page.d.ts +1 -0
- package/src/app/page.tsx +22 -0
- package/tsconfig.json +46 -0
- package/dist/components/Icons/Icons.d.ts +0 -18
- /package/dist/{components → esm/types/components}/Button/Button.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Button/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Checkbox/Checkbox.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Checkbox/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/ConditionalWrapper/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/DatePicker/RangePicker/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/DatePicker/TimePicker/TimePicker.d.ts +0 -0
- /package/dist/{components → esm/types/components}/DatePicker/TimePicker/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/DatePicker/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Dropdown/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Empty/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Form/Form.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Form/Item/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Form/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Icons/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Input/Textarea/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Input/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Menu/Item/Item.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Menu/Menu.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Menu/SubMenu/SubMenu.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Menu/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Popover/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Radio/Button/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Radio/Group/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Radio/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Result/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Select/Option/Option.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Select/Option/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Select/Select.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Select/Tag/Tag.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Select/Tag/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Select/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Avatar/Avatar.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Avatar/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Button/Button.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Button/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Image/Image.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Image/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Input/Input.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Input/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/Skeleton.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Skeleton/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Switch/index.d.ts +0 -0
- /package/dist/{components → esm/types/components}/Upload/index.d.ts +0 -0
- /package/dist/{helpers → esm/types/helpers}/flatten.d.ts +0 -0
- /package/dist/{helpers → esm/types/helpers}/index.d.ts +0 -0
- /package/dist/{helpers → esm/types/helpers}/mask.d.ts +0 -0
- /package/dist/{hooks → esm/types/hooks}/useForm.d.ts +0 -0
- /package/dist/{hooks → esm/types/hooks}/usePosition.d.ts +0 -0
- /package/dist/{hooks → esm/types/hooks}/useWatch.d.ts +0 -0
- /package/dist/{hooks → esm/types/hooks}/useWatchError.d.ts +0 -0
- /package/dist/{types → esm/types/types}/button.d.ts +0 -0
- /package/dist/{types → esm/types/types}/checkbox.d.ts +0 -0
- /package/dist/{types → esm/types/types}/datepicker.d.ts +0 -0
- /package/dist/{types → esm/types/types}/dropdown.d.ts +0 -0
- /package/dist/{types → esm/types/types}/empty.d.ts +0 -0
- /package/dist/{types → esm/types/types}/form.d.ts +0 -0
- /package/dist/{types → esm/types/types}/index.d.ts +0 -0
- /package/dist/{types → esm/types/types}/menu.d.ts +0 -0
- /package/dist/{types → esm/types/types}/popover.d.ts +0 -0
- /package/dist/{types → esm/types/types}/radio.d.ts +0 -0
- /package/dist/{types → esm/types/types}/result.d.ts +0 -0
- /package/dist/{types → esm/types/types}/select.d.ts +0 -0
- /package/dist/{types → esm/types/types}/skeleton.d.ts +0 -0
- /package/dist/{types → esm/types/types}/switch.d.ts +0 -0
- /package/dist/{types → esm/types/types}/upload.d.ts +0 -0
- /package/dist/{utils → esm/types/utils}/index.d.ts +0 -0
- /package/dist/{utils → esm/types/utils}/lazy.d.ts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Empty } from '../Empty/Empty'
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import React, {
|
|
4
|
+
Children,
|
|
5
|
+
createContext,
|
|
6
|
+
FC,
|
|
7
|
+
isValidElement,
|
|
8
|
+
ReactElement,
|
|
9
|
+
ReactNode,
|
|
10
|
+
SyntheticEvent,
|
|
11
|
+
useCallback,
|
|
12
|
+
useEffect,
|
|
13
|
+
useMemo,
|
|
14
|
+
useRef
|
|
15
|
+
} from 'react';
|
|
16
|
+
import { useForm } from '../../hooks/useForm';
|
|
17
|
+
import { FormInstance, FormItemProps, FormLayoutTypes, FormProps } from '../../types/form';
|
|
18
|
+
import { prefixClsForm, prefixClsFormV3 } from '../../utils';
|
|
19
|
+
import FormItem from './Item/Item';
|
|
20
|
+
import { flattenChildren } from '../../helpers/flatten';
|
|
21
|
+
import { SizeType } from '../../types';
|
|
22
|
+
|
|
23
|
+
export const FormContext = createContext<FormInstance | null>(null);
|
|
24
|
+
|
|
25
|
+
const Form: FC<FormProps> & { Item: FC<FormItemProps> } = ({
|
|
26
|
+
children,
|
|
27
|
+
form,
|
|
28
|
+
style = {},
|
|
29
|
+
prefixCls = prefixClsForm,
|
|
30
|
+
prefixClsV3 = prefixClsFormV3,
|
|
31
|
+
className = '',
|
|
32
|
+
onFinish,
|
|
33
|
+
onFinishFailed,
|
|
34
|
+
initialValues = {},
|
|
35
|
+
onValuesChange,
|
|
36
|
+
onFieldsChange,
|
|
37
|
+
layout = 'horizontal',
|
|
38
|
+
scrollToFirstError = false,
|
|
39
|
+
...rest
|
|
40
|
+
}) => {
|
|
41
|
+
const internalForm = useForm({ initialValues, onFieldsChange, onValuesChange, onFinishFailed });
|
|
42
|
+
const formRef = useRef<HTMLFormElement>(null);
|
|
43
|
+
|
|
44
|
+
const formInstance = useMemo(() => {
|
|
45
|
+
const _form = form || internalForm;
|
|
46
|
+
|
|
47
|
+
if (_form && Object.keys(initialValues).length) {
|
|
48
|
+
Object.keys(initialValues).forEach((name) => {
|
|
49
|
+
if (_form.getFieldValue(name) === undefined) {
|
|
50
|
+
_form.setFieldValue(name, initialValues[name]);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return _form;
|
|
56
|
+
}, [form, internalForm, initialValues]);
|
|
57
|
+
const childrenList = useMemo(() => flattenChildren(children), [children])
|
|
58
|
+
|
|
59
|
+
const handleSubmit = useCallback(async (e: SyntheticEvent) => {
|
|
60
|
+
e.preventDefault();
|
|
61
|
+
e.stopPropagation();
|
|
62
|
+
|
|
63
|
+
await formInstance.submit();
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (onFinish) { formInstance.setOnFinish?.(onFinish) }
|
|
68
|
+
if (onFieldsChange) { formInstance.setOnFieldsChange?.(onFieldsChange) }
|
|
69
|
+
if (onValuesChange) { formInstance.setOnValuesChange?.(onValuesChange) }
|
|
70
|
+
if (onFinishFailed) { formInstance.setOnFinishFailed?.(onFinishFailed) }
|
|
71
|
+
|
|
72
|
+
if (scrollToFirstError) { formInstance.setScrollToFirstError(scrollToFirstError) }
|
|
73
|
+
}, [formInstance, onFieldsChange, onValuesChange, onFinishFailed, onFinish, scrollToFirstError]);
|
|
74
|
+
|
|
75
|
+
const injectPropsIntoFinalLeaf = useCallback((child: ReactNode): ReactNode => {
|
|
76
|
+
if (!isValidElement(child)) {
|
|
77
|
+
return child;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const childProps = child.props as ReactElement & {
|
|
81
|
+
children: ReactElement[],
|
|
82
|
+
__injected: boolean;
|
|
83
|
+
size?: SizeType;
|
|
84
|
+
layout?: FormLayoutTypes;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const isWrapper =
|
|
88
|
+
typeof child.type === 'string' &&
|
|
89
|
+
!('dangerouslySetInnerHTML' in childProps) &&
|
|
90
|
+
['div', 'span', 'label'].includes(child.type);
|
|
91
|
+
|
|
92
|
+
if (isWrapper) {
|
|
93
|
+
return (
|
|
94
|
+
<child.type {...childProps}>
|
|
95
|
+
{Children.map(flattenChildren(childProps.children), injectPropsIntoFinalLeaf)}
|
|
96
|
+
</child.type>
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (childProps?.__injected) {
|
|
101
|
+
return child;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return <child.type
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
106
|
+
// @ts-expect-error
|
|
107
|
+
{...child.props}
|
|
108
|
+
child={child}
|
|
109
|
+
size={childProps.size || rest.size}
|
|
110
|
+
layout={childProps.layout || layout}
|
|
111
|
+
/>
|
|
112
|
+
}, [rest.size, layout]);
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
<FormContext.Provider value={formInstance}>
|
|
116
|
+
<form
|
|
117
|
+
style={style}
|
|
118
|
+
ref={formRef}
|
|
119
|
+
onSubmit={handleSubmit}
|
|
120
|
+
className={`${prefixCls} ${prefixClsV3} ${className}`}
|
|
121
|
+
>
|
|
122
|
+
{Children.map(childrenList, injectPropsIntoFinalLeaf)}
|
|
123
|
+
</form>
|
|
124
|
+
</FormContext.Provider>
|
|
125
|
+
);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
Form.Item = FormItem;
|
|
129
|
+
|
|
130
|
+
export default Form;
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import React, {
|
|
4
|
+
Children,
|
|
5
|
+
isValidElement,
|
|
6
|
+
ReactElement,
|
|
7
|
+
useCallback,
|
|
8
|
+
useContext,
|
|
9
|
+
useEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
useRef,
|
|
12
|
+
useState
|
|
13
|
+
} from 'react';
|
|
14
|
+
import { clsx } from '../../../helpers';
|
|
15
|
+
import { RuleType, SyntheticBaseEvent } from '../../../types';
|
|
16
|
+
import { flattenChildren } from '../../../helpers/flatten';
|
|
17
|
+
import {
|
|
18
|
+
FieldInstancesRef,
|
|
19
|
+
FormItemChildComponentProps,
|
|
20
|
+
FormItemProps
|
|
21
|
+
} from '../../../types/form';
|
|
22
|
+
import { OptionProps } from '../../../types/select';
|
|
23
|
+
import { prefixClsFormItem, prefixClsFormItemV3 } from '../../../utils';
|
|
24
|
+
import { FormContext } from '../Form';
|
|
25
|
+
import { useWatchError } from '../../../hooks/useWatchError';
|
|
26
|
+
import './style.css';
|
|
27
|
+
|
|
28
|
+
const FormItem = ({
|
|
29
|
+
prefixCls = prefixClsFormItem,
|
|
30
|
+
prefixClsV3 = prefixClsFormItemV3,
|
|
31
|
+
name,
|
|
32
|
+
label,
|
|
33
|
+
rules = [],
|
|
34
|
+
children,
|
|
35
|
+
className = '',
|
|
36
|
+
layout = 'vertical',
|
|
37
|
+
style = {},
|
|
38
|
+
dependencies = [],
|
|
39
|
+
initialValue,
|
|
40
|
+
feedbackIcons,
|
|
41
|
+
extra,
|
|
42
|
+
hideLabel = false,
|
|
43
|
+
removeErrorMessageHeight = false,
|
|
44
|
+
...props
|
|
45
|
+
}: FormItemProps) => {
|
|
46
|
+
const formContext = useContext(FormContext);
|
|
47
|
+
|
|
48
|
+
const errorRef = useRef<HTMLSpanElement>(null);
|
|
49
|
+
const fieldRef = useRef<FieldInstancesRef | null>(null);
|
|
50
|
+
|
|
51
|
+
if (!formContext) {
|
|
52
|
+
throw new Error('FormItem must be used within a Form');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const errors = useWatchError(formContext, name)?.[0];
|
|
56
|
+
|
|
57
|
+
const {
|
|
58
|
+
isReseting,
|
|
59
|
+
registerField,
|
|
60
|
+
getFieldValue,
|
|
61
|
+
setFieldValue,
|
|
62
|
+
getFieldInstance,
|
|
63
|
+
setFieldInstance,
|
|
64
|
+
subscribeToFields,
|
|
65
|
+
validateFields
|
|
66
|
+
} = formContext;
|
|
67
|
+
|
|
68
|
+
const childrenList = useMemo(() => flattenChildren(children), [children]);
|
|
69
|
+
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
if (name && !getFieldInstance(name)) {
|
|
72
|
+
registerField(name, rules);
|
|
73
|
+
}
|
|
74
|
+
}, [name, rules]);
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
setFieldInstance(name, fieldRef.current);
|
|
78
|
+
}, [name, fieldRef.current])
|
|
79
|
+
|
|
80
|
+
useEffect(() => () => registerField(name, undefined, true), [name])
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
if (initialValue && getFieldValue(name) === undefined) {
|
|
84
|
+
setFieldValue(name, initialValue);
|
|
85
|
+
}
|
|
86
|
+
}, [name]);
|
|
87
|
+
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
if (name && dependencies.length > 0) {
|
|
90
|
+
const unsubscribe = subscribeToFields(dependencies, () => {
|
|
91
|
+
validateFields([name]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return () => {
|
|
95
|
+
unsubscribe();
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}, [dependencies, name]);
|
|
99
|
+
|
|
100
|
+
const isRequired = useMemo(
|
|
101
|
+
() => rules.some((rule: RuleType) => rule.required),
|
|
102
|
+
[rules]
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<div
|
|
107
|
+
style={style}
|
|
108
|
+
data-instance={name}
|
|
109
|
+
className={clsx([
|
|
110
|
+
`${prefixCls} ${prefixClsV3}`,
|
|
111
|
+
{
|
|
112
|
+
[layout]: layout,
|
|
113
|
+
[className]: className,
|
|
114
|
+
noStyle: props.noStyle
|
|
115
|
+
}
|
|
116
|
+
])}
|
|
117
|
+
>
|
|
118
|
+
{!props.noStyle && (label || name) && !hideLabel && (
|
|
119
|
+
<label className={`${prefixCls}-label ${prefixClsV3}-label`} htmlFor={name}>
|
|
120
|
+
{label || name}
|
|
121
|
+
{isRequired && <span className={`${prefixCls}-required ${prefixClsV3}-required`}>*</span>}
|
|
122
|
+
{/* @Todo need to add Tooltip like Ant design */}
|
|
123
|
+
</label>
|
|
124
|
+
)}
|
|
125
|
+
|
|
126
|
+
{Children.map(childrenList, (child, key) => {
|
|
127
|
+
if (isValidElement(child)) {
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
129
|
+
// @ts-expect-error
|
|
130
|
+
const { onChange, value, ...childProps } = child.props;
|
|
131
|
+
const fieldValue = value ?? getFieldValue(name) ?? initialValue;
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<div className={`${prefixCls}-content`}>
|
|
135
|
+
<FormItemChildComponent
|
|
136
|
+
{...props}
|
|
137
|
+
key={`${key}_${name}_${isReseting}`}
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
139
|
+
// @ts-expect-error
|
|
140
|
+
ref={fieldRef}
|
|
141
|
+
name={name}
|
|
142
|
+
child={child}
|
|
143
|
+
value={value}
|
|
144
|
+
error={!!errors}
|
|
145
|
+
fieldValue={fieldValue}
|
|
146
|
+
setFieldValue={setFieldValue}
|
|
147
|
+
feedbackIcons={feedbackIcons}
|
|
148
|
+
onChange={onChange}
|
|
149
|
+
noStyle={props.noStyle}
|
|
150
|
+
normalize={props.normalize}
|
|
151
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
152
|
+
// @ts-expect-error
|
|
153
|
+
size={childProps.size || props.size}
|
|
154
|
+
/>
|
|
155
|
+
|
|
156
|
+
{extra
|
|
157
|
+
? <div className={`${prefixCls}-extra ${prefixClsV3}-extra`}>
|
|
158
|
+
{extra || ''}
|
|
159
|
+
</div>
|
|
160
|
+
: null}
|
|
161
|
+
|
|
162
|
+
{!props.noStyle && (
|
|
163
|
+
<span
|
|
164
|
+
ref={errorRef}
|
|
165
|
+
className={clsx([
|
|
166
|
+
`${prefixCls}-error ${prefixClsV3}-error`,
|
|
167
|
+
{ [`${prefixCls}-has-error ${prefixClsV3}-has-error`]: errors?.length }
|
|
168
|
+
])}
|
|
169
|
+
style={{
|
|
170
|
+
...removeErrorMessageHeight ? { minHeight: 0 } : {},
|
|
171
|
+
...extra ? { marginBottom: 0 } : {}
|
|
172
|
+
}}
|
|
173
|
+
>
|
|
174
|
+
{errors || ''}
|
|
175
|
+
</span>
|
|
176
|
+
)}
|
|
177
|
+
</div>
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return child;
|
|
182
|
+
})}
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const FormItemChildComponent = ({
|
|
188
|
+
child,
|
|
189
|
+
name,
|
|
190
|
+
error,
|
|
191
|
+
fieldValue,
|
|
192
|
+
setFieldValue,
|
|
193
|
+
onChange,
|
|
194
|
+
normalize,
|
|
195
|
+
noStyle,
|
|
196
|
+
feedbackIcons,
|
|
197
|
+
ref,
|
|
198
|
+
...props
|
|
199
|
+
}: FormItemChildComponentProps) => {
|
|
200
|
+
const formContext = useContext(FormContext);
|
|
201
|
+
|
|
202
|
+
const [wasNormalize, setWasNormalize] = useState(false);
|
|
203
|
+
|
|
204
|
+
const { getFieldsValue } = formContext || {};
|
|
205
|
+
|
|
206
|
+
const handleChange = (e: SyntheticBaseEvent) => {
|
|
207
|
+
let rawValue: RuleType | SyntheticBaseEvent = e?.target
|
|
208
|
+
? e.target.value
|
|
209
|
+
: e;
|
|
210
|
+
|
|
211
|
+
if (normalize) {
|
|
212
|
+
const prevValue = fieldValue ?? props.value;
|
|
213
|
+
const allValues = getFieldsValue?.();
|
|
214
|
+
|
|
215
|
+
rawValue = normalize(rawValue, prevValue, allValues);
|
|
216
|
+
|
|
217
|
+
if (rawValue === prevValue) {
|
|
218
|
+
e.target.value = rawValue;
|
|
219
|
+
setWasNormalize(prev => !prev);
|
|
220
|
+
|
|
221
|
+
const timeout = setTimeout(() => {
|
|
222
|
+
(
|
|
223
|
+
document.querySelector(`[name='${name}']`) as HTMLInputElement
|
|
224
|
+
)?.focus();
|
|
225
|
+
|
|
226
|
+
clearTimeout(timeout);
|
|
227
|
+
}, 0);
|
|
228
|
+
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
setFieldValue(name, rawValue, undefined, undefined, true);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const injectPropsIntoFinalLeaf = (child: ReactElement): ReactElement => {
|
|
237
|
+
if (!isValidElement(child)) {
|
|
238
|
+
return child;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const childProps = child.props as ReactElement & {
|
|
242
|
+
children: ReactElement[],
|
|
243
|
+
__injected: boolean,
|
|
244
|
+
onChange?: (e: SyntheticBaseEvent, option?: OptionProps) => void
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const isWrapper =
|
|
248
|
+
typeof child.type === 'string' &&
|
|
249
|
+
!('dangerouslySetInnerHTML' in childProps) &&
|
|
250
|
+
['div', 'span', 'label'].includes(child.type);
|
|
251
|
+
|
|
252
|
+
if (isWrapper) {
|
|
253
|
+
return (
|
|
254
|
+
<child.type {...childProps}>
|
|
255
|
+
{Children.map(flattenChildren(childProps.children), injectPropsIntoFinalLeaf)}
|
|
256
|
+
</child.type>
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const _onChange = useCallback((e: SyntheticBaseEvent, option?: OptionProps) => {
|
|
261
|
+
handleChange(e);
|
|
262
|
+
|
|
263
|
+
childProps?.onChange?.(e, option)
|
|
264
|
+
}, [handleChange, childProps?.onChange])
|
|
265
|
+
|
|
266
|
+
if (childProps?.__injected) {
|
|
267
|
+
return child;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return <child.type
|
|
271
|
+
{...props}
|
|
272
|
+
ref={ref}
|
|
273
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
274
|
+
// @ts-expect-error
|
|
275
|
+
{...child.props}
|
|
276
|
+
name={name}
|
|
277
|
+
child={child}
|
|
278
|
+
onChange={_onChange}
|
|
279
|
+
key={`${name}_${wasNormalize}`}
|
|
280
|
+
value={fieldValue ?? props.value}
|
|
281
|
+
{...('dangerouslySetInnerHTML' in childProps ? {} : {
|
|
282
|
+
__injected: true,
|
|
283
|
+
...(error ? { error } : {}),
|
|
284
|
+
})
|
|
285
|
+
}
|
|
286
|
+
/>
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
return injectPropsIntoFinalLeaf(child)
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
FormItem.displayName = 'FormItem';
|
|
293
|
+
|
|
294
|
+
export default FormItem;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Item } from './Item'
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
.xUi-form-item {
|
|
2
|
+
display: flex;
|
|
3
|
+
position: relative;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.xUi-form-item.noStyle {
|
|
7
|
+
display: inline-flex;
|
|
8
|
+
margin-bottom: 0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.xUi-form-item-label {
|
|
12
|
+
font-weight: 500;
|
|
13
|
+
margin-bottom: 4px;
|
|
14
|
+
display: flex;
|
|
15
|
+
line-height: 20px;
|
|
16
|
+
align-items: center;
|
|
17
|
+
font-size: var(--xui-font-size-md);
|
|
18
|
+
color: var(--xui-text-color);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.xUi-form-item-error {
|
|
22
|
+
right: 0;
|
|
23
|
+
margin-top: 4px;
|
|
24
|
+
margin-bottom: 8px;
|
|
25
|
+
display: block;
|
|
26
|
+
min-height: 16px;
|
|
27
|
+
font-size: var(--xui-font-size-xs);
|
|
28
|
+
position: relative;
|
|
29
|
+
color: var(--xui-error-color);
|
|
30
|
+
user-select: none;
|
|
31
|
+
line-height: 16px;
|
|
32
|
+
text-align: end;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.xUi-form-item-required {
|
|
36
|
+
line-height: 1;
|
|
37
|
+
font-size: var(--xui-font-size-md);
|
|
38
|
+
margin-left: 4px;
|
|
39
|
+
margin-right: 4px;
|
|
40
|
+
display: inline-block;
|
|
41
|
+
color: var(--xui-error-color);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.xUi-form-item.horizontal {
|
|
45
|
+
gap: 4px;
|
|
46
|
+
flex-direction: row;
|
|
47
|
+
align-items: center;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.xUi-form-item.vertical {
|
|
51
|
+
flex-direction: column;
|
|
52
|
+
align-self: flex-start;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.xUi-form-item .xUi-input-container {
|
|
56
|
+
width: -webkit-fill-available;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.xUi-form-item-content {
|
|
60
|
+
width: -webkit-fill-available;
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Form } from './Form'
|