x-ui-design 0.4.76 → 0.4.78

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.
@@ -14,6 +14,7 @@ import { clsx } from '../../../helpers';
14
14
  import { RuleType, SyntheticBaseEvent } from '../../../types';
15
15
  import { flattenChildren } from '../../../helpers/flatten';
16
16
  import {
17
+ FieldInstancesInputRef,
17
18
  FieldInstancesRef,
18
19
  FormItemChildComponentProps,
19
20
  FormItemProps
@@ -44,7 +45,7 @@ const FormItem = ({
44
45
  const formContext = useContext(FormContext);
45
46
 
46
47
  const errorRef = useRef<HTMLSpanElement>(null);
47
- const fieldRef = useRef<FieldInstancesRef>(null);
48
+ const fieldRef = useRef(null);
48
49
 
49
50
  if (!formContext) {
50
51
  throw new Error('FormItem must be used within a Form');
@@ -65,7 +66,7 @@ const FormItem = ({
65
66
 
66
67
  useEffect(() => {
67
68
  if (name && !getFieldInstance(name)) {
68
- registerField(name, rules, false, fieldRef.current ?? undefined);
69
+ registerField(name, rules, false, fieldRef.current);
69
70
  }
70
71
  // eslint-disable-next-line react-hooks/exhaustive-deps
71
72
  }, [name, rules, fieldRef.current]);
@@ -100,22 +101,10 @@ const FormItem = ({
100
101
 
101
102
  const errorMessage = getFieldError(name)?.[0];
102
103
 
103
- const mergeRefs = (elementRef: (el: FieldInstancesRef) => void) => {
104
- return (el: FieldInstancesRef) => {
105
- fieldRef.current = el;
106
-
107
- if (typeof elementRef === 'function') {
108
- elementRef(el);
109
- } else if (elementRef && typeof elementRef === 'object') {
110
- // @ts-ignore
111
- elementRef.current = el;
112
- }
113
- };
114
- };
115
-
116
104
  return (
117
105
  <div
118
106
  style={style}
107
+ ref={fieldRef}
119
108
  className={clsx([
120
109
  `${prefixCls}`,
121
110
  {
@@ -142,9 +131,7 @@ const FormItem = ({
142
131
 
143
132
  return (
144
133
  <div
145
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
146
- // @ts-expect-error
147
- ref={mergeRefs(child.ref)}
134
+
148
135
  >
149
136
  <FormItemChildComponent
150
137
  {...props}
@@ -168,10 +168,14 @@ const useForm = (
168
168
  return !!name;
169
169
  }
170
170
 
171
- function registerField(name: string, rules: RuleObject[] = [], remove: boolean = false, fieldRef?: FieldInstancesRef) {
171
+ function registerField(name: string, rules: RuleObject[] = [], remove: boolean = false, fieldRef?: FieldInstancesRef | null) {
172
172
  if (remove) {
173
173
  delete formRef.current[stepRef.current]?.[name];
174
174
  delete rulesRef.current[name];
175
+
176
+ if (fieldInstancesRef.current[name]) {
177
+ delete fieldInstancesRef.current[name];
178
+ }
175
179
  } else {
176
180
  if (!(name in formRef.current[stepRef.current])) {
177
181
  formRef.current[stepRef.current][name] = initialValues?.[name];
package/lib/types/form.ts CHANGED
@@ -34,7 +34,7 @@ export interface FieldData {
34
34
  errors?: string[];
35
35
  }
36
36
 
37
- export type FieldInstancesInputRef = HTMLInputElement | null;
37
+ export type FieldInstancesInputRef = HTMLInputElement | HTMLDivElement | null;
38
38
  export type FieldInstancesRef = {
39
39
  input?: FieldInstancesInputRef;
40
40
  };
@@ -119,7 +119,7 @@ export interface FormInstance {
119
119
  setFields: (fields: FieldData[]) => void;
120
120
  resetFields: (nameList?: string[], showError?: boolean | null) => void;
121
121
  getFieldError: (name: string) => string[];
122
- registerField: (name: string, rules?: RuleObject[], remove?: boolean, fieldRef?: FieldInstancesRef) => void;
122
+ registerField: (name: string, rules?: RuleObject[], remove?: boolean, fieldRef?: FieldInstancesRef | null) => void;
123
123
  setFieldValue: (name: string, value: RuleTypes, errors?: string[],
124
124
  reset?: boolean | null | undefined,
125
125
  touch?: boolean) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x-ui-design",
3
- "version": "0.4.76",
3
+ "version": "0.4.78",
4
4
  "license": "ISC",
5
5
  "author": "Gabriel Boyajyan",
6
6
  "main": "dist/index.js",
package/src/app/page.tsx CHANGED
@@ -2080,6 +2080,8 @@ export default function Home() {
2080
2080
  setStep(step - 1)
2081
2081
  }}>Previous</Button>}
2082
2082
  <Button type="primary" size="middle" htmlType="button" onClick={async () => {
2083
+ console.log(form.getFieldInstance('username'));
2084
+
2083
2085
  if (await form.validateFields()) {
2084
2086
  if (step === 2) {
2085
2087
  form.submit();