From 91d99b15fd487fd36fdaf28bbffde038946e7ff9 Mon Sep 17 00:00:00 2001 From: 2betop <2698393+2betop@users.noreply.github.com> Date: Tue, 10 Sep 2024 20:00:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20autoComplete=20?= =?UTF-8?q?=E5=92=8C=20addApi=20=E6=90=AD=E9=85=8D=E6=97=B6=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E9=80=89=E9=A1=B9=E4=B8=8D=E5=88=B7=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-core/src/renderers/Options.tsx | 43 ++++++++++---- packages/amis-ui/src/components/Select.tsx | 4 +- packages/amis/src/renderers/Form/Select.tsx | 61 ++++++++++++++++++++ 3 files changed, 96 insertions(+), 12 deletions(-) diff --git a/packages/amis-core/src/renderers/Options.tsx b/packages/amis-core/src/renderers/Options.tsx index 08a42ebe9..aec8770dc 100644 --- a/packages/amis-core/src/renderers/Options.tsx +++ b/packages/amis-core/src/renderers/Options.tsx @@ -239,10 +239,16 @@ export interface OptionsControlProps onAdd?: ( idx?: number | Array, 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 = -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(); diff --git a/packages/amis-ui/src/components/Select.tsx b/packages/amis-ui/src/components/Select.tsx index 08ebb3669..5934f895e 100644 --- a/packages/amis-ui/src/components/Select.tsx +++ b/packages/amis-ui/src/components/Select.tsx @@ -89,7 +89,7 @@ export interface OptionProps { idx?: number | Array, value?: any, skipForm?: boolean, - closePopOver?: () => void + callback?: () => void ) => void; editable?: boolean; onEdit?: (value: Option, origin?: Option, skipForm?: boolean) => void; @@ -764,7 +764,7 @@ export class Select extends React.Component { @autobind handleAddClick() { const {onAdd} = this.props; - onAdd && onAdd(undefined, undefined, false, this.close); + onAdd && onAdd(); } @autobind diff --git a/packages/amis/src/renderers/Form/Select.tsx b/packages/amis/src/renderers/Form/Select.tsx index 07853b97f..55162f03b 100644 --- a/packages/amis/src/renderers/Form/Select.tsx +++ b/packages/amis/src/renderers/Form/Select.tsx @@ -460,6 +460,64 @@ export default class SelectControl extends React.Component { } } + @autobind + handleOptionAdd( + idx: number | Array = -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 { ) : (