From d544504f87aa6d6771f30960686008f24dda7c60 Mon Sep 17 00:00:00 2001 From: Mollie Anderson Date: Thu, 14 Nov 2024 15:58:40 -0800 Subject: [PATCH 01/11] feat(ui): create ToggleButtonGroup component --- .../ToggleButtonGroup/ToggleButtonGroup.tsx | 48 +++++++++++++++++++ .../components/ToggleButtonGroup/index.tsx | 2 + .../components/ToggleButtonGroup/types.tsx | 6 +++ 3 files changed, 56 insertions(+) create mode 100644 weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx create mode 100644 weave-js/src/components/ToggleButtonGroup/index.tsx create mode 100644 weave-js/src/components/ToggleButtonGroup/types.tsx diff --git a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx new file mode 100644 index 00000000000..7237ade9f27 --- /dev/null +++ b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx @@ -0,0 +1,48 @@ +import * as ToggleGroup from '@radix-ui/react-toggle-group'; +import React from 'react'; +import { twMerge } from 'tailwind-merge'; +import {Tailwind} from '../Tailwind'; +import {ToggleButtonGroupSize} from './types'; + +export type ToggleButtonGroupProps = { + options: string[]; + value: string; + onValueChange: (value: string) => void; + size: ToggleButtonGroupSize; +}; + +export const ToggleButtonGroup = React.forwardRef( + ({ options, value, onValueChange, size }, ref) => { + return ( + + + {options.map((option) => ( + + {option} + + ))} + + + ); + } +); + +ToggleButtonGroup.displayName = 'ToggleButtonGroup'; diff --git a/weave-js/src/components/ToggleButtonGroup/index.tsx b/weave-js/src/components/ToggleButtonGroup/index.tsx new file mode 100644 index 00000000000..3e1c860a4be --- /dev/null +++ b/weave-js/src/components/ToggleButtonGroup/index.tsx @@ -0,0 +1,2 @@ +export * from './ToggleButtonGroup'; +export * from './types'; diff --git a/weave-js/src/components/ToggleButtonGroup/types.tsx b/weave-js/src/components/ToggleButtonGroup/types.tsx new file mode 100644 index 00000000000..454fed479ee --- /dev/null +++ b/weave-js/src/components/ToggleButtonGroup/types.tsx @@ -0,0 +1,6 @@ +export const ToggleButtonGroupSizes = { + Small: 'small', + Medium: 'medium', + Large: 'large', +} as const; +export type ToggleButtonGroupSize = (typeof ToggleButtonGroupSizes)[keyof typeof ToggleButtonGroupSizes]; From 5c11a0d1416d27762869714445f73e8a014f2b3f Mon Sep 17 00:00:00 2001 From: Mollie Anderson Date: Fri, 15 Nov 2024 14:46:11 -0800 Subject: [PATCH 02/11] add icon prop --- .../ToggleButtonGroup/ToggleButtonGroup.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx index 7237ade9f27..67716eacc7e 100644 --- a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx +++ b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx @@ -1,18 +1,22 @@ -import * as ToggleGroup from '@radix-ui/react-toggle-group'; import React from 'react'; import { twMerge } from 'tailwind-merge'; +import * as ToggleGroup from '@radix-ui/react-toggle-group'; + +import {Icon, IconName} from '../Icon'; import {Tailwind} from '../Tailwind'; import {ToggleButtonGroupSize} from './types'; export type ToggleButtonGroupProps = { options: string[]; value: string; - onValueChange: (value: string) => void; + icon?: IconName; size: ToggleButtonGroupSize; + isDisabled?: boolean; + onValueChange: (value: string) => void; }; export const ToggleButtonGroup = React.forwardRef( - ({ options, value, onValueChange, size }, ref) => { + ({ options, value, icon, size, isDisabled, onValueChange }, ref) => { return ( Date: Mon, 18 Nov 2024 12:11:33 -0800 Subject: [PATCH 03/11] import Button --- .../ToggleButtonGroup/ToggleButtonGroup.tsx | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx index 67716eacc7e..e24ac9063fd 100644 --- a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx +++ b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx @@ -3,45 +3,56 @@ import { twMerge } from 'tailwind-merge'; import * as ToggleGroup from '@radix-ui/react-toggle-group'; import {Icon, IconName} from '../Icon'; +import {Button} from '../Button'; import {Tailwind} from '../Tailwind'; import {ToggleButtonGroupSize} from './types'; export type ToggleButtonGroupProps = { options: string[]; value: string; - icon?: IconName; size: ToggleButtonGroupSize; isDisabled?: boolean; + icon?: IconName; onValueChange: (value: string) => void; }; export const ToggleButtonGroup = React.forwardRef( - ({ options, value, icon, size, isDisabled, onValueChange }, ref) => { + ({ options, value, size, isDisabled, icon, onValueChange }, ref) => { + const handleValueChange = (newValue: string) => { + if (newValue !== value) { + onValueChange(newValue); + } + }; return ( {options.map((option) => ( - {option} + value={option}> + ))} From 717ffcfb6ab0f5acfc82d8dfeae88efbbbc48bec Mon Sep 17 00:00:00 2001 From: Mollie Anderson Date: Mon, 18 Nov 2024 17:08:23 -0800 Subject: [PATCH 04/11] fiix gap, update styling --- .../ToggleButtonGroup/ToggleButtonGroup.tsx | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx index e24ac9063fd..13eb48799a9 100644 --- a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx +++ b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx @@ -12,12 +12,12 @@ export type ToggleButtonGroupProps = { value: string; size: ToggleButtonGroupSize; isDisabled?: boolean; - icon?: IconName; + icons?: (IconName | undefined)[]; onValueChange: (value: string) => void; }; export const ToggleButtonGroup = React.forwardRef( - ({ options, value, size, isDisabled, icon, onValueChange }, ref) => { + ({ options, value, size, isDisabled, icons, onValueChange }, ref) => { const handleValueChange = (newValue: string) => { if (newValue !== value) { onValueChange(newValue); @@ -29,27 +29,31 @@ export const ToggleButtonGroup = React.forwardRef - {options.map((option) => ( + {options.map((option, index) => ( - From 44b6b9ef910362f82141c163f7d4a878e75b3a4b Mon Sep 17 00:00:00 2001 From: Mollie Anderson Date: Tue, 19 Nov 2024 19:55:39 -0800 Subject: [PATCH 05/11] add error handling, fix styling --- .../ToggleButtonGroup/ToggleButtonGroup.tsx | 103 +++++++++--------- .../components/ToggleButtonGroup/types.tsx | 3 +- 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx index 13eb48799a9..435a842a06b 100644 --- a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx +++ b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx @@ -1,9 +1,9 @@ -import React from 'react'; -import { twMerge } from 'tailwind-merge'; import * as ToggleGroup from '@radix-ui/react-toggle-group'; +import React from 'react'; +import {twMerge} from 'tailwind-merge'; -import {Icon, IconName} from '../Icon'; import {Button} from '../Button'; +import {IconName} from '../Icon'; import {Tailwind} from '../Tailwind'; import {ToggleButtonGroupSize} from './types'; @@ -12,57 +12,58 @@ export type ToggleButtonGroupProps = { value: string; size: ToggleButtonGroupSize; isDisabled?: boolean; - icons?: (IconName | undefined)[]; + icons?: Array; onValueChange: (value: string) => void; }; -export const ToggleButtonGroup = React.forwardRef( - ({ options, value, size, isDisabled, icons, onValueChange }, ref) => { - const handleValueChange = (newValue: string) => { - if (newValue !== value) { - onValueChange(newValue); - } - }; - return ( - - - {options.map((option, index) => ( - - - - ))} - - - ); +export const ToggleButtonGroup = React.forwardRef< + HTMLDivElement, + ToggleButtonGroupProps +>(({options, value, size, isDisabled = false, icons, onValueChange}, ref) => { + if (options.length < 2) { + console.error('ToggleButtonGroup requires at least two options.'); } -); + + const handleValueChange = (newValue: string) => { + if (newValue !== value) { + onValueChange(newValue); + } + }; + return ( + + + {options.map((option, index) => ( + + + + ))} + + + ); +}); ToggleButtonGroup.displayName = 'ToggleButtonGroup'; diff --git a/weave-js/src/components/ToggleButtonGroup/types.tsx b/weave-js/src/components/ToggleButtonGroup/types.tsx index 454fed479ee..977002f9256 100644 --- a/weave-js/src/components/ToggleButtonGroup/types.tsx +++ b/weave-js/src/components/ToggleButtonGroup/types.tsx @@ -3,4 +3,5 @@ export const ToggleButtonGroupSizes = { Medium: 'medium', Large: 'large', } as const; -export type ToggleButtonGroupSize = (typeof ToggleButtonGroupSizes)[keyof typeof ToggleButtonGroupSizes]; +export type ToggleButtonGroupSize = + (typeof ToggleButtonGroupSizes)[keyof typeof ToggleButtonGroupSizes]; From 1027fddb3d3948ed37ae349a3b57c1d272585c73 Mon Sep 17 00:00:00 2001 From: Mollie Anderson Date: Wed, 20 Nov 2024 11:40:20 -0800 Subject: [PATCH 06/11] yarn lock for toggle --- weave-js/package.json | 1 + .../ToggleButtonGroup/ToggleButtonGroup.tsx | 3 +- weave-js/yarn.lock | 105 ++++++++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/weave-js/package.json b/weave-js/package.json index 96647af8cab..cb57125143a 100644 --- a/weave-js/package.json +++ b/weave-js/package.json @@ -48,6 +48,7 @@ "@radix-ui/react-slider": "^1.0.4", "@radix-ui/react-switch": "^1.0.3", "@radix-ui/react-tabs": "^1.0.4", + "@radix-ui/react-toggle-group": "^1.1.0", "@radix-ui/react-tooltip": "^1.0.7", "@segment/analytics.js-core": "^4.1.11", "@segment/analytics.js-integration-segmentio": "^4.4.7", diff --git a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx index 435a842a06b..eaa563c452f 100644 --- a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx +++ b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx @@ -32,7 +32,7 @@ export const ToggleButtonGroup = React.forwardRef< return ( ))} diff --git a/weave-js/src/components/ToggleButtonGroup/types.tsx b/weave-js/src/components/ToggleButtonGroup/types.tsx index 977002f9256..2a79660fbd8 100644 --- a/weave-js/src/components/ToggleButtonGroup/types.tsx +++ b/weave-js/src/components/ToggleButtonGroup/types.tsx @@ -1,7 +1,6 @@ -export const ToggleButtonGroupSizes = { - Small: 'small', - Medium: 'medium', - Large: 'large', -} as const; -export type ToggleButtonGroupSize = - (typeof ToggleButtonGroupSizes)[keyof typeof ToggleButtonGroupSizes]; +export enum ToggleButtonGroupSizes { + small = 'small', + medium = 'medium', + large = 'large', +} +export type ToggleButtonGroupSize = keyof typeof ToggleButtonGroupSizes; From afaef095c7251019a48fa476310513d0ee60e16f Mon Sep 17 00:00:00 2001 From: Mollie Anderson Date: Wed, 20 Nov 2024 16:39:58 -0800 Subject: [PATCH 09/11] remove type ToggleButtonGroupSize --- .../src/components/ToggleButtonGroup/ToggleButtonGroup.tsx | 4 ++-- weave-js/src/components/ToggleButtonGroup/types.tsx | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx index e81c9b93ab6..6d3f7361640 100644 --- a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx +++ b/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx @@ -5,7 +5,7 @@ import {twMerge} from 'tailwind-merge'; import {Button} from '../Button'; import {IconName} from '../Icon'; import {Tailwind} from '../Tailwind'; -import {ToggleButtonGroupSize} from './types'; +import {ToggleButtonGroupSizes} from './types'; export type ToggleOption = { label: string; @@ -15,7 +15,7 @@ export type ToggleOption = { export type ToggleButtonGroupProps = { options: ToggleOption[]; value: string; - size: ToggleButtonGroupSize; + size: ToggleButtonGroupSizes; isDisabled?: boolean; onValueChange: (value: string) => void; }; diff --git a/weave-js/src/components/ToggleButtonGroup/types.tsx b/weave-js/src/components/ToggleButtonGroup/types.tsx index 2a79660fbd8..f58c669454f 100644 --- a/weave-js/src/components/ToggleButtonGroup/types.tsx +++ b/weave-js/src/components/ToggleButtonGroup/types.tsx @@ -3,4 +3,3 @@ export enum ToggleButtonGroupSizes { medium = 'medium', large = 'large', } -export type ToggleButtonGroupSize = keyof typeof ToggleButtonGroupSizes; From 428c80b74d3f8123b21b4a1345f46d3636a7ac87 Mon Sep 17 00:00:00 2001 From: Mollie Anderson Date: Thu, 21 Nov 2024 20:05:07 -0800 Subject: [PATCH 10/11] remove barrel files, use ButtonSize instead of ToggleButtonGroupSizes --- .../ToggleButtonGroup.tsx | 29 +++++++++++-------- .../components/ToggleButtonGroup/index.tsx | 2 -- .../components/ToggleButtonGroup/types.tsx | 5 ---- 3 files changed, 17 insertions(+), 19 deletions(-) rename weave-js/src/components/{ToggleButtonGroup => }/ToggleButtonGroup.tsx (77%) delete mode 100644 weave-js/src/components/ToggleButtonGroup/index.tsx delete mode 100644 weave-js/src/components/ToggleButtonGroup/types.tsx diff --git a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup.tsx similarity index 77% rename from weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx rename to weave-js/src/components/ToggleButtonGroup.tsx index 6d3f7361640..45fc33846ec 100644 --- a/weave-js/src/components/ToggleButtonGroup/ToggleButtonGroup.tsx +++ b/weave-js/src/components/ToggleButtonGroup.tsx @@ -2,20 +2,19 @@ import * as ToggleGroup from '@radix-ui/react-toggle-group'; import React from 'react'; import {twMerge} from 'tailwind-merge'; -import {Button} from '../Button'; -import {IconName} from '../Icon'; -import {Tailwind} from '../Tailwind'; -import {ToggleButtonGroupSizes} from './types'; +import {Button, ButtonSize} from './Button'; +import {IconName} from './Icon'; +import {Tailwind} from './Tailwind'; export type ToggleOption = { - label: string; + value: string; icon?: IconName; }; export type ToggleButtonGroupProps = { options: ToggleOption[]; value: string; - size: ToggleButtonGroupSizes; + size: ButtonSize; isDisabled?: boolean; onValueChange: (value: string) => void; }; @@ -31,6 +30,12 @@ export const ToggleButtonGroup = React.forwardRef< return null; // Do not render if there are fewer than two options } + if (!options.some(option => option.value === value)) { + console.warn( + `Warning: The provided value "${value}" is not one of the options.` + ); + } + const handleValueChange = (newValue: string) => { if (newValue !== value) { onValueChange(newValue); @@ -44,11 +49,11 @@ export const ToggleButtonGroup = React.forwardRef< onValueChange={handleValueChange} className="flex gap-px" ref={ref}> - {options.map(({label, icon}) => ( + {options.map(({value: optionValue, icon}) => ( + key={optionValue} + value={optionValue} + disabled={isDisabled || value === optionValue}> ))} diff --git a/weave-js/src/components/ToggleButtonGroup/index.tsx b/weave-js/src/components/ToggleButtonGroup/index.tsx deleted file mode 100644 index 3e1c860a4be..00000000000 --- a/weave-js/src/components/ToggleButtonGroup/index.tsx +++ /dev/null @@ -1,2 +0,0 @@ -export * from './ToggleButtonGroup'; -export * from './types'; diff --git a/weave-js/src/components/ToggleButtonGroup/types.tsx b/weave-js/src/components/ToggleButtonGroup/types.tsx deleted file mode 100644 index f58c669454f..00000000000 --- a/weave-js/src/components/ToggleButtonGroup/types.tsx +++ /dev/null @@ -1,5 +0,0 @@ -export enum ToggleButtonGroupSizes { - small = 'small', - medium = 'medium', - large = 'large', -} From c0f0b6aad1d9e35e322639a7d9ac7f5a99c7b6e6 Mon Sep 17 00:00:00 2001 From: Mollie Anderson Date: Fri, 22 Nov 2024 09:58:38 -0800 Subject: [PATCH 11/11] update padding --- weave-js/src/components/ToggleButtonGroup.tsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/weave-js/src/components/ToggleButtonGroup.tsx b/weave-js/src/components/ToggleButtonGroup.tsx index 45fc33846ec..65b2c538975 100644 --- a/weave-js/src/components/ToggleButtonGroup.tsx +++ b/weave-js/src/components/ToggleButtonGroup.tsx @@ -58,9 +58,14 @@ export const ToggleButtonGroup = React.forwardRef< icon={icon} size={size} className={twMerge( - size === 'small' && 'gap-2 px-6 py-3 text-sm', - size === 'medium' && 'gap-3 px-10 py-4 text-base', - size === 'large' && 'gap-2 px-12 py-8 text-base', + 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]'