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

feat: Add spinner to Toggle component #3803

Merged
merged 3 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions src/ui/Toggle/Toggle.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,25 @@ export const DisabledToggle: Story = {
)
},
}

export const LoadingToggle: Story = {
render: (args) => {
const [toggle, setToggle] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const toggler = async () => {
setIsLoading(true)
setTimeout(() => {
setToggle(!toggle)
setIsLoading(false)
}, 2000)
}
return (
<Toggle
value={toggle}
isLoading={isLoading}
{...args}
onClick={toggler}
/>
)
},
}
84 changes: 84 additions & 0 deletions src/ui/Toggle/Toggle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,88 @@ describe('Toggle', () => {
expect(button).toHaveAttribute('disabled')
})
})

describe('isLoading behavior', () => {
describe('when isLoading is true', () => {
it('renders spinner', () => {
render(
<Toggle
label="🐕"
dataMarketing="marketing"
value={true}
disabled={false}
isLoading={true}
onClick={() => {}}
/>
)

const spinner = screen.getByTestId('toggle-loading-spinner')
expect(spinner).toBeInTheDocument()
})

describe('and is clicked', () => {
it('does not fire onClick', async () => {
const { user } = setup()
const mockFn = vi.fn()
render(
<Toggle
label="🐕"
dataMarketing="marketing"
value={false}
disabled={false}
onClick={mockFn}
isLoading={true}
/>
)

const button = screen.getByRole('button')

await user.click(button)

expect(mockFn).not.toHaveBeenCalled()
})
})
})

describe('when isLoading is false', () => {
it('does not render spinner', () => {
render(
<Toggle
label="🐕"
dataMarketing="marketing"
value={true}
disabled={false}
isLoading={false}
onClick={() => {}}
/>
)

const spinner = screen.queryByTestId('toggle-loading-spinner')
expect(spinner).not.toBeInTheDocument()
})

describe('and is clicked', () => {
it('does fire onClick', async () => {
const { user } = setup()
const mockFn = vi.fn()
render(
<Toggle
label="🐕"
dataMarketing="marketing"
value={false}
disabled={false}
onClick={mockFn}
isLoading={false}
/>
)

const button = screen.getByRole('button')

await user.click(button)

expect(mockFn).toHaveBeenCalled()
})
})
})
})
})
33 changes: 24 additions & 9 deletions src/ui/Toggle/Toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface ToggleProps {
label: string
onClick: () => void
disabled?: boolean
isLoading?: boolean
dataMarketing: string
}

Expand All @@ -16,6 +17,7 @@ function Toggle({
value = false,
onClick,
disabled = false,
isLoading = false,
dataMarketing,
}: ToggleProps) {
const ID = uniqueId('toggle')
Expand All @@ -24,7 +26,7 @@ function Toggle({
<div
data-marketing={`${ID}-${dataMarketing}`}
onClick={() => {
if (!disabled) {
if (!disabled && !isLoading) {
onClick()
}
}}
Expand All @@ -43,7 +45,7 @@ function Toggle({
'bg-toggle-active': value,
'bg-toggle-inactive': !value && !disabled,
'bg-toggle-disabled': disabled,
'cursor-not-allowed': disabled,
'cursor-not-allowed': disabled || isLoading,
}
)}
aria-pressed="false"
Expand All @@ -63,18 +65,31 @@ function Toggle({
)}
>
<div
className={cn({
className={cn('flex size-5 items-center justify-center', {
'text-toggle-active': value,
'text-toggle-inactive': !value && !disabled,
'text-toggle-disabled': disabled,
})}
>
<Icon
name={value ? 'check' : 'x'}
label={value ? 'check' : 'x'}
variant="solid"
size="flex"
/>
{isLoading ? (
<div
data-testid="toggle-loading-spinner"
className={cn(
'size-4 animate-spin rounded-full border-4 border-white',
{
'border-t-toggle-active': value,
'border-t-toggle-inactive': !value && !disabled,
}
)}
/>
) : (
<Icon
name={value ? 'check' : 'x'}
label={value ? 'check' : 'x'}
variant="solid"
size="flex"
/>
)}
</div>
</span>
</button>
Expand Down