Skip to content

Commit

Permalink
fix(react): disable no options on select
Browse files Browse the repository at this point in the history
  • Loading branch information
dackmin committed Jan 3, 2023
1 parent b6e3a50 commit c9249b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/react/lib/SelectField/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare interface SelectFieldProps extends React.ComponentPropsWithRef<any> {
toggleClick?: Boolean;
keyboardHandler?: Boolean;
multiple?: Boolean;
noOptionsEnabled?: Boolean;
noOptionsLabel?: String;
searchable?: Boolean;
searchMinCharacters?: number;
Expand Down
26 changes: 16 additions & 10 deletions packages/react/lib/SelectField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const SelectField = forwardRef(({
clearable = true,
disabled = false,
multiple = false,
noOptionsEnabled = true,
noOptionsLabel = 'No options',
searchable = true,
searchMinCharacters = 2,
Expand Down Expand Up @@ -381,6 +382,10 @@ const SelectField = forwardRef(({
))
), [state.searchResults, state.value, options, onChange]);

const hasOptions = useMemo(() => (
renderedOptions.length > 0 && renderedOptions.some(o => o !== null)
), [renderedOptions]);

return (
<Dropdown
{ ...rest }
Expand Down Expand Up @@ -449,18 +454,18 @@ const SelectField = forwardRef(({
</div>
</div>
</DropdownToggle>
<DropdownMenu
animate={animateMenu}
className={classNames('select-menu', { searching: state.searching })}
>
<div className="content">
{ renderedOptions.length > 0 && renderedOptions.some(o => o !== null)
? renderedOptions
: (
{ (hasOptions || noOptionsEnabled || state.searchResults) && (
<DropdownMenu
animate={animateMenu}
className={classNames('select-menu', { searching: state.searching })}
>
<div className="content">
{ hasOptions ? renderedOptions : (
<div className="no-options">{ noOptionsLabel }</div>
) }
</div>
</DropdownMenu>
</div>
</DropdownMenu>
) }
</Dropdown>
);
});
Expand All @@ -475,6 +480,7 @@ SelectField.propTypes = {
disabled: PropTypes.bool,
keyboardHandler: PropTypes.bool,
multiple: PropTypes.bool,
noOptionsEnabled: PropTypes.bool,
noOptionsLabel: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node,
Expand Down
8 changes: 8 additions & 0 deletions packages/react/lib/SelectField/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@ export const withClickOptionsAndKeyboardHandler = () => (
export const animated = () => (
<SelectField animateMenu={slideInDownMenu} />
);

export const noEmpty = () => (
<SelectField
placeholder="Type a name"
noOptionsEnabled={false}
animateMenu={slideInDownMenu}
/>
);

0 comments on commit c9249b4

Please sign in to comment.