From ea9482128c7002bc5544d8bacb3fe47e9dba65ea Mon Sep 17 00:00:00 2001 From: czgu Date: Tue, 8 Dec 2020 12:21:51 -0500 Subject: [PATCH] Fix selection behavior (#341) https://github.com/pinterest/datahub/pull/341 --- datahub/webapp/__tests__/ui/Select.test.js | 43 ++++++++++++++++++- .../components/AppAdmin/AdminQueryEngine.tsx | 8 +++- .../DataTableTags/TableTagSelect.tsx | 1 + datahub/webapp/ui/Select/Select.tsx | 14 ++---- .../SimpleReactSelect/SimpleReactSelect.tsx | 17 +++++++- 5 files changed, 69 insertions(+), 14 deletions(-) diff --git a/datahub/webapp/__tests__/ui/Select.test.js b/datahub/webapp/__tests__/ui/Select.test.js index 27e5e79e6..bbdeeb068 100644 --- a/datahub/webapp/__tests__/ui/Select.test.js +++ b/datahub/webapp/__tests__/ui/Select.test.js @@ -1,13 +1,52 @@ import React from 'react'; -import { shallow } from 'enzyme'; +import { shallow, mount } from 'enzyme'; import toJson from 'enzyme-to-json'; -import { Select } from '../../ui/Select/Select'; +import { Select, makeSelectOptions } from '../../ui/Select/Select'; it('renders without crashing', () => { shallow( onChangeMock(event.target.value)} + > + {makeSelectOptions(['test', 'test1', 'test2'])} + + ); + wrapper.find('select').instance().selectedIndex = 2; + wrapper.find('select').simulate('change'); + + expect(wrapper.find('option')).toHaveLength(3); + expect(onChangeMock).toHaveBeenCalledTimes(1); + expect(onChangeMock).toHaveBeenCalledWith('test2'); + }); + + it('deselects correctly', () => { + const onChangeMock = jest.fn(); + const wrapper = mount( + + ); + wrapper.find('select').instance().selectedIndex = 0; + wrapper.find('select').simulate('change'); + + expect(wrapper.find('option')).toHaveLength(4); + expect(onChangeMock).toHaveBeenCalledTimes(1); + expect(onChangeMock).toHaveBeenCalledWith(''); + }); +}); + describe('matches enzyme snapshots', () => { it('matches snapshot', () => { const wrapper = shallow( {deselectOption} @@ -65,11 +64,6 @@ export const Select: React.FunctionComponent = ({ ); }; -Select.defaultProps = { - className: '', - deselectValue: '', -}; - export type IOptions = Array< | { key: any; diff --git a/datahub/webapp/ui/SimpleReactSelect/SimpleReactSelect.tsx b/datahub/webapp/ui/SimpleReactSelect/SimpleReactSelect.tsx index 23d720359..0f97ad04e 100644 --- a/datahub/webapp/ui/SimpleReactSelect/SimpleReactSelect.tsx +++ b/datahub/webapp/ui/SimpleReactSelect/SimpleReactSelect.tsx @@ -18,6 +18,9 @@ export interface ISimpleReactSelectProps { withDeselect?: boolean; isDisabled?: boolean; selectProps?: Partial>; + + // Clear selection user picks value + clearAfterSelect?: boolean; } const reactSelectStyle = makeReactSelectStyle(true); @@ -26,10 +29,21 @@ export function SimpleReactSelect({ options, value, onChange, - withDeselect = false, isDisabled, + selectProps = {}, + withDeselect = false, + clearAfterSelect = false, }: ISimpleReactSelectProps) { + const overrideSelectProps = useMemo(() => { + const override: Partial> = {}; + if (clearAfterSelect) { + override.value = null; + } + + return override; + }, [clearAfterSelect]); + const computedOptions = useMemo( () => (options || []).map((option) => @@ -64,6 +78,7 @@ export function SimpleReactSelect({ isClearable={withDeselect} menuPlacement={'auto'} {...selectProps} + {...overrideSelectProps} /> ); }