Skip to content

Commit

Permalink
* picker: fix onSelect, onDeselect, onClear not work.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Apr 8, 2024
1 parent c982270 commit 24e8ad3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/picker/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ onPageUpdate(() => {
defaultValue: 'banana',
placeholder: '请选择你的最爱',
searchHint: '搜索选项',
onSelect: (values) => console.log('onSelect', values),
onDeselect: (values) => console.log('onDeselect', values),
});
console.log('> singlePicker', singlePicker);

Expand All @@ -154,6 +156,8 @@ onPageUpdate(() => {
defaultValue: 'banana,orange',
placeholder: '请选择你的最爱',
toolbar: true,
onSelect: (values) => console.log('onSelect', values),
onDeselect: (values) => console.log('onDeselect', values),
});
console.log('> multiPicker', multiPicker);

Expand Down
25 changes: 25 additions & 0 deletions lib/picker/src/component/picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,31 @@ export class Picker<S extends PickerState = PickerState, O extends PickerOptions
super.componentWillUnmount();
}

protected _handleChange(value: string, oldValue: string) {
super._handleChange(value, oldValue);
if (value !== oldValue) {
const {onDeselect, onSelect, onClear, multiple} = this.props;
const oldValueList = this.formatValueList(oldValue);
const valueList = this.valueList;
if (onClear && !valueList.length && oldValueList.length) {
onClear.call(this);
}
if (onDeselect) {
const deselectedList = oldValueList.filter(x => !valueList.includes(x));
if (deselectedList.length) {
onDeselect.call(this, multiple ? deselectedList : deselectedList[0]);
}
}
if (onSelect) {
const selectedList = valueList.filter(x => !oldValueList.includes(x));
if (selectedList.length) {
onSelect.call(this, multiple ? selectedList : selectedList[0]);
}
}
}

}

protected _getTriggerProps(props: RenderableProps<O>, state: Readonly<S>): PickerSelectProps<S> {
return {
...super._getTriggerProps(props, state),
Expand Down

0 comments on commit 24e8ad3

Please sign in to comment.