Skip to content

Commit

Permalink
feat: make single selct unselectable
Browse files Browse the repository at this point in the history
affects: @medly-components/core
  • Loading branch information
gmukul01 committed Apr 28, 2024
1 parent 12ea435 commit 9abfba0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The `SingleSelect` component allows you to pick a value from a list of options.
onChange={setValue}
size={select('Size', sizes, 'M')}
isSearchable={boolean('Is Searchable', false)}
isUnselectable={boolean('Is Unselectable', false)}
variant={select('Variant', variants, 'filled')}
disabled={boolean('Disabled', false)}
fullWidth={boolean('Full Width', false)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ describe('SingleSelect component', () => {
expect(mockOnChange).toHaveBeenCalledWith('Dummy1');
});

it('should un select option on click on it again if isUnselectable passed', () => {
const mockOnChange = jest.fn();
render(<SingleSelect value="Dummy1" isUnselectable options={options} onChange={mockOnChange} />);

fireEvent.click(screen.getByRole('textbox'));
fireEvent.click(screen.getByText('Dummy1'));
expect(mockOnChange).toHaveBeenCalledWith('');
});

it('should call onChange with input value if input value matches any option label', () => {
const mockOnChange = jest.fn();
render(<SingleSelect options={options} onChange={mockOnChange} />);
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/components/SingleSelect/SingleSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const Component: FC<SingleSelectProps> = memo(
validator,
isSearchable,
suffix,
isUnselectable,
...inputProps
} = props,
selectId = useMemo(() => id || inputProps.label?.toLocaleLowerCase() || 'medly-singleSelect', [id, inputProps.label]),
Expand Down Expand Up @@ -92,13 +93,13 @@ const Component: FC<SingleSelectProps> = memo(
setOptions(getUpdatedOptions(options, option));
setInputValue(option.label);
hideOptions();
onChange?.(option.value);
isUnselectable && value === option.value ? onChange?.('') : onChange?.(option.value);
setErrorMessage('');
} else {
inputRef.current?.focus();
}
},
[inputRef.current, options, onChange]
[inputRef.current, value, options, onChange]
),
handleOuterClick = useCallback(() => {
isFocused.current = false;
Expand Down Expand Up @@ -226,6 +227,7 @@ Component.defaultProps = {
isSearchable: false,
includesNestedOptions: false,
placeholder: 'Please Select . . .',
showDecorators: true
showDecorators: true,
isUnselectable: false
};
export const SingleSelect: FC<SingleSelectProps> & WithStyle = Object.assign(Component, { Style: Styled.Wrapper });
2 changes: 2 additions & 0 deletions packages/core/src/components/SingleSelect/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface SingleSelectProps extends InputProps {
size?: 'S' | 'M';
/** Set it it true to enable search functionality */
isSearchable?: boolean;
/** Set it true to make it unselectable */
isUnselectable?: boolean;
/** Min width in px/rem/% (1rem = 10px)*/
minWidth?: string;
/** Max width in px/rem/% (1rem = 10px)*/
Expand Down

0 comments on commit 9abfba0

Please sign in to comment.