fix: 修复 autoComplete 和 addApi 搭配时新增选项不刷新的问题

This commit is contained in:
2betop 2024-09-10 20:00:19 +08:00
parent acfb3ff1d1
commit 91d99b15fd
3 changed files with 96 additions and 12 deletions

View File

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

View File

@ -89,7 +89,7 @@ export interface OptionProps {
idx?: number | Array<number>, idx?: number | Array<number>,
value?: any, value?: any,
skipForm?: boolean, skipForm?: boolean,
closePopOver?: () => void callback?: () => void
) => void; ) => void;
editable?: boolean; editable?: boolean;
onEdit?: (value: Option, origin?: Option, skipForm?: boolean) => void; onEdit?: (value: Option, origin?: Option, skipForm?: boolean) => void;
@ -764,7 +764,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
@autobind @autobind
handleAddClick() { handleAddClick() {
const {onAdd} = this.props; const {onAdd} = this.props;
onAdd && onAdd(undefined, undefined, false, this.close); onAdd && onAdd();
} }
@autobind @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() @supportStatic()
render() { render() {
let { let {
@ -510,6 +568,9 @@ export default class SelectControl extends React.Component<SelectProps, any> {
) : ( ) : (
<Select <Select
{...rest} {...rest}
onAdd={this.handleOptionAdd}
onEdit={this.handleOptionEdit}
onDelete={this.handleOptionDelete}
className={cx( className={cx(
setThemeClassName({ setThemeClassName({
...this.props, ...this.props,