fix: ConditionBuilder 配置远程拉取并有初始值时options渲染失败的问题 (#3584)

This commit is contained in:
Allen 2022-02-15 16:23:26 +08:00 committed by GitHub
parent 85dd91640f
commit 2a5fbc588c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,7 +18,7 @@ export interface ConditionFieldProps extends ThemeProps, LocaleProps {
}
export interface ConditionFieldState {
options: Array<any>;
searchText: string;
}
const option2value = (item: any) => item.name;
@ -30,45 +30,50 @@ export class ConditionField extends React.Component<
constructor(props: ConditionFieldProps) {
super(props);
this.state = {
options: props.options
searchText: ''
};
this.onSearch = this.onSearch.bind(this);
this.filterOptions = this.filterOptions.bind(this);
}
onSearch(text: string) {
let txt = text.toLowerCase();
this.setState({
options: this.props.options
.map((item: any) => {
if (item.children) {
let children = item.children.filter((child: any) => {
return (
child.name.toLowerCase().includes(txt) ||
child.label.toLowerCase().includes(txt)
);
});
return children.length > 0
? Object.assign({}, item, {children}) // 需要copy一份防止覆盖原始数据
: false;
} else {
return item.name.toLowerCase().includes(txt) ||
item.label.toLowerCase().includes(txt)
? item
: false;
}
})
.filter((item: any) => {
return !!item;
})
});
this.setState({searchText: txt});
}
filterOptions(options: any[]) {
const txt = this.state.searchText;
if (!txt) {
return this.props.options;
}
return options
.map((item: any) => {
if (item.children) {
let children = item.children.filter((child: any) => {
return (
child.name.toLowerCase().includes(txt) ||
child.label.toLowerCase().includes(txt)
);
});
return children.length > 0
? Object.assign({}, item, {children}) // 需要copy一份防止覆盖原始数据
: false;
} else {
return item.name.toLowerCase().includes(txt) ||
item.label.toLowerCase().includes(txt)
? item
: false;
}
})
.filter((item: any) => {
return !!item;
});
}
// 选了值还原options
onPopClose(e: React.MouseEvent, onClose: () => void) {
this.setState({
options: this.props.options
});
this.setState({searchText: ''});
onClose();
}
@ -83,6 +88,7 @@ export class ConditionField extends React.Component<
translate: __,
searchable
} = this.props;
return (
<PopOverContainer
popOverRender={({onClose}) => (
@ -93,7 +99,7 @@ export class ConditionField extends React.Component<
<ListSelection
multiple={false}
onClick={e => this.onPopClose(e, onClose)}
options={this.state.options}
options={this.filterOptions(this.props.options)}
value={[value]}
option2value={option2value}
onChange={(value: any) =>