Skip to content

Commit

Permalink
fix(toggleswitch): disable + checked state and deprecated html event …
Browse files Browse the repository at this point in the history
…replacement (#987)

* docs: add disabled+active toggleswitch example

* fix(toggleswitch): fix state and replace onkeypress with onkeyup

This commit fixes the state for disable + enabled ToggleSwitch and also replaces the deprecated
onKeyPress with onKeyDown

fix #986

* fix(toggleswitch): remove unused event

* refactor(toggleswitch): remove dead code
  • Loading branch information
rluders authored Sep 24, 2023
1 parent 2a6c2e5 commit ce55df1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/docs/components/forms/forms.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ Use the `<ToggleSwitch>` component to ask users to enable or disable an option s
<ToggleSwitch checked={props.switch1} label="Toggle me" onChange={props.setSwitch1} />
<ToggleSwitch checked={props.switch2} label="Toggle me (checked)" onChange={props.setSwitch2} />
<ToggleSwitch checked={false} disabled label="Toggle me (disabled)" onChange={() => undefined} />
<ToggleSwitch checked={true} disabled label="Toggle me (disabled)" onChange={() => undefined} />
<ToggleSwitch checked={props.switch3} onChange={props.setSwitch3} />
</div>
</CodePreview>
Expand Down
15 changes: 8 additions & 7 deletions src/components/ToggleSwitch/ToggleSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentProps, FC, KeyboardEvent, MouseEvent } from 'react';
import type { ComponentProps, FC, KeyboardEvent } from 'react';
import { useId } from 'react';
import { twMerge } from 'tailwind-merge';
import type { DeepPartial, FlowbiteBoolean, FlowbiteColors } from '../../';
Expand Down Expand Up @@ -47,13 +47,14 @@ export const ToggleSwitch: FC<ToggleSwitchProps> = ({

const toggle = (): void => onChange(!checked);

const handleClick = (event: MouseEvent<HTMLButtonElement>): void => {
event.preventDefault();
const handleClick = (): void => {
toggle();
};

const handleKeyPress = (event: KeyboardEvent<HTMLButtonElement>): void => {
event.preventDefault();
const handleOnKeyDown = (event: KeyboardEvent<HTMLButtonElement>): void => {
if (event.code == 'Enter') {
event.preventDefault();
}
};

return (
Expand All @@ -67,7 +68,7 @@ export const ToggleSwitch: FC<ToggleSwitchProps> = ({
disabled={disabled}
id={`${id}-flowbite-toggleswitch`}
onClick={handleClick}
onKeyPress={handleKeyPress}
onKeyDown={handleOnKeyDown}
role="switch"
tabIndex={0}
type="button"
Expand All @@ -79,7 +80,7 @@ export const ToggleSwitch: FC<ToggleSwitchProps> = ({
className={twMerge(
theme.toggle.base,
theme.toggle.checked[checked ? 'on' : 'off'],
!disabled && checked && theme.toggle.checked.color[color],
checked && theme.toggle.checked.color[color],
)}
/>
{label?.length ? (
Expand Down

1 comment on commit ce55df1

@vercel
Copy link

@vercel vercel bot commented on ce55df1 Sep 24, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.