x-ui-design 0.6.14 → 0.6.16

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.
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  import { FormItemProps } from '../../../types/form';
3
3
  import './style.css';
4
4
  declare const FormItem: {
5
- ({ prefixCls, name, label, rules, children, className, layout, style, dependencies, feedbackIcons, extra, hideLabel, removeErrorMessageHeight, ...props }: FormItemProps): React.JSX.Element;
5
+ ({ prefixCls, name, label, rules, children, className, layout, style, extra, hideLabel, removeErrorMessageHeight, ...props }: FormItemProps): React.JSX.Element;
6
6
  displayName: string;
7
7
  };
8
8
  export default FormItem;
@@ -81,6 +81,7 @@ export interface FormItemChildComponentProps {
81
81
  noStyle?: boolean;
82
82
  feedbackIcons?: boolean;
83
83
  ref?: FieldInstancesRef | null;
84
+ dependencies?: string[];
84
85
  }
85
86
  export interface FormInstance {
86
87
  submit: () => Promise<Record<string, RuleTypes> | undefined>;
@@ -112,5 +113,4 @@ export interface FormInstance {
112
113
  setOnFinish?: (onFinish?: ((values: Record<string, RuleTypes>) => void) | undefined) => void;
113
114
  setOnValuesChange?: (onValuesChange?: (changedValues: Record<string, RuleTypes>, allValues: Record<string, RuleTypes>) => void) => void;
114
115
  changeStep: (step: number) => void;
115
- subscribeToErrors?: (callback: (errors: FieldError[]) => void) => () => void;
116
116
  }
package/dist/index.esm.js CHANGED
@@ -620,7 +620,6 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
620
620
  const fieldInstancesRef = useRef({});
621
621
  const [isReseting, setIsReseting] = useState(false);
622
622
  const errorsRef = useRef({});
623
- const errorSubscribers = useRef([]);
624
623
  const fieldSubscribers = useRef({});
625
624
  const formSubscribers = useRef([]);
626
625
  function getFormFields() {
@@ -766,8 +765,6 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
766
765
  [name]: fieldErrors
767
766
  };
768
767
  warningsRef.current[name] = fieldWarnings;
769
- const currentErrors = getFieldsError();
770
- errorSubscribers.current.forEach(callback => callback(currentErrors));
771
768
  return fieldErrors.length === 0;
772
769
  }
773
770
  async function validateFields(nameList) {
@@ -775,6 +772,7 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
775
772
  const results = await Promise.all(fieldsToValidate.map(name => validateField(name)));
776
773
  if (_scrollToFirstError.current) {
777
774
  const firstErrorContent = document.querySelectorAll('.xUi-form-item-has-error')?.[0];
775
+ console.log(firstErrorContent);
778
776
  if (firstErrorContent) {
779
777
  firstErrorContent.closest('.xUi-form-item')?.scrollIntoView({
780
778
  behavior: 'smooth'
@@ -806,8 +804,6 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
806
804
  });
807
805
  }
808
806
  formSubscribers.current.forEach(callback => callback(getFieldsValue()));
809
- const currentErrors = getFieldsError();
810
- errorSubscribers.current.forEach(callback => callback(currentErrors));
811
807
  setIsReseting(prev => !prev);
812
808
  }
813
809
  async function submit() {
@@ -841,12 +837,6 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
841
837
  fieldCallbacks.forEach(unsubscribe => unsubscribe());
842
838
  };
843
839
  }
844
- function subscribeToErrors(callback) {
845
- errorSubscribers.current.push(callback);
846
- return () => {
847
- errorSubscribers.current = errorSubscribers.current.filter(cb => cb !== callback);
848
- };
849
- }
850
840
  function setScrollToFirstError(value) {
851
841
  _scrollToFirstError.current = value;
852
842
  }
@@ -894,8 +884,7 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
894
884
  setOnFinish,
895
885
  setOnFieldsChange,
896
886
  setOnValuesChange,
897
- changeStep,
898
- subscribeToErrors
887
+ changeStep
899
888
  };
900
889
  return formInstance;
901
890
  };
