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

fix(toggleswitch): disable + checked state and deprecated html event replacement #987

Merged
merged 4 commits into from
Sep 24, 2023
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
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 @@

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 @@
disabled={disabled}
id={`${id}-flowbite-toggleswitch`}
onClick={handleClick}
onKeyPress={handleKeyPress}
onKeyDown={handleOnKeyDown}
role="switch"
tabIndex={0}
type="button"
Expand All @@ -79,7 +80,7 @@
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 All @@ -90,7 +91,7 @@
>
{label}
</span>
) : null}

Check warning on line 94 in src/components/ToggleSwitch/ToggleSwitch.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/ToggleSwitch/ToggleSwitch.tsx#L94

Added line #L94 was not covered by tests
</button>
</>
);
Expand Down
Loading