foxit-component 0.0.1-alpha.3 → 0.0.1-alpha.30
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/es/Alert/Alert.d.ts +12 -0
- package/es/Alert/Alert.js +24 -0
- package/es/Button/Button.d.ts +2 -1
- package/es/Button/Button.js +4 -4
- package/es/Checkbox/Checkbox.d.ts +10 -2
- package/es/Checkbox/Checkbox.js +35 -5
- package/es/Drawer/Drawer.d.ts +10 -0
- package/es/Drawer/Drawer.js +14 -0
- package/es/Empty/Empty.js +0 -1
- package/es/Form/Form.d.ts +2 -1
- package/es/Form/Form.js +3 -3
- package/es/Form/Form.types.d.ts +9 -0
- package/es/Form/FormItem.js +1 -2
- package/es/Form/FormProvider.js +148 -115
- package/es/Form/useFormWatch.d.ts +3 -0
- package/es/Form/useFormWatch.js +37 -0
- package/es/Input/Input.d.ts +1 -0
- package/es/Input/Input.js +6 -5
- package/es/Menu/Menu.d.ts +2 -1
- package/es/Menu/Menu.js +124 -11
- package/es/Modal/Modal.d.ts +2 -0
- package/es/Modal/Modal.js +88 -17
- package/es/Modal/ModalTemp.js +4 -1
- package/es/Pagination/Pagination.d.ts +2 -0
- package/es/Pagination/Pagination.js +26 -13
- package/es/Quantity/Quantity.d.ts +10 -0
- package/es/Quantity/Quantity.js +45 -0
- package/es/Radio/Radio.js +0 -1
- package/es/Select/Select.d.ts +10 -0
- package/es/Select/Select.js +15 -10
- package/es/Spin/Spin.d.ts +10 -0
- package/es/Spin/Spin.js +23 -0
- package/es/Switch/Switch.d.ts +9 -0
- package/es/Switch/Switch.js +20 -0
- package/es/Table/Table.d.ts +4 -1
- package/es/Table/Table.js +22 -8
- package/es/Tabs/Tabs.js +0 -1
- package/es/Tag/Tag.d.ts +1 -1
- package/es/Tag/Tag.js +1 -2
- package/es/Toast/Toast.js +0 -1
- package/es/Toast/ToastContainer.js +0 -1
- package/es/Tooltip/Tooltip.js +38 -18
- package/es/constants/icons.d.ts +16 -0
- package/es/constants/icons.js +17 -1
- package/es/index.css +1 -0
- package/es/index.d.ts +9 -6
- package/es/index.js +7 -1
- package/es/node_modules/tslib/tslib.es6.js +39 -1
- package/package.json +18 -16
- package/es/Button/button.css.js +0 -6
- package/es/Checkbox/checkbox.css.js +0 -6
- package/es/Empty/empty.css.js +0 -6
- package/es/Form/form.css.js +0 -6
- package/es/Input/input.css.js +0 -6
- package/es/Menu/menu.css.js +0 -6
- package/es/Modal/modal.css.js +0 -6
- package/es/Pagination/pagination.css.js +0 -6
- package/es/Radio/radio.css.js +0 -6
- package/es/Table/table.css.js +0 -6
- package/es/Tabs/tabs.css.js +0 -6
- package/es/Tag/tag.css.js +0 -6
- package/es/Toast/toast.css.js +0 -6
- package/es/Tooltip/tooltip.css.js +0 -6
- package/es/node_modules/style-inject/dist/style-inject.es.js +0 -28
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @name Alert
|
|
3
|
+
*/
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import './alert.css';
|
|
6
|
+
interface AlertProps {
|
|
7
|
+
message: string | React.ReactNode;
|
|
8
|
+
showIcon?: boolean;
|
|
9
|
+
type?: 'success' | 'warning' | 'error' | 'gradient';
|
|
10
|
+
}
|
|
11
|
+
declare const Alert: React.FC<AlertProps>;
|
|
12
|
+
export { Alert };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { __assign } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { Icon } from '../Icon/index.js';
|
|
4
|
+
|
|
5
|
+
var Alert = function (_a) {
|
|
6
|
+
var message = _a.message, _b = _a.showIcon, showIcon = _b === void 0 ? true : _b, _c = _a.type, type = _c === void 0 ? 'success' : _c;
|
|
7
|
+
var getIconName = function (type) {
|
|
8
|
+
switch (type) {
|
|
9
|
+
case 'success':
|
|
10
|
+
return 'SuccessColoursOutlined';
|
|
11
|
+
case 'error':
|
|
12
|
+
return 'ErrorColoursOutlined';
|
|
13
|
+
case 'warning':
|
|
14
|
+
return 'WarningColoursOutlined';
|
|
15
|
+
case 'loading':
|
|
16
|
+
return 'LoadingOutlined';
|
|
17
|
+
default:
|
|
18
|
+
return 'WarningColoursOutlined';
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
return (jsxs("div", __assign({ className: "foxit-alert foxit-alert-".concat(type) }, { children: [showIcon && (jsx("div", __assign({ className: "foxit-alert-icon-container" }, { children: jsx(Icon, { name: getIconName(type), className: "foxit-alert-icon" }) }))), jsx("div", __assign({ className: "foxit-alert-message" }, { children: message }))] })));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { Alert };
|
package/es/Button/Button.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export interface ButtonProps {
|
|
|
8
8
|
className?: string;
|
|
9
9
|
style?: React.CSSProperties;
|
|
10
10
|
[key: string]: unknown;
|
|
11
|
+
loading?: boolean;
|
|
11
12
|
}
|
|
12
|
-
export declare const Button: ({ primary, size, children, ...props }: ButtonProps) => import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
export declare const Button: ({ primary, size, children, className, style, loading, ...props }: ButtonProps) => import("react/jsx-runtime").JSX.Element;
|
package/es/Button/Button.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { __rest, __assign } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
-
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
3
|
import classNames from 'classnames';
|
|
4
|
-
import '
|
|
4
|
+
import { Icon } from '../Icon/index.js';
|
|
5
5
|
|
|
6
6
|
var Button = function (_a) {
|
|
7
|
-
var _b = _a.primary, primary = _b === void 0 ? false : _b, _c = _a.size, size = _c === void 0 ? 'large' : _c, _d = _a.children, children = _d === void 0 ? '' : _d, props = __rest(_a, ["primary", "size", "children"]);
|
|
7
|
+
var _b = _a.primary, primary = _b === void 0 ? false : _b, _c = _a.size, size = _c === void 0 ? 'large' : _c, _d = _a.children, children = _d === void 0 ? '' : _d, className = _a.className, style = _a.style, loading = _a.loading, props = __rest(_a, ["primary", "size", "children", "className", "style", "loading"]);
|
|
8
8
|
var mode = primary ? 'foxit-button-primary' : 'foxit-button-secondary';
|
|
9
|
-
return (
|
|
9
|
+
return (jsxs("button", __assign({ type: "button", className: classNames('foxit-button', "foxit-button-".concat(size), mode, className, loading && 'foxit-button-prevent-click'), style: style }, props, { children: [loading && jsx(Icon, { name: "LoadingOutlined", className: "foxit-button-loading" }), children] })));
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
export { Button };
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { FC } from 'react';
|
|
2
2
|
import './checkbox.css';
|
|
3
3
|
interface CheckboxProps {
|
|
4
4
|
onChange?: (checked: boolean) => void;
|
|
5
5
|
checked?: boolean;
|
|
6
6
|
disabled?: boolean;
|
|
7
|
+
value?: string;
|
|
7
8
|
children: React.ReactNode;
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
interface CheckboxGroupProps {
|
|
11
|
+
value?: string[];
|
|
12
|
+
onChange?: (checkedValues: string[]) => void;
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
declare const Checkbox: React.FC<CheckboxProps> & {
|
|
16
|
+
Group: FC<CheckboxGroupProps>;
|
|
17
|
+
};
|
|
10
18
|
export default Checkbox;
|
package/es/Checkbox/Checkbox.js
CHANGED
|
@@ -1,19 +1,49 @@
|
|
|
1
|
-
import { __assign } from '../node_modules/tslib/tslib.es6.js';
|
|
1
|
+
import { __assign, __spreadArray } from '../node_modules/tslib/tslib.es6.js';
|
|
2
2
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import './checkbox.css.js';
|
|
3
|
+
import { createContext, useContext, useState, useEffect } from 'react';
|
|
5
4
|
|
|
5
|
+
var CheckboxGroupContext = createContext(null);
|
|
6
6
|
var Checkbox = function (_a) {
|
|
7
|
-
var onChange = _a.onChange, checked = _a.checked, _b = _a.disabled, disabled = _b === void 0 ? false : _b, children = _a.children;
|
|
7
|
+
var onChange = _a.onChange, checked = _a.checked, _b = _a.disabled, disabled = _b === void 0 ? false : _b, value = _a.value, children = _a.children;
|
|
8
|
+
var groupContext = useContext(CheckboxGroupContext);
|
|
8
9
|
var _c = useState(checked || false), internalChecked = _c[0], setInternalChecked = _c[1];
|
|
10
|
+
useEffect(function () {
|
|
11
|
+
if (checked !== undefined) {
|
|
12
|
+
setInternalChecked(checked);
|
|
13
|
+
}
|
|
14
|
+
}, [checked]);
|
|
9
15
|
var handleChange = function (e) {
|
|
10
16
|
var newChecked = e.target.checked;
|
|
11
17
|
setInternalChecked(newChecked);
|
|
12
18
|
if (onChange) {
|
|
13
19
|
onChange(newChecked);
|
|
14
20
|
}
|
|
21
|
+
if (groupContext && value) {
|
|
22
|
+
groupContext.onChange(value, newChecked);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var isChecked = groupContext ? groupContext.value.includes(value || '') : internalChecked;
|
|
26
|
+
return (jsxs("label", __assign({ className: "foxit-checkbox-container ".concat(disabled ? 'disabled' : '') }, { children: [jsx("input", { type: "checkbox", checked: isChecked, disabled: disabled, onChange: handleChange }), jsx("span", { className: "foxit-checkbox-checkmark ".concat(disabled ? 'disabled' : '') }), jsx("span", __assign({ className: "foxit-checkbox-content" }, { children: children }))] })));
|
|
27
|
+
};
|
|
28
|
+
var CheckboxGroup = function (_a) {
|
|
29
|
+
var value = _a.value, onChange = _a.onChange, children = _a.children;
|
|
30
|
+
var _b = useState([]), checkedValues = _b[0], setCheckedValues = _b[1];
|
|
31
|
+
useEffect(function () {
|
|
32
|
+
if (value !== undefined) {
|
|
33
|
+
setCheckedValues(value);
|
|
34
|
+
}
|
|
35
|
+
}, [value]);
|
|
36
|
+
var handleCheckboxChange = function (checkboxValue, checked) {
|
|
37
|
+
var newCheckedValues = checked
|
|
38
|
+
? __spreadArray(__spreadArray([], checkedValues, true), [checkboxValue], false) : checkedValues.filter(function (v) { return v !== checkboxValue; });
|
|
39
|
+
setCheckedValues(newCheckedValues);
|
|
40
|
+
if (onChange) {
|
|
41
|
+
onChange(newCheckedValues);
|
|
42
|
+
}
|
|
15
43
|
};
|
|
16
|
-
|
|
44
|
+
var currentValue = value !== undefined ? value : checkedValues;
|
|
45
|
+
return (jsx(CheckboxGroupContext.Provider, __assign({ value: { value: currentValue, onChange: handleCheckboxChange } }, { children: jsx("div", __assign({ className: "foxit-checkbox-group" }, { children: children })) })));
|
|
17
46
|
};
|
|
47
|
+
Checkbox.Group = CheckboxGroup;
|
|
18
48
|
|
|
19
49
|
export { Checkbox as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { __assign } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
|
|
5
|
+
var Drawer = function (_a) {
|
|
6
|
+
var open = _a.open, _b = _a.width, width = _b === void 0 ? 375 : _b, onClose = _a.onClose, children = _a.children;
|
|
7
|
+
return (jsxs(Fragment, { children: [jsx("div", { className: classNames('foxit-drawer-mask', {
|
|
8
|
+
'foxit-drawer-mask-open': open
|
|
9
|
+
}), onClick: onClose, style: { display: open ? 'block' : 'none' } }), jsx("div", __assign({ className: classNames('foxit-drawer', {
|
|
10
|
+
'foxit-drawer-open': open
|
|
11
|
+
}), style: { width: width, right: open ? 0 : -width } }, { children: jsx("div", __assign({ className: "foxit-drawer-content" }, { children: children })) }))] }));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { Drawer };
|
package/es/Empty/Empty.js
CHANGED
package/es/Form/Form.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React, { ReactNode } from 'react';
|
|
2
2
|
import { FormRefInstance } from './Form.types';
|
|
3
3
|
import FormItem from './FormItem';
|
|
4
|
+
import { useFormWatch } from './useFormWatch';
|
|
4
5
|
export type { FormRefInstance } from './Form.types';
|
|
5
6
|
interface FormProps {
|
|
6
7
|
children: ReactNode;
|
|
@@ -9,6 +10,6 @@ interface FormProps {
|
|
|
9
10
|
[key: string]: unknown;
|
|
10
11
|
};
|
|
11
12
|
}
|
|
12
|
-
export { FormItem };
|
|
13
|
+
export { FormItem, useFormWatch };
|
|
13
14
|
declare const _default: React.ForwardRefExoticComponent<FormProps & React.RefAttributes<FormRefInstance>>;
|
|
14
15
|
export default _default;
|
package/es/Form/Form.js
CHANGED
|
@@ -3,7 +3,6 @@ import { jsx } from 'react/jsx-runtime';
|
|
|
3
3
|
import { forwardRef, useRef, useCallback, useImperativeHandle } from 'react';
|
|
4
4
|
import FormProvider from './FormProvider.js';
|
|
5
5
|
import './FormContext.js';
|
|
6
|
-
import './form.css.js';
|
|
7
6
|
|
|
8
7
|
var Form = function (_a, ref) {
|
|
9
8
|
var children = _a.children, onFinish = _a.onFinish, initialValues = _a.initialValues;
|
|
@@ -22,13 +21,14 @@ var Form = function (_a, ref) {
|
|
|
22
21
|
}
|
|
23
22
|
}, [onFinish]);
|
|
24
23
|
useImperativeHandle(ref, function () {
|
|
25
|
-
var _a, _b, _c, _d, _e;
|
|
24
|
+
var _a, _b, _c, _d, _e, _f;
|
|
26
25
|
return ({
|
|
27
26
|
setFieldsValue: (_a = formRef.current) === null || _a === void 0 ? void 0 : _a.setFieldsValue,
|
|
28
27
|
getFieldsValue: (_b = formRef.current) === null || _b === void 0 ? void 0 : _b.getFieldsValue,
|
|
29
28
|
resetFields: (_c = formRef.current) === null || _c === void 0 ? void 0 : _c.resetFields,
|
|
30
29
|
validateFields: (_d = formRef.current) === null || _d === void 0 ? void 0 : _d.validateFields,
|
|
31
|
-
setFieldsStatus: (_e = formRef.current) === null || _e === void 0 ? void 0 : _e.setFieldsStatus
|
|
30
|
+
setFieldsStatus: (_e = formRef.current) === null || _e === void 0 ? void 0 : _e.setFieldsStatus,
|
|
31
|
+
subscribe: (_f = formRef.current) === null || _f === void 0 ? void 0 : _f.subscribe
|
|
32
32
|
});
|
|
33
33
|
});
|
|
34
34
|
return (jsx(FormProvider, __assign({ initialValues: initialValues, ref: formRef }, { children: jsx("form", __assign({ onSubmit: handleSubmit }, { children: children })) })));
|
package/es/Form/Form.types.d.ts
CHANGED
|
@@ -11,6 +11,12 @@ export interface ValidationRule {
|
|
|
11
11
|
min?: number;
|
|
12
12
|
max?: number;
|
|
13
13
|
}
|
|
14
|
+
export interface FieldSubscriber {
|
|
15
|
+
fieldNames: string[];
|
|
16
|
+
callback: (values: {
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}) => void;
|
|
19
|
+
}
|
|
14
20
|
export interface FormInstance {
|
|
15
21
|
setFieldsValue: (values: {
|
|
16
22
|
[key: string]: unknown;
|
|
@@ -29,6 +35,9 @@ export interface FormInstance {
|
|
|
29
35
|
}[]) => void;
|
|
30
36
|
registerField: (name: string, rules: ValidationRule[]) => void;
|
|
31
37
|
unregisterField: (name: string) => void;
|
|
38
|
+
subscribe: (fieldNames: string[], callback: (values: {
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
}) => void) => () => void;
|
|
32
41
|
}
|
|
33
42
|
export type FormRefInstance = Omit<FormInstance, 'registerField' | 'unregisterField'>;
|
|
34
43
|
export interface FormContextProps {
|
package/es/Form/FormItem.js
CHANGED
|
@@ -2,7 +2,6 @@ import { __assign } from '../node_modules/tslib/tslib.es6.js';
|
|
|
2
2
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
3
|
import React, { useContext, useEffect, useCallback } from 'react';
|
|
4
4
|
import { FormContext } from './FormContext.js';
|
|
5
|
-
import './form.css.js';
|
|
6
5
|
|
|
7
6
|
var FormItem = function (_a) {
|
|
8
7
|
var _b, _c, _d;
|
|
@@ -24,7 +23,7 @@ var FormItem = function (_a) {
|
|
|
24
23
|
var newValue = ((_b = e === null || e === void 0 ? void 0 : e.target) === null || _b === void 0 ? void 0 : _b.value) !== undefined ? e.target.value : e;
|
|
25
24
|
form.setFieldsValue((_a = {}, _a[name] = newValue, _a));
|
|
26
25
|
}, [form, name]);
|
|
27
|
-
return (jsxs("div", __assign({ className: "foxit-form-item" }, { children: [label &&
|
|
26
|
+
return (jsxs("div", __assign({ className: "foxit-form-item" }, { children: [label && (jsxs("label", { children: [label, rules.some(function (rule) { return rule.required; }) && jsx("span", __assign({ className: "foxit-form-required" }, { children: "*" }))] })), jsx("span", __assign({ className: "".concat(error ? 'foxit-form-error-component' : warning ? 'foxit-form-warning-component' : '') }, { children: React.Children.map(children, function (child) {
|
|
28
27
|
return React.cloneElement(child, {
|
|
29
28
|
value: value,
|
|
30
29
|
onChange: handleChange
|
package/es/Form/FormProvider.js
CHANGED
|
@@ -1,43 +1,56 @@
|
|
|
1
1
|
import { __assign } from '../node_modules/tslib/tslib.es6.js';
|
|
2
2
|
import { jsx } from 'react/jsx-runtime';
|
|
3
|
-
import { forwardRef,
|
|
3
|
+
import { forwardRef, useRef, useState, useEffect, useCallback, useImperativeHandle } from 'react';
|
|
4
4
|
import { FormContext } from './FormContext.js';
|
|
5
5
|
|
|
6
6
|
var FormProvider = function (_a, ref) {
|
|
7
7
|
var children = _a.children, _b = _a.initialValues, initialValues = _b === void 0 ? {} : _b;
|
|
8
|
-
var
|
|
8
|
+
var formFieldsRef = useRef({});
|
|
9
|
+
var _c = useState({}), forceUpdate = _c[1]; // Used to force re-render when ref changes
|
|
9
10
|
var formInstanceRef = useRef({});
|
|
11
|
+
var subscribersRef = useRef([]);
|
|
10
12
|
// 初始值的设置
|
|
11
13
|
useEffect(function () {
|
|
12
14
|
if (Object.keys(initialValues).length > 0) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
newFields[key] = { value: initialValues[key], rules: [] };
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
newFields[key] = __assign(__assign({}, newFields[key]), { value: initialValues[key] });
|
|
21
|
-
}
|
|
15
|
+
formFieldsRef.current = __assign({}, formFieldsRef.current); // Create a new object
|
|
16
|
+
for (var key in initialValues) {
|
|
17
|
+
if (!formFieldsRef.current[key]) {
|
|
18
|
+
formFieldsRef.current[key] = { value: initialValues[key], rules: [] };
|
|
22
19
|
}
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
else {
|
|
21
|
+
formFieldsRef.current[key] = __assign(__assign({}, formFieldsRef.current[key]), { value: initialValues[key] });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
forceUpdate({}); // Trigger re-render
|
|
25
25
|
}
|
|
26
26
|
}, [initialValues]);
|
|
27
|
+
// 订阅方法
|
|
28
|
+
var subscribe = useCallback(function (fieldNames, callback) {
|
|
29
|
+
var subscriber = { fieldNames: fieldNames, callback: callback };
|
|
30
|
+
subscribersRef.current.push(subscriber);
|
|
31
|
+
// 立即触发一次回调,提供当前值
|
|
32
|
+
var currentValues = fieldNames.reduce(function (acc, fieldName) {
|
|
33
|
+
var _a;
|
|
34
|
+
acc[fieldName] = (_a = formFieldsRef.current[fieldName]) === null || _a === void 0 ? void 0 : _a.value;
|
|
35
|
+
return acc;
|
|
36
|
+
}, {});
|
|
37
|
+
callback(currentValues);
|
|
38
|
+
// 返回取消订阅函数
|
|
39
|
+
return function () {
|
|
40
|
+
subscribersRef.current = subscribersRef.current.filter(function (s) { return s !== subscriber; });
|
|
41
|
+
};
|
|
42
|
+
}, []);
|
|
27
43
|
// 注册字段
|
|
28
44
|
var registerField = useCallback(function (name, rules) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
});
|
|
45
|
+
formFieldsRef.current = __assign({}, formFieldsRef.current); // Create a new object
|
|
46
|
+
formFieldsRef.current[name] = __assign(__assign({}, formFieldsRef.current[name]), { rules: rules || [] });
|
|
47
|
+
forceUpdate({}); // Trigger re-render
|
|
33
48
|
}, []);
|
|
34
49
|
// 销毁字段
|
|
35
50
|
var unregisterField = useCallback(function (name) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return newFields;
|
|
40
|
-
});
|
|
51
|
+
formFieldsRef.current = __assign({}, formFieldsRef.current); // Create a new object
|
|
52
|
+
delete formFieldsRef.current[name];
|
|
53
|
+
forceUpdate({}); // Trigger re-render
|
|
41
54
|
}, []);
|
|
42
55
|
// 验证字段
|
|
43
56
|
var validateField = useCallback(function (name, value, rules) {
|
|
@@ -60,13 +73,17 @@ var FormProvider = function (_a, ref) {
|
|
|
60
73
|
if (rule.type === 'string' && typeof value !== 'string') {
|
|
61
74
|
return { type: 'error', message: rule.message || 'The input is not a string' };
|
|
62
75
|
}
|
|
63
|
-
if (rule.min !== undefined &&
|
|
76
|
+
if (rule.min !== undefined &&
|
|
77
|
+
typeof value === 'string' &&
|
|
78
|
+
new Blob([value]).size < rule.min) {
|
|
64
79
|
return {
|
|
65
80
|
type: 'error',
|
|
66
81
|
message: rule.message || "The input is less than ".concat(rule.min, " characters")
|
|
67
82
|
};
|
|
68
83
|
}
|
|
69
|
-
if (rule.max !== undefined &&
|
|
84
|
+
if (rule.max !== undefined &&
|
|
85
|
+
typeof value === 'string' &&
|
|
86
|
+
new Blob([value]).size > rule.max) {
|
|
70
87
|
return {
|
|
71
88
|
type: 'error',
|
|
72
89
|
message: rule.message || "The input is more than ".concat(rule.max, " characters")
|
|
@@ -77,140 +94,153 @@ var FormProvider = function (_a, ref) {
|
|
|
77
94
|
}, []);
|
|
78
95
|
// 设置字段值
|
|
79
96
|
var setFieldsValue = useCallback(function (values) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
97
|
+
var _a, _b;
|
|
98
|
+
var changedFields = [];
|
|
99
|
+
formFieldsRef.current = __assign({}, formFieldsRef.current); // Create a new object
|
|
100
|
+
for (var key in values) {
|
|
101
|
+
if (Object.prototype.hasOwnProperty.call(values, key)) {
|
|
102
|
+
var newValue = values[key];
|
|
103
|
+
var oldValue = (_a = formFieldsRef.current[key]) === null || _a === void 0 ? void 0 : _a.value;
|
|
104
|
+
if (JSON.stringify(oldValue) !== JSON.stringify(newValue)) {
|
|
105
|
+
changedFields.push(key);
|
|
106
|
+
}
|
|
107
|
+
formFieldsRef.current[key] = __assign(__assign({}, formFieldsRef.current[key]), { value: newValue });
|
|
108
|
+
// 验证字段
|
|
109
|
+
var validation = validateField(key, newValue, ((_b = formFieldsRef.current[key]) === null || _b === void 0 ? void 0 : _b.rules) || []);
|
|
110
|
+
if (validation) {
|
|
111
|
+
if (validation.type === 'error') {
|
|
112
|
+
formFieldsRef.current[key] = __assign(__assign({}, formFieldsRef.current[key]), { error: validation.message, warning: undefined // Clear any previous warnings
|
|
113
|
+
});
|
|
98
114
|
}
|
|
99
|
-
else {
|
|
100
|
-
|
|
115
|
+
else if (validation.type === 'warning') {
|
|
116
|
+
formFieldsRef.current[key] = __assign(__assign({}, formFieldsRef.current[key]), { warning: validation.message, error: undefined // Clear any previous errors
|
|
117
|
+
});
|
|
101
118
|
}
|
|
102
119
|
}
|
|
120
|
+
else {
|
|
121
|
+
formFieldsRef.current[key] = __assign(__assign({}, formFieldsRef.current[key]), { error: undefined, warning: undefined });
|
|
122
|
+
}
|
|
103
123
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
124
|
+
}
|
|
125
|
+
// 通知订阅者
|
|
126
|
+
if (changedFields.length > 0 && subscribersRef.current.length > 0) {
|
|
127
|
+
subscribersRef.current.forEach(function (subscriber) {
|
|
128
|
+
// 检查是否有该订阅者关心的字段发生变化
|
|
129
|
+
var relevantFields = subscriber.fieldNames.filter(function (name) {
|
|
130
|
+
return changedFields.includes(name);
|
|
131
|
+
});
|
|
132
|
+
if (relevantFields.length > 0) {
|
|
133
|
+
// 提取订阅者关心的所有字段当前值
|
|
134
|
+
var watchValues = subscriber.fieldNames.reduce(function (acc, fieldName) {
|
|
135
|
+
var _a;
|
|
136
|
+
acc[fieldName] = (_a = formFieldsRef.current[fieldName]) === null || _a === void 0 ? void 0 : _a.value;
|
|
137
|
+
return acc;
|
|
138
|
+
}, {});
|
|
139
|
+
subscriber.callback(watchValues);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
forceUpdate({}); // Trigger re-render
|
|
144
|
+
}, [validateField]);
|
|
107
145
|
// 获取字段值
|
|
108
146
|
var getFieldsValue = useCallback(function (names) {
|
|
109
147
|
if (!names) {
|
|
110
|
-
return Object.keys(
|
|
111
|
-
acc[key] =
|
|
148
|
+
return Object.keys(formFieldsRef.current).reduce(function (acc, key) {
|
|
149
|
+
acc[key] = formFieldsRef.current[key].value;
|
|
112
150
|
return acc;
|
|
113
151
|
}, {});
|
|
114
152
|
}
|
|
115
153
|
return names.reduce(function (acc, key) {
|
|
116
154
|
var _a;
|
|
117
|
-
acc[key] = (_a =
|
|
155
|
+
acc[key] = (_a = formFieldsRef.current[key]) === null || _a === void 0 ? void 0 : _a.value;
|
|
118
156
|
return acc;
|
|
119
157
|
}, {});
|
|
120
|
-
}, [
|
|
158
|
+
}, []);
|
|
121
159
|
// 重置字段
|
|
122
160
|
var resetFields = useCallback(function (names) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
});
|
|
143
|
-
}, [formFields, initialValues]);
|
|
161
|
+
formFieldsRef.current = __assign({}, formFieldsRef.current); // Create a new object
|
|
162
|
+
if (names) {
|
|
163
|
+
names.forEach(function (name) {
|
|
164
|
+
if (formFieldsRef.current[name]) {
|
|
165
|
+
formFieldsRef.current[name].value = initialValues[name] || '';
|
|
166
|
+
formFieldsRef.current[name].error = undefined;
|
|
167
|
+
formFieldsRef.current[name].warning = undefined;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
Object.keys(formFieldsRef.current).forEach(function (key) {
|
|
173
|
+
formFieldsRef.current[key].value = initialValues[key] || '';
|
|
174
|
+
formFieldsRef.current[key].error = undefined;
|
|
175
|
+
formFieldsRef.current[key].warning = undefined;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
forceUpdate({}); // Trigger re-render
|
|
179
|
+
}, [initialValues]);
|
|
144
180
|
var validateFields = useCallback(function (names) {
|
|
145
181
|
return new Promise(function (resolve, reject) {
|
|
146
|
-
var fieldsToValidate = names || Object.keys(
|
|
182
|
+
var fieldsToValidate = names || Object.keys(formFieldsRef.current);
|
|
147
183
|
var values = getFieldsValue(fieldsToValidate);
|
|
148
184
|
var errors = {};
|
|
149
185
|
for (var _i = 0, fieldsToValidate_1 = fieldsToValidate; _i < fieldsToValidate_1.length; _i++) {
|
|
150
186
|
var key = fieldsToValidate_1[_i];
|
|
151
|
-
var field =
|
|
187
|
+
var field = formFieldsRef.current[key];
|
|
152
188
|
if (!field)
|
|
153
189
|
continue; // Skip if field is not registered
|
|
154
190
|
var validation = validateField(key, values[key], field.rules || []);
|
|
155
|
-
if (validation) {
|
|
156
|
-
if (validation.type === 'error') {
|
|
157
|
-
errors[key] = validation.message;
|
|
191
|
+
if (validation || field.error || field.warning) {
|
|
192
|
+
if ((validation === null || validation === void 0 ? void 0 : validation.type) === 'error' || field.error) {
|
|
193
|
+
errors[key] = (validation === null || validation === void 0 ? void 0 : validation.message) || field.error || '';
|
|
158
194
|
}
|
|
159
|
-
else if (validation.type === 'warning') {
|
|
160
|
-
validation.message;
|
|
195
|
+
else if ((validation === null || validation === void 0 ? void 0 : validation.type) === 'warning' || field.warning) {
|
|
196
|
+
(validation === null || validation === void 0 ? void 0 : validation.message) || field.warning || '';
|
|
161
197
|
}
|
|
162
198
|
}
|
|
163
199
|
}
|
|
164
200
|
if (Object.keys(errors).length > 0) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return newFields;
|
|
171
|
-
});
|
|
201
|
+
formFieldsRef.current = __assign({}, formFieldsRef.current); // Create a new object
|
|
202
|
+
for (var key in errors) {
|
|
203
|
+
formFieldsRef.current[key] = __assign(__assign({}, formFieldsRef.current[key]), { error: errors[key] });
|
|
204
|
+
}
|
|
205
|
+
forceUpdate({}); // Trigger re-render
|
|
172
206
|
reject(errors);
|
|
173
207
|
}
|
|
174
208
|
else {
|
|
175
209
|
resolve(values);
|
|
176
210
|
}
|
|
177
211
|
});
|
|
178
|
-
}, [
|
|
212
|
+
}, [getFieldsValue, validateField]);
|
|
179
213
|
var setFieldsStatus = useCallback(function (status) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (newFields[n]) {
|
|
187
|
-
// Only update if field exists
|
|
188
|
-
if (type === 'error') {
|
|
189
|
-
newFields[n] = __assign(__assign({}, newFields[n]), { error: message });
|
|
190
|
-
}
|
|
191
|
-
else if (type === 'warning') {
|
|
192
|
-
newFields[n] = __assign(__assign({}, newFields[n]), { warning: message });
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
if (newFields[name]) {
|
|
214
|
+
formFieldsRef.current = __assign({}, formFieldsRef.current); // Create a new object
|
|
215
|
+
status.forEach(function (_a) {
|
|
216
|
+
var type = _a.type, name = _a.name, message = _a.message;
|
|
217
|
+
if (Array.isArray(name)) {
|
|
218
|
+
name.forEach(function (n) {
|
|
219
|
+
if (formFieldsRef.current[n]) {
|
|
199
220
|
// Only update if field exists
|
|
200
221
|
if (type === 'error') {
|
|
201
|
-
|
|
222
|
+
formFieldsRef.current[n] = __assign(__assign({}, formFieldsRef.current[n]), { error: message });
|
|
202
223
|
}
|
|
203
224
|
else if (type === 'warning') {
|
|
204
|
-
|
|
225
|
+
formFieldsRef.current[n] = __assign(__assign({}, formFieldsRef.current[n]), { warning: message });
|
|
205
226
|
}
|
|
206
227
|
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
if (formFieldsRef.current[name]) {
|
|
232
|
+
// Only update if field exists
|
|
233
|
+
if (type === 'error') {
|
|
234
|
+
formFieldsRef.current[name] = __assign(__assign({}, formFieldsRef.current[name]), { error: message });
|
|
235
|
+
}
|
|
236
|
+
else if (type === 'warning') {
|
|
237
|
+
formFieldsRef.current[name] = __assign(__assign({}, formFieldsRef.current[name]), { warning: message });
|
|
238
|
+
}
|
|
207
239
|
}
|
|
208
|
-
}
|
|
209
|
-
return newFields;
|
|
240
|
+
}
|
|
210
241
|
});
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
useImperativeHandle(ref, function () { return (__assign({}, formInstanceRef.current)); });
|
|
242
|
+
forceUpdate({}); // Trigger re-render
|
|
243
|
+
}, []);
|
|
214
244
|
formInstanceRef.current = {
|
|
215
245
|
setFieldsValue: setFieldsValue,
|
|
216
246
|
getFieldsValue: getFieldsValue,
|
|
@@ -218,9 +248,12 @@ var FormProvider = function (_a, ref) {
|
|
|
218
248
|
validateFields: validateFields,
|
|
219
249
|
setFieldsStatus: setFieldsStatus,
|
|
220
250
|
registerField: registerField,
|
|
221
|
-
unregisterField: unregisterField
|
|
251
|
+
unregisterField: unregisterField,
|
|
252
|
+
subscribe: subscribe
|
|
222
253
|
};
|
|
223
|
-
|
|
254
|
+
// 暴露给外部的方法
|
|
255
|
+
useImperativeHandle(ref, function () { return (__assign({}, formInstanceRef.current)); });
|
|
256
|
+
return (jsx(FormContext.Provider, __assign({ value: { form: formInstanceRef.current, formFields: formFieldsRef.current } }, { children: children })));
|
|
224
257
|
};
|
|
225
258
|
var FormProvider$1 = forwardRef(FormProvider);
|
|
226
259
|
|