优化搜索相关

This commit is contained in:
2betop 2020-05-14 11:19:39 +08:00
parent 2f22d7621b
commit d4155c02fb
3 changed files with 61 additions and 43 deletions

View File

@ -121,24 +121,26 @@ export class Transfer extends React.Component<TransferPorps, TransferState> {
}
lazySearch = debounce(
async (text: string) => {
const onSearch = this.props.onSearch!;
let result = await onSearch(
text,
(cancel: () => void) => (this.cancelSearch = cancel)
);
(text: string) => {
(async (text: string) => {
const onSearch = this.props.onSearch!;
let result = await onSearch(
text,
(cancelExecutor: () => void) => (this.cancelSearch = cancelExecutor)
);
if (this.unmounted) {
return;
}
if (this.unmounted) {
return;
}
if (!Array.isArray(result)) {
throw new Error('onSearch 需要返回数组');
}
if (!Array.isArray(result)) {
throw new Error('onSearch 需要返回数组');
}
this.setState({
searchResult: result
});
this.setState({
searchResult: result
});
})(text).catch(e => console.error(e));
},
250,
{

View File

@ -59,7 +59,7 @@ export interface OptionsControlProps extends FormControlProps, OptionProps {
) => void;
onToggleAll: () => void;
selectedOptions: Array<Option>;
setOptions: (value: Array<any>) => void;
setOptions: (value: Array<any>, skipNormalize?: boolean) => void;
setLoading: (value: boolean) => void;
reloadOptions: () => void;
creatable?: boolean;
@ -466,9 +466,12 @@ export function registerOptionsControl(config: OptionsConfig) {
}
@autobind
setOptions(options: Array<any>) {
setOptions(options: Array<any>, skipNormalize = false) {
const formItem = this.props.formItem as IFormItemStore;
formItem && formItem.setOptions(normalizeOptions(options || []));
formItem &&
formItem.setOptions(
skipNormalize ? options : normalizeOptions(options || [])
);
}
@autobind
@ -797,7 +800,7 @@ export function registerOptionsControl(config: OptionsConfig) {
}
export function OptionsControl(config: OptionsBasicConfig) {
return function<T extends React.ComponentType<OptionsControlProps>>(
return function <T extends React.ComponentType<OptionsControlProps>>(
component: T
): T {
const renderer = registerOptionsControl({

View File

@ -63,7 +63,7 @@ export class TransferRenderer extends React.Component<TransferProps> {
}
}
newOptions.length > options.length && setOptions(newOptions);
newOptions.length > options.length && setOptions(newOptions, true);
onChange(newValue);
}
@ -73,35 +73,48 @@ export class TransferRenderer extends React.Component<TransferProps> {
}
@autobind
async handleSearch(term: string) {
async handleSearch(term: string, cancelExecutor: Function) {
const {searchApi, options, labelField, valueField, env, data} = this.props;
if (searchApi) {
const payload = await env.fetcher(searchApi, createObject(data, {term}));
try {
const payload = await env.fetcher(
searchApi,
createObject(data, {term}),
{
cancelExecutor
}
);
if (!payload.ok) {
env.notify('error', payload.msg || '搜索请求异常');
return [];
}
const result = payload.data.options || payload.data.items || payload.data;
if (!Array.isArray(result)) {
env.notify('error', '期望接口返回数组信息');
return [];
}
return result.map(item => {
let resolved: any = null;
if (Array.isArray(options)) {
resolved = find(
options,
optionValueCompare(item[valueField || 'value'], valueField)
);
if (!payload.ok) {
throw new Error(payload.msg || '搜索请求异常');
}
return resolved || item;
});
const result =
payload.data.options || payload.data.items || payload.data;
if (!Array.isArray(result)) {
throw new Error('期望接口返回数组信息');
}
return result.map(item => {
let resolved: any = null;
if (Array.isArray(options)) {
resolved = find(
options,
optionValueCompare(item[valueField || 'value'], valueField)
);
}
return resolved || item;
});
} catch (e) {
if (!env.isCancel(e)) {
env.notify('error', e.message);
}
return [];
}
} else if (term) {
const regexp = string2regExp(term);