Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions packages/amis-core/src/renderers/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

// 自己接收的属性。
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
) {
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions packages/amis-ui/src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -764,7 +764,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
Expand Down
61 changes: 61 additions & 0 deletions packages/amis/src/renderers/Form/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
Loading