feat:事件干预逻辑

This commit is contained in:
xujiahao01 2022-01-28 17:42:45 +08:00
parent 8d46a49195
commit fc1d3b9be3
2 changed files with 31 additions and 12 deletions

View File

@ -151,16 +151,23 @@ export default class SelectControl extends React.Component<SelectProps, any> {
this.input && this.input.focus();
}
dispatchEvent(eventName: SelectRendererEvent, e: any = {}) {
async dispatchEvent(eventName: SelectRendererEvent, e: any = {}) {
const event = 'on' + eventName.charAt(0).toUpperCase() + eventName.slice(1);
const {dispatchEvent, options} = this.props;
dispatchEvent(eventName, createObject(e, {
options
}));
// 触发渲染器事件
const rendererEvent = await dispatchEvent(
eventName,
createObject(e, {
options
})
);
if (rendererEvent?.prevented) {
return;
}
this.props[event](e);
}
changeValue(value: Option | Array<Option> | string | void) {
async changeValue(value: Option | Array<Option> | string | void) {
const {
joinValues,
extractValue,
@ -215,10 +222,14 @@ export default class SelectControl extends React.Component<SelectProps, any> {
// 不设置没法回显
additonalOptions.length && setOptions(options.concat(additonalOptions));
dispatchEvent('change', {
const rendererEvent = await dispatchEvent('change', {
value: newValue,
options
});
if (rendererEvent?.prevented) {
return;
}
onChange(newValue);
}

View File

@ -128,7 +128,7 @@ export class BaseTransferRenderer<
T extends OptionsControlProps = BaseTransferProps
> extends React.Component<T> {
@autobind
handleChange(value: Array<Option> | Option, optionModified?: boolean) {
async handleChange(value: Array<Option> | Option, optionModified?: boolean) {
const {
onChange,
joinValues,
@ -179,11 +179,19 @@ export class BaseTransferRenderer<
(newOptions.length > options.length || optionModified) &&
setOptions(newOptions, true);
dispatchEvent('change', {
value: newValue,
options
});
// 触发渲染器事件
const rendererEvent = await dispatchEvent(
'change',
{
value: newValue,
options
}
);
if (rendererEvent?.prevented) {
return;
}
onChange(newValue);
}