Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(weave): Update ToggleButtonGroup to allow disabling individual options #3194

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 38 additions & 29 deletions weave-js/src/components/ToggleButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Tailwind} from './Tailwind';
export type ToggleOption = {
value: string;
icon?: IconName;
isDisabled?: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added isDisabled prop to specific options

};

export type ToggleButtonGroupProps = {
Expand Down Expand Up @@ -37,7 +38,10 @@ export const ToggleButtonGroup = React.forwardRef<
}

const handleValueChange = (newValue: string) => {
if (newValue !== value) {
if (
newValue !== value &&
options.find(option => option.value === newValue)?.isDisabled !== true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'pointer-events-none' didn't work to prevent the onChange from firing, so moved the isDisabled check here, and fixed it below to not show a pointer cursor

) {
onValueChange(newValue);
}
};
Expand All @@ -49,34 +53,39 @@ export const ToggleButtonGroup = React.forwardRef<
onValueChange={handleValueChange}
className="flex gap-px"
ref={ref}>
{options.map(({value: optionValue, icon}) => (
<ToggleGroup.Item
key={optionValue}
value={optionValue}
disabled={isDisabled || value === optionValue}>
<Button
icon={icon}
size={size}
className={twMerge(
size === 'small' &&
(icon ? 'gap-4 px-4 py-3 text-sm' : 'px-8 py-3 text-sm'),
size === 'medium' &&
(icon ? 'gap-5 px-7 py-4 text-base' : 'px-10 py-4 text-base'),
size === 'large' &&
(icon
? 'gap-6 px-10 py-8 text-base'
: 'px-12 py-8 text-base'),
isDisabled && 'pointer-events-none opacity-35',
value === optionValue
? 'bg-teal-300/[0.48] text-teal-600 hover:bg-teal-300/[0.48]'
: 'hover:bg-oblivion/7 bg-oblivion/5 text-moon-600 hover:text-moon-800',
'first:rounded-l-sm', // First button rounded left
'last:rounded-r-sm' // Last button rounded right
)}>
{optionValue}
</Button>
</ToggleGroup.Item>
))}
{options.map(
({value: optionValue, icon, isDisabled: optionIsDisabled}) => (
<ToggleGroup.Item
key={optionValue}
value={optionValue}
disabled={isDisabled}
asChild>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asChild is needed to prevent a validateDomNesting error for nested buttons, see https://www.radix-ui.com/primitives/docs/guides/composition

<Button
icon={icon}
size={size}
className={twMerge(
size === 'small' &&
(icon ? 'gap-4 px-4 py-3 text-sm' : 'px-8 py-3 text-sm'),
size === 'medium' &&
(icon
? 'gap-5 px-7 py-4 text-base'
: 'px-10 py-4 text-base'),
size === 'large' &&
(icon
? 'gap-6 px-10 py-8 text-base'
: 'px-12 py-8 text-base'),
(isDisabled || optionIsDisabled) && 'cursor-auto opacity-35',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checks optionIsDisabled and changes pointer-events-none to cursor-auto

value === optionValue
? 'bg-teal-300/[0.48] text-teal-600 hover:bg-teal-300/[0.48]'
: 'hover:bg-oblivion/7 bg-oblivion/5 text-moon-600 hover:text-moon-800',
'first:rounded-l-sm', // First button rounded left
'last:rounded-r-sm' // Last button rounded right
)}>
{optionValue}
</Button>
</ToggleGroup.Item>
)
)}
</ToggleGroup.Root>
</Tailwind>
);
Expand Down
Loading