Skip to content

Commit 0d9d5ab

Browse files
committed
fix: Temporary Save 2025303
1 parent a161a74 commit 0d9d5ab

File tree

17 files changed

+533
-120
lines changed

17 files changed

+533
-120
lines changed

packages/form/src/components/Item/index.less

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
.item__message {
5656
font-size: 12px;
5757
color: red;
58+
white-space: normal;
59+
word-break: break-word;
5860
}
5961

6062
&--error .item__control {

packages/form/src/core.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ export const ItemCore = (props: ItemCoreProps) => {
9797
if (!isValidFormName(currentPath) || !form) return;
9898
// 回填初始值
9999
const initValue = initialValue === undefined ? deepGet(initialValues, currentPath) : initialValue;
100-
form.setInitialValue(currentPath, initValue);
100+
if (initValue !== undefined) {
101+
form.setInitialValue(currentPath, initValue);
102+
}
101103
onFieldsMounted && onFieldsMounted({ name: currentPath, value: initValue }, form?.getFieldValue());
102104
return () => {
103105
// 清除该表单域的props(在设置值的前面)
@@ -111,8 +113,21 @@ export const ItemCore = (props: ItemCoreProps) => {
111113
const bindChildren = (children: ItemCoreProps['children']) => {
112114
if (typeof children === 'function') {
113115
const bindProps = form && form.getBindProps(currentPath, value) || {};
114-
return children({ className: errorClassName, form: form, bindProps: bindProps });
116+
return children({ className: errorClassName, form, bindProps });
115117
} else {
118+
const len = React.Children.count(children);
119+
if (len === 1 && React.isValidElement(children)) {
120+
const bindProps = form && form.getBindProps(currentPath, value) || {};
121+
return React.cloneElement(children, {
122+
...bindProps,
123+
[trigger]: (...args) => {
124+
children?.props?.[trigger]?.(...args);
125+
bindProps?.[trigger]?.(...args);
126+
},
127+
className: errorClassName,
128+
form,
129+
});
130+
}
116131
return children;
117132
}
118133
};

packages/form/src/store.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { deepGet, deepSet, getValuePropName, comparePrefix, FormPathType, isVali
22
import { deepClone } from './utils/object';
33
import { handleRules, isCanTrigger } from './validator';
44
import { getRulesTriggers, mergeTriggers } from './core';
5-
import { isEmpty, isObject } from './utils/type';
5+
import { isArray, isEmpty, isObject } from './utils/type';
66
import { FormItemProps } from './form-item';
77
import { GetMapValueType, PathValue } from './typings';
88

@@ -171,7 +171,11 @@ export class SimpleForm<T = unknown> {
171171
this.notifyForm(path);
172172
};
173173

174+
<<<<<<< Updated upstream
174175
if (isObject(args?.[0])) {
176+
=======
177+
if (isObject(args?.[0]) || isArray(args?.[0])) {
178+
>>>>>>> Stashed changes
175179
Promise.all(Object.keys(args[0]).map((n) => setFormItemValue(n, args[0]?.[n], args[1])));
176180
} else {
177181
setFormItemValue(...args);

packages/form/src/validator/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const handleRules = async (rules?: FormRule[], value?: unknown, eventName
6868
export const isCanTrigger = (eventName?: ItemCoreProps['trigger'], validateTrigger?: ItemCoreProps['validateTrigger']) => {
6969
// 默认允许触发
7070
if (eventName === undefined) return true;
71+
if (typeof eventName === 'boolean') return eventName;
7172
if (validateTrigger === undefined) return true;
7273
if (typeof validateTrigger === 'string') {
7374
return validateTrigger === eventName;

packages/interpreter/src/constants.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,38 @@ const WORKER_CODE = [
101101
"close();",
102102
"};"];
103103

104-
const placeholderGet_ =
105-
function () { throw Error('Placeholder getter'); };
106-
const placeholderSet_ =
107-
function () { throw Error('Placeholder setter'); };
104+
// 伪JS全局变量/方法
105+
const GLOBAL_TYPE = {
106+
Function: 'Function',
107+
Object: 'Object',
108+
Array: 'Array',
109+
String: 'String',
110+
Boolean: 'Boolean',
111+
Number: 'Number',
112+
Date: 'Date',
113+
RegExp: 'RegExp',
114+
Error: 'Error',
115+
Math: 'Math',
116+
JSON: 'JSON'
117+
};
118+
119+
const ERROR_KEYS = {
120+
'EvalError': 'EvalError',
121+
'RangeError': 'RangeError',
122+
'ReferenceError': 'ReferenceError',
123+
'SyntaxError': 'SyntaxError',
124+
'TypeError': 'TypeError',
125+
'URIError': 'URIError',
126+
};
127+
128+
const ERROR_TYPES = {
129+
[ERROR_KEYS.EvalError]: EvalError,
130+
[ERROR_KEYS.RangeError]: RangeError,
131+
[ERROR_KEYS.ReferenceError]: ReferenceError,
132+
[ERROR_KEYS.SyntaxError]: SyntaxError,
133+
[ERROR_KEYS.TypeError]: TypeError,
134+
[ERROR_KEYS.URIError]: URIError,
135+
};
108136

109137
export const Constants = {
110138
Completion,
@@ -121,6 +149,7 @@ export const Constants = {
121149
REGEXP_TIMEOUT,
122150
vm,
123151
WORKER_CODE,
124-
placeholderGet_,
125-
placeholderSet_,
152+
GLOBAL_TYPE,
153+
ERROR_KEYS,
154+
ERROR_TYPES,
126155
};

packages/interpreter/src/constructor.ts

Lines changed: 191 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11

2+
import { Constants } from "./constants";
3+
import { isArray, isString } from "./global/type";
24
import { AcornNode } from "./typings";
3-
import { getPropInPrototypeChain } from "./utils";
5+
import { legalArrayIndex, legalArrayLength } from "./utils/array";
6+
import { throwException } from "./utils/error";
7+
import {
8+
bindClassPrototype,
9+
getPropInPrototypeChain,
10+
placeholderGet_,
11+
placeholderSet_
12+
} from "./utils/object";
413

514
// 对象构造函数
615
class ObjectConstructor {
@@ -10,18 +19,19 @@ class ObjectConstructor {
1019
getter: { [key: string]: () => any };
1120
setter: { [key: string]: (value: any) => void };
1221
properties: { length: number; name?: string; message?: string };
13-
proto: object | null;
22+
proto: ObjectConstructor | null;
1423
class: string = 'Object';
1524
data: Date | RegExp | boolean | number | string | null = null; // 值
1625
preventExtensions?: boolean;
26+
illegalConstructor?: boolean;
1727

1828
constructor(proto) {
1929
this.getter = Object.create(null);
2030
this.setter = Object.create(null);
2131
this.properties = Object.create(null);
2232
this.proto = proto;
33+
bindClassPrototype(ObjectConstructor, this);
2334
}
24-
2535
toString() {
2636
if (!ObjectConstructor.currentInterpreter_) {
2737
// Called from outside an interpreter.
@@ -93,6 +103,184 @@ class ObjectConstructor {
93103
}
94104
return /** @type {(boolean|number|string)} */ (this.data); // 基础类型
95105
}
106+
getProperty(name: string, script) {
107+
if (script.getterStep_) {
108+
throw Error('Getter not supported in that context');
109+
}
110+
name = String(name);
111+
let obj: ObjectConstructor | null = this;
112+
if (obj === undefined || obj === null) {
113+
throwException(Constants.ERROR_KEYS.TypeError,
114+
"Cannot read property '" + name + "' of " + obj, script);
115+
}
116+
if (typeof obj === 'object' && !(obj instanceof ObjectConstructor)) {
117+
throw TypeError('Expecting native value or pseudo object');
118+
}
119+
if (name === 'length') {
120+
// Special cases for magic length property.
121+
if (isString(obj)) {
122+
return String(obj).length;
123+
}
124+
} else if (name.charCodeAt(0) < 0x40) {
125+
// Might have numbers in there?
126+
// Special cases for string array indexing
127+
if (isString(obj)) {
128+
const n = legalArrayIndex(name);
129+
if (!isNaN(n) && n < String(obj).length) {
130+
return String(obj)[n];
131+
}
132+
}
133+
}
134+
while (obj !== null) {
135+
if (obj.properties && name in obj.properties) {
136+
const getter = obj.getter[name];
137+
if (getter) {
138+
script.getterStep_ = true;
139+
return getter;
140+
}
141+
return obj.properties[name];
142+
}
143+
obj = obj.proto;
144+
}
145+
return undefined;
146+
}
147+
setProperty(name, value, opt_descriptor, script) {
148+
const obj = this;
149+
if (script?.setterStep_) {
150+
// Getter from previous call to setProperty was not handled.
151+
throw Error('Setter not supported in that context');
152+
}
153+
const strict = !script?.stateStack || script?.getScope()?.strict;
154+
if (isString(obj)) {
155+
const n = legalArrayIndex(name);
156+
if (name === 'length' || (!isNaN(n) && n < String(obj).length)) {
157+
// Can't set length or letters on String objects.
158+
if (strict) {
159+
throwException(Constants.ERROR_KEYS.TypeError, "Cannot assign to read only " +
160+
"property '" + name + "' of String '" + obj.data + "'", script);
161+
}
162+
return;
163+
}
164+
}
165+
if (isArray(obj)) {
166+
// Arrays have a magic length variable that is bound to the elements.
167+
const len = obj.properties.length;
168+
if (name === 'length') {
169+
if (opt_descriptor && !('value' in opt_descriptor)) {
170+
return;
171+
}
172+
const maxLen = legalArrayLength(opt_descriptor && 'value' in opt_descriptor ? opt_descriptor['value'] : value);
173+
if (isNaN(maxLen)) {
174+
throwException(Constants.ERROR_KEYS.RangeError, 'Invalid array length', script);
175+
}
176+
if (maxLen < len) {
177+
for (let key in obj.properties) {
178+
const index = legalArrayIndex(key);
179+
if (!isNaN(index) && index >= maxLen) {
180+
delete obj.properties[index];
181+
}
182+
}
183+
}
184+
} else {
185+
// Increase length if this index is larger.
186+
const index = legalArrayIndex(name);
187+
if (!isNaN(index)) {
188+
obj.properties.length = Math.max(len, index + 1);
189+
}
190+
}
191+
}
192+
if (obj.preventExtensions && !(name in obj.properties)) {
193+
if (strict) {
194+
throwException(Constants.ERROR_KEYS.TypeError, "Can't add property '" + name +
195+
"', object is not extensible", script);
196+
}
197+
return;
198+
}
199+
if (opt_descriptor) {
200+
if (opt_descriptor && ('get' in opt_descriptor || 'set' in opt_descriptor) &&
201+
('value' in opt_descriptor || 'writable' in opt_descriptor)) {
202+
throwException(Constants.ERROR_KEYS.TypeError, 'Invalid property descriptor. ' +
203+
'Cannot both specify accessors and a value or writable attribute', script);
204+
}
205+
// Define the property.
206+
const descriptor = {};
207+
if ('get' in opt_descriptor && opt_descriptor['get']) {
208+
obj.getter[name] = opt_descriptor['get'];
209+
descriptor['get'] = placeholderGet_;
210+
}
211+
if ('set' in opt_descriptor && opt_descriptor['set']) {
212+
obj.setter[name] = opt_descriptor['set'];
213+
descriptor['set'] = placeholderSet_;
214+
}
215+
if ('configurable' in opt_descriptor) {
216+
descriptor['configurable'] = opt_descriptor['configurable'];
217+
}
218+
if ('enumerable' in opt_descriptor) {
219+
descriptor['enumerable'] = opt_descriptor['enumerable'];
220+
}
221+
if ('writable' in opt_descriptor) {
222+
descriptor['writable'] = opt_descriptor['writable'];
223+
delete obj.getter[name];
224+
delete obj.setter[name];
225+
}
226+
if ('value' in opt_descriptor) {
227+
descriptor['value'] = opt_descriptor['value'];
228+
delete obj.getter[name];
229+
delete obj.setter[name];
230+
} else if (value !== Constants.VALUE_IN_DESCRIPTOR) {
231+
descriptor['value'] = value;
232+
delete obj.getter[name];
233+
delete obj.setter[name];
234+
}
235+
try {
236+
Object.defineProperty(obj.properties, name, descriptor);
237+
} catch (e) {
238+
throwException(Constants.ERROR_KEYS.TypeError, 'Cannot redefine property: ' + name, script);
239+
}
240+
// Now that the definition has suceeded, clean up any obsolete get/set funcs.
241+
if ('get' in opt_descriptor && !opt_descriptor['get']) {
242+
delete obj.getter[name];
243+
}
244+
if ('set' in opt_descriptor && !opt_descriptor['set']) {
245+
delete obj.setter[name];
246+
}
247+
} else {
248+
// Set the property.
249+
if (value === Constants.VALUE_IN_DESCRIPTOR) {
250+
throw ReferenceError('Value not specified');
251+
}
252+
// Determine the parent (possibly self) where the property is defined.
253+
let defObj: ObjectConstructor | null = obj;
254+
while (!(name in defObj.properties)) {
255+
defObj = defObj.proto;
256+
if (!defObj) {
257+
// This is a new property.
258+
defObj = obj;
259+
break;
260+
}
261+
}
262+
if (defObj.setter && defObj.setter[name]) {
263+
script.setterStep_ = true;
264+
return defObj.setter[name];
265+
}
266+
if (defObj.getter && defObj.getter[name]) {
267+
if (strict) {
268+
throwException(Constants.ERROR_KEYS.TypeError, "Cannot set property '" + name +
269+
"' of object '" + obj + "' which only has a getter", script);
270+
}
271+
} else {
272+
// No setter, simple assignment.
273+
try {
274+
obj.properties[name] = value;
275+
} catch (_e) {
276+
if (strict) {
277+
throwException(Constants.ERROR_KEYS.TypeError, "Cannot assign to read only " +
278+
"property '" + name + "' of object '" + obj + "'", script);
279+
}
280+
}
281+
}
282+
}
283+
}
96284
};
97285

98286
// 作用域构造函数

0 commit comments

Comments
 (0)