From 36f529324303d655ba2814ba5134a3107684df7b Mon Sep 17 00:00:00 2001 From: SummerOverture <740834798@qq.com> Date: Mon, 9 Aug 2021 11:35:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DSelect=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E9=83=A8=E5=88=86=E4=BA=8B=E4=BB=B6=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=AD=BE=E5=90=8D=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E3=80=82=E4=BF=AE=E5=A4=8DSelect=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=A4=9A=E9=80=89=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=8F=96?= =?UTF-8?q?=E6=B6=88=E9=80=89=E6=8B=A9=E6=97=B6=E4=B9=9F=E4=BC=9A=E8=A7=A6?= =?UTF-8?q?=E5=8F=91onSelect=E4=BA=8B=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9B=E5=90=8C=E6=97=B6=E6=B7=BB=E5=8A=A0onDeselect?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E8=AF=A5=E4=BA=8B=E4=BB=B6=E7=9A=84=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E6=89=BF=E8=BD=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/docs/zh-CN/select.md | 1 + source/components/Select/Select.tsx | 18 ++++++++++++----- .../components/Select/__tests__/index.test.js | 20 +++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/site/docs/zh-CN/select.md b/site/docs/zh-CN/select.md index 7fee24f72..b48f3dd30 100644 --- a/site/docs/zh-CN/select.md +++ b/site/docs/zh-CN/select.md @@ -665,6 +665,7 @@ render(){ | multipleSelectAllText | 在多选模式下选项全部选中时,在已选框中显示的特定的文案 | String | '全部选中' | | notFoundContent | 当下拉列表为空时显示的内容 | String \| ReactNode | '无匹配结果' | | onChange | 选中 option时,调用此函数 | (value) => Void | - | +| onDeselect | 多选模式下取消选定时的回调 | (value) => Void | - | | onMouseEnter | 鼠标移入时回调 | (value) => Void | - | | onMouseLeave | 鼠标移出时回调 | (value) => Void | - | | onPopupScroll | 下拉列表滚动时的回调 | (value) => Void | - | diff --git a/source/components/Select/Select.tsx b/source/components/Select/Select.tsx index 3c85c887f..f5123e426 100644 --- a/source/components/Select/Select.tsx +++ b/source/components/Select/Select.tsx @@ -44,11 +44,12 @@ interface SelectProps { multipleSelectAllText?: string; notFoundContent?: string | React.ReactNode; onChange?: (value?: any) => void; - onMouseEnter?: () => void; - onMouseLeave?: () => void; - onPopupScroll?: () => void; + onMouseEnter?: React.MouseEventHandler; + onMouseLeave?: React.MouseEventHandler; + onPopupScroll?: React.UIEventHandler; onSearch?: (value: any) => void; onSelect?: (value: any) => void; + onDeselect?: (value: any) => void; onVisibleChange?: (value: any) => void; placeholder?: string; placement?: 'bottomLeft' | 'bottomCenter' | 'bottomRight' | 'topLeft' | 'topCenter' | 'topRight'; @@ -126,6 +127,7 @@ class Select extends React.Component { onPopupScroll: PropTypes.func, onSearch: PropTypes.func, onSelect: PropTypes.func, + onDeselect: PropTypes.func, onVisibleChange: PropTypes.func, placeholder: PropTypes.string, placement: PropTypes.oneOf([ @@ -176,6 +178,7 @@ class Select extends React.Component { onPopupScroll: noop, onSearch: noop, onSelect: noop, + onDeselect: noop, onVisibleChange: noop, placement: 'bottomLeft', prefixCls: 'fishd-select', @@ -449,7 +452,7 @@ class Select extends React.Component { //处理 label、option的click操作 onOptionClick = (e, obj, clickInLabel) => { e && e.stopPropagation(); - const { onChange, mode, onSelect, labelInValue } = this.props; + const { onChange, mode, onSelect, onDeselect, labelInValue } = this.props; const { selectValue } = this.state; const index = selectValue.findIndex(selected => selected.key === obj.key); if (mode === 'single') { @@ -501,7 +504,12 @@ class Select extends React.Component { } } //fire onSelect event => option/label click - onSelect(obj); + if(clickInLabel) { + onDeselect(obj); + } else { + onSelect(obj); + } + }; //获取加料后的children diff --git a/source/components/Select/__tests__/index.test.js b/source/components/Select/__tests__/index.test.js index 519c60a62..94e869357 100644 --- a/source/components/Select/__tests__/index.test.js +++ b/source/components/Select/__tests__/index.test.js @@ -46,5 +46,25 @@ describe(' + 1 + 2 + 3 + + ); + + wrapper.find('.fishd-select').simulate('click'); + wrapper.find('.fishd-select-dropdown-option-item').at(0).simulate('click'); + expect(onSelect).toBeCalledWith(expect.objectContaining(triggerValue)); + wrapper.find('.fishd-select-option-clearable-option-close').simulate('click'); + expect(onSelect).toBeCalledTimes(1); + expect(onDeselect).toBeCalledWith(expect.objectContaining(triggerValue)); }) });