Skip to content

Commit

Permalink
fix: incorrect data display in dynamic forms
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuzhang committed Jan 29, 2024
1 parent fbfee89 commit 18f9ea8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/use/useFormItemContentController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import { Apis, GlobalProps } from 'react-form-simple/types/form';
import { useController } from 'react-form-simple/use/useController';
import { useControllerRef } from 'react-form-simple/use/useControllerRef';
import { FormUtil } from 'react-form-simple/utils/FormUtil';
import {
getProxyValue,
Expand Down Expand Up @@ -54,6 +53,8 @@ export function useFormItemContentController(
formatChangeValue,
} = options;

const preBindId = useRef(bindId).current;

const _model = useController({
value: convertStringToObject(bindId, initialValue),
});
Expand All @@ -77,11 +78,17 @@ export function useFormItemContentController(
formUtil.replace({ model: modelValue });
}, [modelValue]);

const methods = useControllerRef({
useEffect(() => {
if (bindId !== preBindId) {
_model.value = convertStringToObject(bindId, initialValue);
}
}, [bindId]);

const methods = {
set(value: any) {
updateProxyValue(modelValue, bindId as string, value);
},
});
};

const value = getProxyValue(modelValue, bindId) ?? '';

Expand Down
13 changes: 9 additions & 4 deletions src/use/useSubscribe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { cloneDeep } from 'lodash';
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { UseSubscribeNamespace } from 'react-form-simple/types/use';
import { useControllerRef } from 'react-form-simple/use/useControllerRef';
import { isEqual } from 'react-form-simple/utils/util';

const subscribeObject = () => {
const object = {
Expand All @@ -26,15 +27,19 @@ export const usePrivateSubscribe = <T extends Record<string, any>>(options: {
T,
ReturnType<typeof subscribeObject>
> => {
const { model } = options;

const subscribes = useControllerRef(subscribeObject());

const useSubscribe: UseSubscribeNamespace.UseSubscribe<T> = (cb) => {
const [state, setState] = useState<any>();
const preValueRef = useRef(null) as any;

useEffect(() => {
subscribes.set(() => {
setState(cb({ model: cloneDeep(model) }));
const { model } = options;
const value = cb({ model: cloneDeep(model) });
if (isEqual(value, preValueRef.current)) return;
preValueRef.current = value;
setState(value);
});
setTimeout(() => {
subscribes.emit();
Expand Down

0 comments on commit 18f9ea8

Please sign in to comment.