-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseFormItemContentController.ts
More file actions
181 lines (157 loc) · 5.04 KB
/
Copy pathuseFormItemContentController.ts
File metadata and controls
181 lines (157 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { useCallback, useEffect, useRef, useState } from 'react';
import {
getProxyValue,
updateProxyValue,
} from 'react-form-simple/driver/ControllerDriver';
import { ValidationUtils } from 'react-form-simple/driver/ValidDriver';
import { Apis, GlobalProps } from 'react-form-simple/types/form';
import { Subscribe } from 'react-form-simple/utils/subscribe';
import { getEventCbValue, isMeaningful } from 'react-form-simple/utils/util';
const convertStringToObject = (
inputString?: GlobalProps.BindId,
lastValue: any = undefined,
) => {
if (!inputString) return {};
const keys: string[] = String(inputString).split('.');
const result: Record<string, any> = {};
keys.reduce(
(currentLevel: Record<string, any>, key: string, index: number) => {
currentLevel[key] = index === keys.length - 1 ? lastValue : {};
return currentLevel[key];
},
result,
);
return result;
};
export interface UseFormItemContentController
extends GlobalProps.FormItemProps {
validationUtil: ValidationUtils;
subscribe: InstanceType<typeof Subscribe>;
onChange: (...args: any[]) => void;
onBlur: () => void;
apis: Apis.FormItemApis;
contextProps?: { model?: Record<string, any>; observerFactory?: any };
}
export function useFormItemContentController(
options: UseFormItemContentController,
) {
const {
bindId,
initialValue,
validationUtil,
getContent,
subscribe,
onChange,
onBlur,
readOnly,
apis,
formatChangeValue,
contextProps,
} = options;
const previousBindId = useRef(bindId);
// Simplified forceUpdate implementation
const [, setTick] = useState(0);
const forceUpdate = useCallback(() => {
// Trigger subscribe and watch notifications for form item updates
if (contextProps?.observerFactory) {
contextProps.observerFactory.subscribeManager.notify();
contextProps.observerFactory.watchManager.notify();
}
setTick((prev) => prev + 1);
}, [contextProps?.observerFactory]);
// Use shared model instead of independent modelValue
const sharedModel = contextProps?.model;
const modelValue = useRef(
sharedModel || convertStringToObject(bindId, initialValue),
);
// If there's a shared model, use shared model
if (sharedModel) {
modelValue.current = sharedModel;
}
validationUtil.updateModel(modelValue.current);
const isError = useRef(false);
const errorMessage = useRef('');
const isSubscribeEventInitialized = useRef(true);
if (isSubscribeEventInitialized.current) {
subscribe.on('update', (value) => {
methods.set(value);
});
subscribe.on('onErr', (value) => {
if (errorMessage.current !== value) {
forceUpdate();
}
errorMessage.current = value;
isError.current = isMeaningful(value);
});
isSubscribeEventInitialized.current = false;
}
useEffect(() => {
if (bindId !== previousBindId.current) {
if (!sharedModel) {
modelValue.current = convertStringToObject(bindId, initialValue);
}
previousBindId.current = bindId;
forceUpdate();
}
}, [bindId, forceUpdate, sharedModel]);
const methods = {
set(value: any) {
if (sharedModel) {
// If using shared model, update shared model directly
// Try to directly trigger Proxy's set trap
const path = previousBindId.current as string;
const pathParts = path.split('.');
if (pathParts.length === 3 && pathParts[0] === 'array') {
// Special handling for array paths: array.0.itemValue
const arrayIndex = parseInt(pathParts[1]);
const property = pathParts[2];
// Directly set array element property
if (sharedModel.array && sharedModel.array[arrayIndex]) {
sharedModel.array[arrayIndex][property] = value;
}
// Manually trigger observer notification
if (contextProps?.observerFactory?.watchManager) {
contextProps.observerFactory.watchManager.notify(sharedModel, path);
}
} else {
// Use updateProxyValue for other paths
updateProxyValue(sharedModel, path, value);
}
} else {
// Otherwise update local model
updateProxyValue(
modelValue.current,
previousBindId.current as string,
value,
);
}
forceUpdate();
},
};
const value = getProxyValue(modelValue.current, previousBindId.current) ?? '';
const renderContent =
getContent?.({
model: modelValue.current,
bindId: previousBindId.current as string,
attrs: {
readOnly,
onChange: (e, tagType) => {
const formattedValue = getEventCbValue(e, tagType, formatChangeValue);
methods.set(formattedValue);
onChange?.(formattedValue);
},
onBlur: () => {
onBlur?.();
},
value,
checked: Boolean(value),
},
isError: isError.current,
errorMessage: errorMessage.current,
...apis,
}) || null;
return {
renderContent,
};
}
export default useFormItemContentController;