fix: fix the problem that the props value appears when the name is 0

This commit is contained in:
liujuping 2023-03-24 15:45:16 +08:00 committed by 林熠
parent 8c073c3c95
commit affdfbbb1c
5 changed files with 44 additions and 17 deletions

View File

@ -16,6 +16,7 @@ const jestConfig = {
// testMatch: ['**/prop.test.ts'], // testMatch: ['**/prop.test.ts'],
// testMatch: ['(/tests?/.*(test))\\.[jt]s$'], // testMatch: ['(/tests?/.*(test))\\.[jt]s$'],
// testMatch: ['**/document/node/node.add.test.ts'], // testMatch: ['**/document/node/node.add.test.ts'],
// testMatch: ['**/setting-field.test.ts'],
transformIgnorePatterns: [ transformIgnorePatterns: [
`/node_modules/(?!${esModules})/`, `/node_modules/(?!${esModules})/`,
], ],

View File

@ -9,10 +9,10 @@ import { ISettingTopEntry } from './setting-top-entry';
import { ISettingField, isSettingField } from './setting-field'; import { ISettingField, isSettingField } from './setting-field';
export interface ISettingPropEntry extends ISettingEntry { export interface ISettingPropEntry extends ISettingEntry {
get props(): ISettingTopEntry;
readonly isGroup: boolean; readonly isGroup: boolean;
get props(): ISettingTopEntry;
get name(): string | number | undefined; get name(): string | number | undefined;
valueChange(options: IPublicTypeSetValueOptions): void; valueChange(options: IPublicTypeSetValueOptions): void;
@ -75,7 +75,7 @@ export class SettingPropEntry implements ISettingPropEntry {
@computed get path() { @computed get path() {
const path = this.parent.path.slice(); const path = this.parent.path.slice();
if (this.type === 'field' && this.name) { if (this.type === 'field' && this.name?.toString()) {
path.push(this.name); path.push(this.name);
} }
return path; return path;
@ -191,7 +191,7 @@ export class SettingPropEntry implements ISettingPropEntry {
*/ */
getValue(): any { getValue(): any {
let val: any; let val: any;
if (this.type === 'field' && this.name) { if (this.type === 'field' && this.name?.toString()) {
val = this.parent.getPropValue(this.name); val = this.parent.getPropValue(this.name);
} }
const { getValue } = this.extraProps; const { getValue } = this.extraProps;
@ -209,7 +209,7 @@ export class SettingPropEntry implements ISettingPropEntry {
setValue(val: any, isHotValue?: boolean, force?: boolean, extraOptions?: IPublicTypeSetValueOptions) { setValue(val: any, isHotValue?: boolean, force?: boolean, extraOptions?: IPublicTypeSetValueOptions) {
const oldValue = this.getValue(); const oldValue = this.getValue();
if (this.type === 'field') { if (this.type === 'field') {
this.name && this.parent.setPropValue(this.name, val); this.name?.toString() && this.parent.setPropValue(this.name, val);
} }
const { setValue } = this.extraProps; const { setValue } = this.extraProps;
@ -233,7 +233,7 @@ export class SettingPropEntry implements ISettingPropEntry {
*/ */
clearValue() { clearValue() {
if (this.type === 'field') { if (this.type === 'field') {
this.name && this.parent.clearPropValue(this.name); this.name?.toString() && this.parent.clearPropValue(this.name);
} }
const { setValue } = this.extraProps; const { setValue } = this.extraProps;
if (setValue) { if (setValue) {
@ -395,6 +395,6 @@ export class SettingPropEntry implements ISettingPropEntry {
} }
internalToShellField(): IPublicModelSettingField { internalToShellField(): IPublicModelSettingField {
return this.designer!.shellModelFactory.createSettingField(this);; return this.designer!.shellModelFactory.createSettingField(this);
} }
} }

View File

@ -155,7 +155,7 @@ export interface IBaseNode<Schema extends IPublicTypeNodeSchema = IPublicTypeNod
getChildren(): INodeChildren | null; getChildren(): INodeChildren | null;
clearPropValue(path: string): void; clearPropValue(path: string | number): void;
setProps(props?: IPublicTypePropsMap | IPublicTypePropsList | Props | null): void; setProps(props?: IPublicTypePropsMap | IPublicTypePropsList | Props | null): void;

View File

@ -65,8 +65,8 @@ describe('setting-field 测试', () => {
it('常规方法', () => { it('常规方法', () => {
// 普通 field // 普通 field
const settingEntry = mockNode.settingEntry as SettingTopEntry; const settingEntry = mockNode.settingEntry;
const field = settingEntry.get('behavior') as SettingField; const field = settingEntry.get('behavior');
expect(field.title).toBe('默认状态'); expect(field.title).toBe('默认状态');
expect(field.expanded).toBeTruthy(); expect(field.expanded).toBeTruthy();
field.setExpanded(false); field.setExpanded(false);
@ -103,24 +103,24 @@ describe('setting-field 测试', () => {
expect(nonExistingField.setter).toBeNull(); expect(nonExistingField.setter).toBeNull();
// group 类型的 field // group 类型的 field
const groupField = settingEntry.get('groupkgzzeo41') as SettingField; const groupField = settingEntry.get('groupkgzzeo41');
expect(groupField.items).toEqual([]); expect(groupField.items).toEqual([]);
// 有子节点的 field // 有子节点的 field
const objField = settingEntry.get('obj') as SettingField; const objField = settingEntry.get('obj');
expect(objField.items).toHaveLength(3); expect(objField.items).toHaveLength(3);
expect(objField.getItems()).toHaveLength(3); expect(objField.getItems()).toHaveLength(3);
expect(objField.getItems(x => x.name === 'a')).toHaveLength(1); expect(objField.getItems(x => x.name === 'a')).toHaveLength(1);
objField.purge(); objField.purge();
expect(objField.items).toHaveLength(0); expect(objField.items).toHaveLength(0);
const objAField = settingEntry.get('obj.a') as SettingField; const objAField = settingEntry.get('obj.a');
expect(objAField.setter).toBe('StringSetter'); expect(objAField.setter).toBe('StringSetter');
}); });
it('setValue / getValue / setHotValue / getHotValue', () => { it('setValue / getValue / setHotValue / getHotValue', () => {
// 获取已有的 prop // 获取已有的 prop
const settingEntry = mockNode.settingEntry as SettingTopEntry; const settingEntry = mockNode.settingEntry as SettingTopEntry;
const field = settingEntry.get('behavior') as SettingField; const field = settingEntry.get('behavior');
// 会读取 extraProps.defaultValue // 会读取 extraProps.defaultValue
expect(field.getHotValue()).toBe('NORMAL'); expect(field.getHotValue()).toBe('NORMAL');
@ -140,11 +140,37 @@ describe('setting-field 测试', () => {
// dirty fix list setter // dirty fix list setter
field.setHotValue([{ __sid__: 1 }]); field.setHotValue([{ __sid__: 1 }]);
// 数组的 field
const arrField = settingEntry.get('arr');
const subArrField = arrField.createField({
name: 0,
title: 'sub',
});
const subArrField02 = arrField.createField({
name: 1,
title: 'sub',
});
const subArrField03 = arrField.createField({
name: '2',
title: 'sub',
});
subArrField.setValue({name: '1'});
expect(subArrField.path).toEqual(['arr', 0]);
expect(subArrField02.path).toEqual(['arr', 1]);
subArrField02.setValue({name: '2'});
expect(subArrField.getValue()).toEqual({name: '1'});
expect(arrField.getHotValue()).toEqual([{name: '1'}, {name: '2'}]);
subArrField.clearValue();
expect(subArrField.getValue()).toBeUndefined();
expect(arrField.getHotValue()).toEqual([undefined, {name: '2'}]);
subArrField03.setValue({name: '3'});
expect(arrField.getHotValue()).toEqual([undefined, {name: '2'}, {name: '3'}]);
}); });
it('onEffect', async () => { it('onEffect', async () => {
const settingEntry = mockNode.settingEntry as SettingTopEntry; const settingEntry = mockNode.settingEntry as SettingTopEntry;
const field = settingEntry.get('behavior') as SettingField; const field = settingEntry.get('behavior');
const mockFn = jest.fn(); const mockFn = jest.fn();

View File

@ -299,7 +299,7 @@ export interface IBaseModelNode<
* @param path a / a.b / a.0 * @param path a / a.b / a.0
* @param createIfNone true * @param createIfNone true
*/ */
getProp(path: string, createIfNone?: boolean): Prop | null; getProp(path: string | number, createIfNone?: boolean): Prop | null;
/** /**
* path * path
@ -336,7 +336,7 @@ export interface IBaseModelNode<
* @param path a / a.b / a.0 * @param path a / a.b / a.0
* @param value * @param value
*/ */
setPropValue(path: string, value: IPublicTypeCompositeValue): void; setPropValue(path: string | number, value: IPublicTypeCompositeValue): void;
/** /**
* path * path