@@ -982,6 +971,17 @@ function flattenChildren(children) {
982
971
  var css_248z$l = ".xUi-form-item{display:flex;position:relative}.xUi-form-item.noStyle{display:inline-flex;margin-bottom:0}.xUi-form-item-label{align-items:center;color:var(--xui-text-color);display:flex;font-size:var(--xui-font-size-md);font-weight:500;line-height:20px;margin-bottom:4px}.xUi-form-item-error{color:var(--xui-error-color);display:block;font-size:var(--xui-font-size-xs);line-height:16px;margin-bottom:8px;margin-top:4px;min-height:16px;position:relative;right:0;text-align:end;user-select:none}.xUi-form-item-required{color:var(--xui-error-color);display:inline-block;font-size:var(--xui-font-size-md);line-height:1;margin-left:4px;margin-right:4px}.xUi-form-item.horizontal{align-items:center;flex-direction:row;gap:4px}.xUi-form-item.vertical{align-self:flex-start;flex-direction:column}.xUi-form-item .xUi-input-container{width:-webkit-fill-available}";
983
972
  styleInject(css_248z$l);
984
973
 
974
+ const debounce = (func, delay) => {
975
+ let timeoutId = null;
976
+ return (...args) => {
977
+ if (timeoutId) {
978
+ clearTimeout(timeoutId);
979
+ }
980
+ timeoutId = setTimeout(() => {
981
+ func(...args);
982
+ }, delay);
983
+ };
984
+ };
985
985
  const FormItem$1 = ({
986
986
  prefixCls = prefixClsFormItem,
987
987
  name,
@@ -991,8 +991,6 @@ const FormItem$1 = ({
991
991
  className = '',
992
992
  layout = 'vertical',
993
993
  style = {},
994
- dependencies = [],
995
- feedbackIcons,
996
994
  extra,
997
995
  hideLabel = false,
998
996
  removeErrorMessageHeight = false,
@@ -1009,10 +1007,7 @@ const FormItem$1 = ({
1009
1007
  isReseting,
1010
1008
  registerField,
1011
1009
  getFieldInstance,
1012
- setFieldInstance,
1013
- subscribeToFields,
1014
- validateFields,
1015
- subscribeToErrors
1010
+ setFieldInstance
1016
1011
  } = formContext;
1017
1012
  const childrenList = useMemo(() => flattenChildren(children), [children]);
1018
1013
  useEffect(() => {
@@ -1024,22 +1019,6 @@ const FormItem$1 = ({
1024
1019
  setFieldInstance(name, fieldRef.current);
1025
1020
  }, [name, fieldRef.current]);
1026
1021
  useEffect(() => () => registerField(name, undefined, true), [name]);
1027
- useEffect(() => {
1028
- if (name && dependencies.length > 0) {
1029
- const unsubscribe = subscribeToFields(dependencies, () => {
1030
- validateFields([name]);
1031
- });
1032
- return () => {
1033
- unsubscribe();
1034
- };
1035
- }
1036
- }, [dependencies, name]);
1037
- useEffect(() => {
1038
- const unsubscribe = subscribeToErrors?.(errors => {
1039
- setErrorMessage(errors.find(error => error.name === name)?.errors?.[0] || '');
1040
- });
1041
- return () => unsubscribe?.();
1042
- }, []);
1043
1022
  const isRequired = useMemo(() => rules.some(rule => rule.required), [rules]);
1044
1023
  return /*#__PURE__*/React.createElement("div", {
1045
1024
  style: style,
@@ -1092,8 +1071,8 @@ const FormItemChildComponent = ({
1092
1071
  initialValue,
1093
1072
  normalize,
1094
1073
  noStyle,
1095
- feedbackIcons,
1096
1074
  ref,
1075
+ dependencies = [],
1097
1076
  ...props
1098
1077
  }) => {
1099
1078
  const formContext = useContext(FormContext);
@@ -1101,7 +1080,9 @@ const FormItemChildComponent = ({
1101
1080
  const {
1102
1081
  getFieldsValue,
1103
1082
  getFieldValue,
1104
- setFieldValue
1083
+ setFieldValue,
1084
+ subscribeToFields,
1085
+ validateFields
1105
1086
  } = formContext || {};
1106
1087
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1107
1088
  // @ts-expect-error
@@ -1115,6 +1096,19 @@ const FormItemChildComponent = ({
1115
1096
  setFieldValue?.(name, initialValue);
1116
1097
  }
1117
1098
  }, []);
1099
+ useEffect(() => {
1100
+ if (name && dependencies.length > 0) {
1101
+ const unsubscribe = subscribeToFields?.(dependencies, () => {
1102
+ validateFields?.([name]);
1103
+ });
1104
+ return () => {
1105
+ unsubscribe?.();
1106
+ };
1107
+ }
1108
+ }, [dependencies, name]);
1109
+ const debouncedSetFieldValue = useRef(debounce((name, value) => {
1110
+ setFieldValue?.(name, value, undefined, undefined, true);
1111
+ }, 10)).current;
1118
1112
  const handleChange = (e, option) => {
1119
1113
  let rawValue = e?.target ? e.target.value : e;
1120
1114
  if (normalize) {
@@ -1131,7 +1125,7 @@ const FormItemChildComponent = ({
1131
1125
  return;
1132
1126
  }
1133
1127
  }
1134
- setFieldValue?.(name, rawValue, undefined, undefined, true);
1128
+ debouncedSetFieldValue(name, rawValue);
1135
1129
  onChange?.(e, option);
1136
1130
  };
1137
1131
  const injectPropsIntoFinalLeaf = child => {
@@ -3036,6 +3030,7 @@ const InputComponent = ({
3036
3030
  }, prefix && /*#__PURE__*/React.createElement("span", {
3037
3031
  className: `${prefixCls}-prefix`
3038
3032
  }, prefix), /*#__PURE__*/React.createElement("input", _extends({}, props, {
3033
+ suppressHydrationWarning: true,
3039
3034
  ref: inputRef
3040
3035
  }, props.type === 'password' && iconRender ? {
3041
3036
  type: iconRenderVisible ? 'text' : 'password'