Skip to content

Commit

Permalink
feat(Select): Add ability to remove clear button
Browse files Browse the repository at this point in the history
  • Loading branch information
thyhjwb6 committed Jun 10, 2021
1 parent 718349b commit e95c0c8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ export const Disabled: Story<SelectProps> = (props) => (

Disabled.storyName = 'Disabled'

export const NotClearable: Story<SelectProps> = (props) => (
<Select
{...props}
options={options}
label="Example label"
name="select-disabled"
isClearable={false}
/>
)

NotClearable.storyName = 'Not clearable'

export const WithIcons: Story<SelectProps> = (props) => {
const iconOptions = options.map((option) => ({
...option,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ describe('Select', () => {
wrapper.getByText('Option 1').click()
})

it('should render a clear button', () => {
const control = wrapper.container.children[0].children[1]
const indicators = control.children[1]
const firstIndicator = indicators.children[0]

expect(firstIndicator.classList).toContain(
'rn-select__clear-indicator'
)
})

it('should render the select with the selected value', () => {
return waitFor(() => {
expect(
Expand Down Expand Up @@ -195,4 +205,41 @@ describe('Select', () => {
})
})
})

describe('when `isClearable` is `false`', () => {
beforeEach(() => {
onChangeSpy = jest.fn()

wrapper = render(
<Select isClearable={false} onChange={onChangeSpy} options={options} />
)
})

describe('when the select is clicked', () => {
beforeEach(() => {
const input = wrapper.getByTestId('react-select-vendor-input')
fireEvent.focus(input)
fireEvent.keyDown(input, {
key: 'ArrowDown',
code: 40,
})
})

describe('when the first option is clicked', () => {
beforeEach(() => {
wrapper.getByText('Option 1').click()
})

it('should not render a clear button', () => {
const control = wrapper.container.children[0].children[1]
const indicators = control.children[1]
const firstIndicator = indicators.children[0]

expect(firstIndicator.classList).not.toContain(
'rn-select__clear-indicator'
)
})
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export interface SelectProps
extends ReactSelectProps<any>,
ComponentWithClass,
InputValidationProps {
/**
* Specify whether or not to have a clear button.
*/
isClearable?: boolean
/**
* Optional text label to display within the component.
*/
Expand All @@ -41,6 +45,7 @@ export interface SelectProps
}

export const Select: React.FC<SelectProps> = ({
isClearable = true,
label,
name,
onChange,
Expand Down Expand Up @@ -79,7 +84,7 @@ export const Select: React.FC<SelectProps> = ({
MenuList: StyledMenuList,
ValueContainer: StyledValueContainer,
}}
isClearable
isClearable={isClearable}
name={name}
onChange={onSelectChange}
options={options}
Expand Down

0 comments on commit e95c0c8

Please sign in to comment.