Skip to content

Commit

Permalink
fix(Select): pass disabled prop to renderControl, don't change state …
Browse files Browse the repository at this point in the history
…if disabled (#1766)
  • Loading branch information
ValeraS authored Aug 23, 2024
1 parent 442ce85 commit 24bbb05
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function
open: propsOpen,
onClose,
onOpenChange,
disabled,
});

React.useEffect(() => {
Expand Down
6 changes: 4 additions & 2 deletions src/components/Select/__stories__/SelectShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,13 @@ export const SelectShowcase = (props: SelectProps) => {
selectProps={{
...props,
className: b('user-control'),
renderControl: ({onClick, onKeyDown, ref, renderClear}) => {
renderControl: ({onClick, onKeyDown, ref, renderClear, disabled}) => {
return (
<Button
ref={ref}
view="action"
onClick={onClick}
disabled={disabled}
extraProps={{
onKeyDown,
}}
Expand Down Expand Up @@ -300,12 +301,13 @@ export const SelectShowcase = (props: SelectProps) => {
...props,
className: b('user-control-placement'),
popupPlacement: ['bottom'],
renderControl: ({onClick, onKeyDown, ref}) => {
renderControl: ({onClick, onKeyDown, ref, disabled}) => {
return (
<Button
ref={ref}
view="action"
onClick={onClick}
disabled={disabled}
extraProps={{
onKeyDown,
'aria-label': 'Add',
Expand Down
6 changes: 4 additions & 2 deletions src/components/Select/__stories__/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,13 @@ export const EXAMPLE_USER_CONTROL = `const [value, setValue] = React.useState<st
<Select
value={value}
renderControl={({onClick, onKeyDown, ref, renderClear}) => {
renderControl={({onClick, onKeyDown, ref, renderClear, disabled}) => {
return (
<Button
ref={ref}
view="action"
onClick={onClick}
disabled={disabled}
extraProps={{
onKeyDown,
}}
Expand All @@ -175,12 +176,13 @@ export const EXAMPLE_USER_CONTROL_WITH_PLACEMENT = `const [value, setValue] = Re
<Select
value={value}
popupPlacement={['bottom']}
renderControl={({onClick, onKeyDown, ref}) => {
renderControl={({onClick, onKeyDown, ref, disabled}) => {
return (
<Button
ref={ref}
view="action"
onClick={onClick}
disabled={disabled}
extraProps={{
onKeyDown,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const SelectControl = React.forwardRef<HTMLButtonElement, ControlProps>((
popupId,
selectId,
activeIndex,
disabled,
},
{value},
);
Expand Down
1 change: 1 addition & 0 deletions src/components/Select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type SelectRenderControlProps = {
popupId: string;
selectId: string;
activeIndex?: number;
disabled?: boolean;
};
export type SelectRenderControlOptions = {
value: SelectProps['value'];
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useSelect/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type UseSelectProps = {
defaultValue?: string[];
multiple?: boolean;
onUpdate?: (value: string[]) => void;
disabled?: boolean;
} & UseOpenProps;

export type UseSelectResult<T> = {
Expand Down
12 changes: 11 additions & 1 deletion src/hooks/useSelect/useSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export const useSelect = <T extends unknown>({
defaultValue = [],
multiple,
onUpdate,
disabled,
}: UseSelectProps): UseSelectResult<T> => {
const [value, setValue] = useControlledState(valueProps, defaultValue, onUpdate);
const [value, setValueInner] = useControlledState(valueProps, defaultValue, onUpdate);
const [activeIndex, setActiveIndex] = React.useState<number>();
const {toggleOpen, ...openState} = useOpenState({
defaultOpen,
Expand All @@ -24,6 +25,15 @@ export const useSelect = <T extends unknown>({
open,
});

const setValue = React.useCallback(
(v: string[]) => {
if (!disabled) {
setValueInner(v);
}
},
[setValueInner, disabled],
);

const handleSingleSelection = React.useCallback(
(option: UseSelectOption<T>) => {
if (!value.includes(option.value)) {
Expand Down

0 comments on commit 24bbb05

Please sign in to comment.