From e790eab18b2ef02b855f8ee0c29d0115c95cca1f Mon Sep 17 00:00:00 2001 From: Douglas Egiemeh Date: Thu, 30 May 2024 12:03:00 +0200 Subject: [PATCH] Pass down components prop to selectable search input (#2831) * feat(selectable search input): pass down components prop to selectable search input * feat(selectable search input): test to assert custom component prop is used * feat(selectable search input): include property name in changeset --------- Co-authored-by: Ddouglasz --- .changeset/selfish-hounds-beg.md | 6 ++++ .../inputs/selectable-search-input/README.md | 1 + .../src/selectable-search-input.spec.tsx | 29 +++++++++++++++++++ .../src/selectable-search-input.tsx | 7 +++++ .../src/selectable-select.tsx | 1 + 5 files changed, 44 insertions(+) create mode 100644 .changeset/selfish-hounds-beg.md diff --git a/.changeset/selfish-hounds-beg.md b/.changeset/selfish-hounds-beg.md new file mode 100644 index 0000000000..77073d429e --- /dev/null +++ b/.changeset/selfish-hounds-beg.md @@ -0,0 +1,6 @@ +--- +'@commercetools-uikit/selectable-search-input': minor +--- + +We're introducing a new property, `selectCustomComponents` so consumers can customize the different parts of the select element in this component. +You can learn more about how this customization works in the `react-select` library [docs](https://react-select.com/components) we use under the hood diff --git a/packages/components/inputs/selectable-search-input/README.md b/packages/components/inputs/selectable-search-input/README.md index fc6c87b4d4..9a7f09a4ad 100644 --- a/packages/components/inputs/selectable-search-input/README.md +++ b/packages/components/inputs/selectable-search-input/README.md @@ -92,6 +92,7 @@ export default Example; | `showSubmitButton` | `boolean` | | `true` | Show submit button in the input | | `selectDataProps` | `Record` | | | used to pass data-\* props to the select component. eg: selectDataProps={\[{ 'prop-1': 'value-1' }, { 'prop-2': 'value-2' }]} | | `inputDataProps` | `Record` | | | used to pass data-\* props to the input element. eg: inputDataProps={\[{ 'prop-1': 'value-1' }, { 'prop-2': 'value-2' }]} | +| `selectCustomComponents` | `ReactSelectProps['components']` | | | Map of components to overwrite the default ones, see what components you can override
[Props from React select was used](https://react-select.com/props) | ## Signatures diff --git a/packages/components/inputs/selectable-search-input/src/selectable-search-input.spec.tsx b/packages/components/inputs/selectable-search-input/src/selectable-search-input.spec.tsx index 9befc2e331..913fe1d353 100644 --- a/packages/components/inputs/selectable-search-input/src/selectable-search-input.spec.tsx +++ b/packages/components/inputs/selectable-search-input/src/selectable-search-input.spec.tsx @@ -4,6 +4,7 @@ import SelectableSearchInput, { type TCustomEvent, } from './selectable-search-input'; import { screen, render, fireEvent } from '../../../../../test/test-utils'; +import { components, OptionProps } from 'react-select'; // We use this component to simulate the whole flow of // changing a value and submitting. @@ -181,6 +182,34 @@ describe('SelectableSearchInput', () => { }); }); + it('should render custom dropdown component', async () => { + const onFocus = jest.fn(); + + const Option = (props: OptionProps) => { + return ( +
+ Custom text + +
+ ); + }; + + render( + + ); + + const selectInput = screen.getByLabelText('Bar'); + fireEvent.focus(selectInput); + fireEvent.keyDown(selectInput, { key: 'ArrowDown', code: 'ArrowDown' }); + expect( + screen.getByTestId('selectable-search-input-container') + ).toHaveTextContent('Custom text'); + }); + it('should call onBlur twice when input loses focus for outside element', () => { const onBlur = jest.fn(); render( diff --git a/packages/components/inputs/selectable-search-input/src/selectable-search-input.tsx b/packages/components/inputs/selectable-search-input/src/selectable-search-input.tsx index 8acf6de5eb..da7cdf5777 100644 --- a/packages/components/inputs/selectable-search-input/src/selectable-search-input.tsx +++ b/packages/components/inputs/selectable-search-input/src/selectable-search-input.tsx @@ -221,6 +221,12 @@ export type TSelectableSearchInputProps = { * eg: inputDataProps={[{ 'prop-1': 'value-1' }, { 'prop-2': 'value-2' }]} */ inputDataProps?: Record; + /** + * Map of components to overwrite the default ones, see what components you can override + *
+ * [Props from React select was used](https://react-select.com/props) + */ + selectCustomComponents?: ReactSelectProps['components']; }; const defaultProps: Pick< @@ -447,6 +453,7 @@ const SelectableSearchInput = (props: TSelectableSearchInputProps) => { textInputRef={textInputRef} selectedOption={selectedOption} dataProps={transformedSelectDataProps} + selectCustomComponents={props.selectCustomComponents} />
{ /> ), DropdownIndicator, + ...props.selectCustomComponents, }} options={props.options} menuIsOpen={props.isReadOnly ? false : undefined}