Merge pull request #10902 from 2betop/fix-select910

fix: 修复 autoComplete 和 addApi 搭配时新增选项不刷新的问题
This commit is contained in:
hsm-lv 2024-09-18 10:50:27 +08:00 committed by GitHub
commit 73bf9073a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 96 additions and 12 deletions

View File

@ -239,10 +239,16 @@ export interface OptionsControlProps
onAdd?: (
idx?: number | Array<number>,
value?: any,
skipForm?: boolean
skipForm?: boolean,
callback?: (value: any) => any
) => void;
onEdit?: (value: Option, origin?: Option, skipForm?: boolean) => void;
onDelete?: (value: Option) => void;
onEdit?: (
value: Option,
origin?: Option,
skipForm?: boolean,
callback?: (value: any) => any
) => void;
onDelete?: (value: Option, callback?: (value: any) => any) => void;
}
// 自己接收的属性。
@ -899,7 +905,8 @@ export function registerOptionsControl(config: OptionsConfig) {
async handleOptionAdd(
idx: number | Array<number> = -1,
value?: any,
skipForm: boolean = false
skipForm: boolean = false,
callback?: (value: any) => any
) {
let {
addControls,
@ -1057,8 +1064,13 @@ export function registerOptionsControl(config: OptionsConfig) {
return;
}
// 如果是懒加载的,只懒加载当前节点。
if (
const ret = await callback?.(result);
if (ret === false) {
// 如果回调里面返回 false就不继续了。
return;
} else if (
// 如果是懒加载的,只懒加载当前节点。
(parent?.hasOwnProperty(deferField) && parent[deferField]) ||
parent?.defer
) {
@ -1085,7 +1097,8 @@ export function registerOptionsControl(config: OptionsConfig) {
async handleOptionEdit(
value: any,
origin: any = value,
skipForm: boolean = false
skipForm: boolean = false,
callback?: (value: any) => any
) {
let {
editControls,
@ -1209,7 +1222,12 @@ export function registerOptionsControl(config: OptionsConfig) {
return;
}
if (source && editApi) {
const ret = await callback?.(result);
if (ret === false) {
// 如果回调里面返回 false就不继续了。
return;
} else if (source && editApi) {
this.reload();
} else {
const indexes = findTreeIndex(model.options, item => item === origin);
@ -1228,7 +1246,7 @@ export function registerOptionsControl(config: OptionsConfig) {
}
@autobind
async handleOptionDelete(value: any) {
async handleOptionDelete(value: any, callback?: (value: any) => any) {
let {
deleteConfirmText,
disabled,
@ -1296,7 +1314,12 @@ export function registerOptionsControl(config: OptionsConfig) {
onDelete(ctx);
}
if (source) {
const ret = callback?.(ctx);
if (ret === false) {
// 如果回调里面返回 false就不继续了。
return;
} else if (source) {
this.reload();
} else {
const options = model.options.concat();

View File

@ -89,7 +89,7 @@ export interface OptionProps {
idx?: number | Array<number>,
value?: any,
skipForm?: boolean,
closePopOver?: () => void
callback?: () => void
) => void;
editable?: boolean;
onEdit?: (value: Option, origin?: Option, skipForm?: boolean) => void;
@ -766,7 +766,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
@autobind
handleAddClick() {
const {onAdd} = this.props;
onAdd && onAdd(undefined, undefined, false, this.close);
onAdd && onAdd();
}
@autobind

View File

@ -460,6 +460,64 @@ export default class SelectControl extends React.Component<SelectProps, any> {
}
}
@autobind
handleOptionAdd(
idx: number | Array<number> = -1,
value?: any,
skipForm: boolean = false,
callback?: (value: any) => any
) {
const {onAdd, autoComplete} = this.props;
onAdd?.(idx, value, skipForm, async () => {
callback?.(value);
if (autoComplete) {
await this.loadRemote(this.lastTerm);
return false;
}
return;
});
}
@autobind
handleOptionEdit(
value: Option,
origin?: Option,
skipForm?: boolean,
callback?: (value: any) => any
) {
const {onEdit, autoComplete} = this.props;
onEdit?.(value, origin, skipForm, async () => {
callback?.(value);
if (autoComplete) {
await this.loadRemote(this.lastTerm);
return false;
}
return;
});
}
@autobind
handleOptionDelete(value: any, callback?: (value: any) => any) {
const {onDelete, autoComplete} = this.props;
onDelete?.(value, async () => {
callback?.(value);
if (autoComplete) {
await this.loadRemote(this.lastTerm);
return false;
}
return;
});
}
@supportStatic()
render() {
let {
@ -510,6 +568,9 @@ export default class SelectControl extends React.Component<SelectProps, any> {
) : (
<Select
{...rest}
onAdd={this.handleOptionAdd}
onEdit={this.handleOptionEdit}
onDelete={this.handleOptionDelete}
className={cx(
setThemeClassName({
...this.props,