feat: get SettingField instead of SettingPropEntry for compatibility

This commit is contained in:
shuaige.zsg 2020-06-08 20:57:55 +08:00
parent 8961cfcb89
commit 2787a12d83
2 changed files with 15 additions and 13 deletions

View File

@ -37,7 +37,7 @@ export class SettingField extends SettingPropEntry implements SettingEntry {
this._expanded = value;
}
constructor(readonly parent: SettingEntry, config: FieldConfig) {
constructor(readonly parent: SettingEntry, config: FieldConfig, settingFieldCollector?: (name: string | number, field: SettingField) => void) {
super(parent, config.name, config.type);
const { title, items, setter, extraProps, ...rest } = config;
@ -52,7 +52,9 @@ export class SettingField extends SettingPropEntry implements SettingEntry {
// initial items
if (this.type === 'group' && items) {
this.initItems(items);
this.initItems(items, settingFieldCollector);
} else if (settingFieldCollector && config.name) {
settingFieldCollector(config.name, this);
}
// compatiable old config
@ -65,12 +67,12 @@ export class SettingField extends SettingPropEntry implements SettingEntry {
return this._items;
}
private initItems(items: Array<FieldConfig | CustomView>) {
private initItems(items: Array<FieldConfig | CustomView>, settingFieldCollector?: { (name: string | number, field: SettingField): void; (name: string, field: SettingField): void; }) {
this._items = items.map((item) => {
if (isCustomView(item)) {
return item;
}
return new SettingField(this, item);
return new SettingField(this, item, settingFieldCollector);
});
}

View File

@ -20,6 +20,7 @@ export class SettingTopEntry implements SettingEntry {
private _items: Array<SettingField | CustomView> = [];
private _componentMeta: ComponentMeta | null = null;
private _isSame: boolean = true;
private _settingFieldMap: { [prop: string]: SettingField } = {};
readonly path = [];
readonly top = this;
readonly parent = this;
@ -95,13 +96,19 @@ export class SettingTopEntry implements SettingEntry {
}
private setupItems() {
console.log('set')
if (this.componentMeta) {
const settingFieldMap: { [prop: string]: SettingField } = {};
const settingFieldCollector = (name: string | number, field: SettingField) => {
settingFieldMap[name] = field;
}
this._items = this.componentMeta.configure.map((item) => {
if (isCustomView(item)) {
return item;
}
return new SettingField(this, item as any);
return new SettingField(this, item as any, settingFieldCollector);
});
this._settingFieldMap = settingFieldMap;
}
}
@ -124,14 +131,7 @@ export class SettingTopEntry implements SettingEntry {
*
*/
get(propName: string | number): SettingPropEntry {
const matched = this.items.find(item => {
if (isSettingField(item)) {
// TODO: thinkof use name or path?
return item.name === propName;
}
return false;
}) as SettingPropEntry;
return matched || (new SettingPropEntry(this, propName));
return this._settingFieldMap[propName] || (new SettingPropEntry(this, propName));
}
/**