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

feat: 级联选择器增加选中项标题事件动作变量 #11325

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions packages/amis-editor/src/plugin/Form/NestedSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ export class NestedSelectControlPlugin extends BasePlugin {
type: 'object',
title: '数据',
properties: {
value
value,
label: {type: 'string', title: '选中项标题'}
}
}
}
Expand All @@ -164,7 +165,8 @@ export class NestedSelectControlPlugin extends BasePlugin {
type: 'object',
title: '数据',
properties: {
value
value,
label: {type: 'string', title: '选中项标题'}
}
}
}
Expand All @@ -187,7 +189,8 @@ export class NestedSelectControlPlugin extends BasePlugin {
type: 'object',
title: '数据',
properties: {
value
value,
label: {type: 'string', title: '选中项标题'}
}
}
}
Expand Down
46 changes: 19 additions & 27 deletions packages/amis/src/renderers/Form/NestedSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,32 +210,18 @@ export default class NestedSelectControl extends React.Component<
}

async removeItem(index: number, e?: React.MouseEvent<HTMLElement>) {
let {
onChange,
selectedOptions,
joinValues,
valueField,
extractValue,
delimiter,
value
} = this.props;
let {onChange, selectedOptions} = this.props;

e && e.stopPropagation();

selectedOptions.splice(index, 1);

if (joinValues) {
value = (selectedOptions as Options)
.map(item => item[valueField || 'value'])
.join(delimiter || ',');
} else if (extractValue) {
value = (selectedOptions as Options).map(
item => item[valueField || 'value']
);
}
const value = this.getValue();
const label = this.getValue('label');

const isPrevented = await this.dispatchEvent('change', {
value
value,
label
});
isPrevented || onChange(value);
}
Expand Down Expand Up @@ -315,7 +301,8 @@ export default class NestedSelectControl extends React.Component<
}

const isPrevented = await this.dispatchEvent('change', {
value
value,
label: option.label
});
isPrevented || onChange(value);
isPrevented || this.handleResultClear();
Expand Down Expand Up @@ -476,24 +463,25 @@ export default class NestedSelectControl extends React.Component<
}

@autobind
getValue() {
getValue(propName?: string) {
let {
selectedOptions,
joinValues,
valueField,
labelField,
extractValue,
delimiter,
value
} = this.props;
const attribute =
propName === 'label' ? labelField || 'label' : valueField || 'value';

if (joinValues) {
value = (selectedOptions as Options)
.map(item => item[valueField || 'value'])
.map(item => item[attribute])
.join(delimiter || ',');
} else if (extractValue) {
value = (selectedOptions as Options).map(
item => item[valueField || 'value']
);
value = (selectedOptions as Options).map(item => item[attribute]);
}

return value;
Expand All @@ -504,14 +492,16 @@ export default class NestedSelectControl extends React.Component<
const {onFocus, disabled} = this.props;

const value = this.getValue();
const label = this.getValue('label');

if (!disabled && !this.state.isOpened) {
this.setState({
isFocused: true
});

const isPrevented = await this.dispatchEvent('focus', {
value
value,
label
});
isPrevented || (onFocus && onFocus(e));
}
Expand All @@ -522,13 +512,15 @@ export default class NestedSelectControl extends React.Component<
const {onBlur} = this.props;

const value = this.getValue();
const label = this.getValue('label');

this.setState({
isFocused: false
});

const isPrevented = await this.dispatchEvent('blur', {
value
value,
label
});
isPrevented || (onBlur && onBlur(e));
}
Expand Down
Loading