fix: 修复嵌套 prop 的内部结构不正常以及部分重构

This commit is contained in:
lihao.ylh 2021-09-23 10:27:33 +08:00
parent 69d5ab0b3a
commit d984192aeb

View File

@ -373,35 +373,30 @@ export class Prop implements IPropParent {
} }
@computed private get items(): Prop[] | null { @computed private get items(): Prop[] | null {
let _items: any = this._items; if (this._items) return this._items;
if (!_items) { let items: Prop[] | null = [];
if (this._type === 'list') { if (this._type === 'list') {
const data = this._value; const data = this._value;
const items = []; for (const item of data) {
for (const item of data) { items.push(new Prop(this, item));
items.push(new Prop(this, item));
}
_items = items;
this._maps = null;
} else if (this._type === 'map') {
const data = this._value;
const items = [];
const maps = new Map<string, Prop>();
const keys = Object.keys(data);
for (const key of keys) {
const prop = new Prop(this, data[key], key);
items.push(prop);
maps.set(key, prop);
}
_items = items;
this._maps = maps;
} else {
_items = null;
this._maps = null;
} }
this._items = _items; this._maps = null;
} else if (this._type === 'map') {
const data = this._value;
const maps = new Map<string, Prop>();
const keys = Object.keys(data);
for (const key of keys) {
const prop = new Prop(this, data[key], key);
items.push(prop);
maps.set(key, prop);
}
this._maps = maps;
} else {
items = null;
this._maps = null;
} }
return _items; this._items = items;
return this._items;
} }
@computed private get maps(): Map<string | number, Prop> | null { @computed private get maps(): Map<string | number, Prop> | null {
@ -548,7 +543,7 @@ export class Prop implements IPropParent {
} }
} }
const prop = isProp(value) ? value : new Prop(this, value, key); const prop = isProp(value) ? value : new Prop(this, value, key);
const items = this.items!; let items = this._items! || [];
if (this.type === 'list') { if (this.type === 'list') {
if (!isValidArrayIndex(key)) { if (!isValidArrayIndex(key)) {
return null; return null;
@ -558,20 +553,21 @@ export class Prop implements IPropParent {
} else { } else {
items[key] = prop; items[key] = prop;
} }
} else if (this.maps) { } else if (this.type === 'map') {
const { maps } = this; const { maps } = this;
const orig = maps.get(key); const orig = maps?.get(key);
if (orig) { if (orig) {
// replace // replace
const i = items.indexOf(orig); const i = items.indexOf(orig);
if (i > -1) { if (i > -1) {
items.splice(i, 1, prop)[0].purge(); items.splice(i, 1, prop)[0].purge();
} }
maps.set(key, prop); maps?.set(key, prop);
} else { } else {
// push // push
items.push(prop); items.push(prop);
maps.set(key, prop); this._items = items;
maps?.set(key, prop);
} }
} /* istanbul ignore next */ else { } /* istanbul ignore next */ else {
return null; return null;