Skip to content

Commit affdfbb

Browse files
liujupingJackLian
authored andcommitted
fix: fix the problem that the props value appears when the name is 0
1 parent 8c073c3 commit affdfbb

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

packages/designer/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const jestConfig = {
1616
// testMatch: ['**/prop.test.ts'],
1717
// testMatch: ['(/tests?/.*(test))\\.[jt]s$'],
1818
// testMatch: ['**/document/node/node.add.test.ts'],
19+
// testMatch: ['**/setting-field.test.ts'],
1920
transformIgnorePatterns: [
2021
`/node_modules/(?!${esModules})/`,
2122
],

packages/designer/src/designer/setting/setting-prop-entry.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { ISettingTopEntry } from './setting-top-entry';
99
import { ISettingField, isSettingField } from './setting-field';
1010

1111
export interface ISettingPropEntry extends ISettingEntry {
12-
get props(): ISettingTopEntry;
13-
1412
readonly isGroup: boolean;
1513

14+
get props(): ISettingTopEntry;
15+
1616
get name(): string | number | undefined;
1717

1818
valueChange(options: IPublicTypeSetValueOptions): void;
@@ -75,7 +75,7 @@ export class SettingPropEntry implements ISettingPropEntry {
7575

7676
@computed get path() {
7777
const path = this.parent.path.slice();
78-
if (this.type === 'field' && this.name) {
78+
if (this.type === 'field' && this.name?.toString()) {
7979
path.push(this.name);
8080
}
8181
return path;
@@ -191,7 +191,7 @@ export class SettingPropEntry implements ISettingPropEntry {
191191
*/
192192
getValue(): any {
193193
let val: any;
194-
if (this.type === 'field' && this.name) {
194+
if (this.type === 'field' && this.name?.toString()) {
195195
val = this.parent.getPropValue(this.name);
196196
}
197197
const { getValue } = this.extraProps;
@@ -209,7 +209,7 @@ export class SettingPropEntry implements ISettingPropEntry {
209209
setValue(val: any, isHotValue?: boolean, force?: boolean, extraOptions?: IPublicTypeSetValueOptions) {
210210
const oldValue = this.getValue();
211211
if (this.type === 'field') {
212-
this.name && this.parent.setPropValue(this.name, val);
212+
this.name?.toString() && this.parent.setPropValue(this.name, val);
213213
}
214214

215215
const { setValue } = this.extraProps;
@@ -233,7 +233,7 @@ export class SettingPropEntry implements ISettingPropEntry {
233233
*/
234234
clearValue() {
235235
if (this.type === 'field') {
236-
this.name && this.parent.clearPropValue(this.name);
236+
this.name?.toString() && this.parent.clearPropValue(this.name);
237237
}
238238
const { setValue } = this.extraProps;
239239
if (setValue) {
@@ -395,6 +395,6 @@ export class SettingPropEntry implements ISettingPropEntry {
395395
}
396396

397397
internalToShellField(): IPublicModelSettingField {
398-
return this.designer!.shellModelFactory.createSettingField(this);;
398+
return this.designer!.shellModelFactory.createSettingField(this);
399399
}
400400
}

packages/designer/src/document/node/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface IBaseNode<Schema extends IPublicTypeNodeSchema = IPublicTypeNod
155155

156156
getChildren(): INodeChildren | null;
157157

158-
clearPropValue(path: string): void;
158+
clearPropValue(path: string | number): void;
159159

160160
setProps(props?: IPublicTypePropsMap | IPublicTypePropsList | Props | null): void;
161161

packages/designer/tests/designer/setting/setting-field.test.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ describe('setting-field 测试', () => {
6565

6666
it('常规方法', () => {
6767
// 普通 field
68-
const settingEntry = mockNode.settingEntry as SettingTopEntry;
69-
const field = settingEntry.get('behavior') as SettingField;
68+
const settingEntry = mockNode.settingEntry;
69+
const field = settingEntry.get('behavior');
7070
expect(field.title).toBe('默认状态');
7171
expect(field.expanded).toBeTruthy();
7272
field.setExpanded(false);
@@ -103,24 +103,24 @@ describe('setting-field 测试', () => {
103103
expect(nonExistingField.setter).toBeNull();
104104

105105
// group 类型的 field
106-
const groupField = settingEntry.get('groupkgzzeo41') as SettingField;
106+
const groupField = settingEntry.get('groupkgzzeo41');
107107
expect(groupField.items).toEqual([]);
108108

109109
// 有子节点的 field
110-
const objField = settingEntry.get('obj') as SettingField;
110+
const objField = settingEntry.get('obj');
111111
expect(objField.items).toHaveLength(3);
112112
expect(objField.getItems()).toHaveLength(3);
113113
expect(objField.getItems(x => x.name === 'a')).toHaveLength(1);
114114
objField.purge();
115115
expect(objField.items).toHaveLength(0);
116-
const objAField = settingEntry.get('obj.a') as SettingField;
116+
const objAField = settingEntry.get('obj.a');
117117
expect(objAField.setter).toBe('StringSetter');
118118
});
119119

120120
it('setValue / getValue / setHotValue / getHotValue', () => {
121121
// 获取已有的 prop
122122
const settingEntry = mockNode.settingEntry as SettingTopEntry;
123-
const field = settingEntry.get('behavior') as SettingField;
123+
const field = settingEntry.get('behavior');
124124

125125
// 会读取 extraProps.defaultValue
126126
expect(field.getHotValue()).toBe('NORMAL');
@@ -140,11 +140,37 @@ describe('setting-field 测试', () => {
140140

141141
// dirty fix list setter
142142
field.setHotValue([{ __sid__: 1 }]);
143+
144+
// 数组的 field
145+
const arrField = settingEntry.get('arr');
146+
const subArrField = arrField.createField({
147+
name: 0,
148+
title: 'sub',
149+
});
150+
const subArrField02 = arrField.createField({
151+
name: 1,
152+
title: 'sub',
153+
});
154+
const subArrField03 = arrField.createField({
155+
name: '2',
156+
title: 'sub',
157+
});
158+
subArrField.setValue({name: '1'});
159+
expect(subArrField.path).toEqual(['arr', 0]);
160+
expect(subArrField02.path).toEqual(['arr', 1]);
161+
subArrField02.setValue({name: '2'});
162+
expect(subArrField.getValue()).toEqual({name: '1'});
163+
expect(arrField.getHotValue()).toEqual([{name: '1'}, {name: '2'}]);
164+
subArrField.clearValue();
165+
expect(subArrField.getValue()).toBeUndefined();
166+
expect(arrField.getHotValue()).toEqual([undefined, {name: '2'}]);
167+
subArrField03.setValue({name: '3'});
168+
expect(arrField.getHotValue()).toEqual([undefined, {name: '2'}, {name: '3'}]);
143169
});
144170

145171
it('onEffect', async () => {
146172
const settingEntry = mockNode.settingEntry as SettingTopEntry;
147-
const field = settingEntry.get('behavior') as SettingField;
173+
const field = settingEntry.get('behavior');
148174

149175
const mockFn = jest.fn();
150176

packages/types/src/shell/model/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export interface IBaseModelNode<
299299
* @param path 属性路径,支持 a / a.b / a.0 等格式
300300
* @param createIfNone 如果不存在,是否新建,默认为 true
301301
*/
302-
getProp(path: string, createIfNone?: boolean): Prop | null;
302+
getProp(path: string | number, createIfNone?: boolean): Prop | null;
303303

304304
/**
305305
* 获取指定 path 的属性模型实例值
@@ -336,7 +336,7 @@ export interface IBaseModelNode<
336336
* @param path 属性路径,支持 a / a.b / a.0 等格式
337337
* @param value 值
338338
*/
339-
setPropValue(path: string, value: IPublicTypeCompositeValue): void;
339+
setPropValue(path: string | number, value: IPublicTypeCompositeValue): void;
340340

341341
/**
342342
* 设置指定 path 的属性模型实例值

0 commit comments

Comments
 (0)