tdesign-react
Version:
TDesign Component for React
153 lines (149 loc) • 7.49 kB
JavaScript
/**
* tdesign v1.16.2
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _toConsumableArray } from '../_chunks/dep-a3a3e527.js';
import { _ as _slicedToArray } from '../_chunks/dep-10d5731f.js';
import { _ as _defineProperty } from '../_chunks/dep-d67deb2c.js';
import React, { useRef, useImperativeHandle, useEffect } from 'react';
import classNames from 'classnames';
import forwardRefWithStatics from '../_util/forwardRefWithStatics.js';
import noop from '../_util/noop.js';
import useConfig from '../hooks/useConfig.js';
import useDefaultProps from '../hooks/useDefaultProps.js';
import FormContext from './FormContext.js';
import FormItem from './FormItem.js';
import FormList from './FormList.js';
import { formDefaultProps } from './defaultProps.js';
import useForm, { HOOK_MARK } from './hooks/useForm.js';
import useInstance from './hooks/useInstance.js';
import useWatch from './hooks/useWatch.js';
import '../_chunks/dep-74a10cfb.js';
import '../_chunks/dep-8abcbcbc.js';
import 'hoist-non-react-statics';
import '../config-provider/ConfigContext.js';
import 'lodash-es';
import '../_chunks/dep-6b235a32.js';
import '../_chunks/dep-b9afa32b.js';
import 'dayjs';
import '../_chunks/dep-4f115c18.js';
import 'tdesign-icons-react';
import '../hooks/useGlobalIcon.js';
import '../locale/LocalReceiver.js';
import '../config-provider/ConfigProvider.js';
import './const.js';
import './formModel.js';
import '../_chunks/dep-fe89155e.js';
import '../_chunks/dep-ed34fbd4.js';
import './hooks/useFormItemInitialData.js';
import './hooks/useFormItemStyle.js';
import '../_util/parseTNode.js';
import '../_chunks/dep-2ba7b13f.js';
import './utils/index.js';
import '../_chunks/dep-ff40554e.js';
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var Form = forwardRefWithStatics(function (originalProps, ref) {
var _form$getInternalHook, _form$getInternalHook2;
var _useConfig = useConfig(),
classPrefix = _useConfig.classPrefix,
globalFormConfig = _useConfig.form;
var props = useDefaultProps(originalProps, formDefaultProps);
var style = props.style,
className = props.className,
labelWidth = props.labelWidth,
statusIcon = props.statusIcon,
labelAlign = props.labelAlign,
layout = props.layout,
colon = props.colon,
initialData = props.initialData,
_props$requiredMark = props.requiredMark,
requiredMark = _props$requiredMark === void 0 ? globalFormConfig.requiredMark : _props$requiredMark,
_props$requiredMarkPo = props.requiredMarkPosition,
requiredMarkPosition = _props$requiredMarkPo === void 0 ? globalFormConfig.requiredMarkPosition : _props$requiredMarkPo,
scrollToFirstError = props.scrollToFirstError,
showErrorMessage = props.showErrorMessage,
resetType = props.resetType,
rules = props.rules,
_props$errorMessage = props.errorMessage,
errorMessage = _props$errorMessage === void 0 ? globalFormConfig.errorMessage : _props$errorMessage,
disabled = props.disabled,
children = props.children,
id = props.id,
onReset = props.onReset,
_props$onValuesChange = props.onValuesChange,
onValuesChange = _props$onValuesChange === void 0 ? noop : _props$onValuesChange;
var formClass = classNames("".concat(classPrefix, "-form"), className, _defineProperty({}, "".concat(classPrefix, "-form-inline"), layout === "inline"));
var _useForm = useForm(props.form),
_useForm2 = _slicedToArray(_useForm, 1),
form = _useForm2[0];
var formRef = useRef(null);
var formMapRef = useRef(/* @__PURE__ */new Map());
var floatingFormDataRef = useRef({});
var formInstance = useInstance(props, formRef, formMapRef, floatingFormDataRef);
useImperativeHandle(ref, function () {
return formInstance;
});
Object.assign(form, _objectSpread({}, formInstance));
form === null || form === void 0 || (_form$getInternalHook = form.getInternalHooks) === null || _form$getInternalHook === void 0 || (_form$getInternalHook = _form$getInternalHook.call(form, HOOK_MARK)) === null || _form$getInternalHook === void 0 || (_form$getInternalHook2 = _form$getInternalHook.setForm) === null || _form$getInternalHook2 === void 0 || _form$getInternalHook2.call(_form$getInternalHook, formInstance);
useEffect(function () {
var _form$getInternalHook3, _form$getInternalHook4;
form === null || form === void 0 || (_form$getInternalHook3 = form.getInternalHooks) === null || _form$getInternalHook3 === void 0 || (_form$getInternalHook3 = _form$getInternalHook3.call(form, HOOK_MARK)) === null || _form$getInternalHook3 === void 0 || (_form$getInternalHook4 = _form$getInternalHook3.flashQueue) === null || _form$getInternalHook4 === void 0 || _form$getInternalHook4.call(_form$getInternalHook3);
}, [form]);
function onResetHandler(e) {
var _form$getInternalHook5, _form$getInternalHook6;
_toConsumableArray(formMapRef.current.values()).forEach(function (formItemRef) {
formItemRef === null || formItemRef === void 0 || formItemRef.current.resetField();
});
form === null || form === void 0 || (_form$getInternalHook5 = form.getInternalHooks) === null || _form$getInternalHook5 === void 0 || (_form$getInternalHook5 = _form$getInternalHook5.call(form, HOOK_MARK)) === null || _form$getInternalHook5 === void 0 || (_form$getInternalHook6 = _form$getInternalHook5.notifyWatch) === null || _form$getInternalHook6 === void 0 || _form$getInternalHook6.call(_form$getInternalHook5, []);
form.store = {};
onReset === null || onReset === void 0 || onReset({
e: e
});
}
function onFormItemValueChange(changedValue) {
requestAnimationFrame(function () {
var allFields = formInstance.getFieldsValue(true);
onValuesChange(changedValue, allFields);
});
}
return /* @__PURE__ */React.createElement(FormContext.Provider, {
value: {
form: form,
labelWidth: labelWidth,
statusIcon: statusIcon,
labelAlign: labelAlign,
layout: layout,
colon: colon,
initialData: initialData,
requiredMark: requiredMark,
requiredMarkPosition: requiredMarkPosition,
errorMessage: errorMessage,
showErrorMessage: showErrorMessage,
scrollToFirstError: scrollToFirstError,
resetType: resetType,
rules: rules,
disabled: disabled,
readOnly: props.readOnly || props.readonly,
formMapRef: formMapRef,
floatingFormDataRef: floatingFormDataRef,
onFormItemValueChange: onFormItemValueChange
}
}, /* @__PURE__ */React.createElement("form", {
ref: formRef,
id: id,
style: style,
className: formClass,
onSubmit: formInstance.submit,
onReset: onResetHandler
}, children));
}, {
useForm: useForm,
useWatch: useWatch,
FormItem: FormItem,
FormList: FormList
});
Form.displayName = "Form";
export { Form as default };
//# sourceMappingURL=Form.js.map