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

refactor(toggleswitch.tsx): toggleSwitch optional props #928

Merged
merged 2 commits into from
Sep 21, 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={props.switch3} onChange={props.setSwitch3} />
</div>
</CodePreview>

Expand Down
3 changes: 3 additions & 0 deletions app/docs/components/forms/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import FormDocs from './forms.mdx';
const FormsPageContent: FC = () => {
const [switch1, setSwitch1] = useState(false);
const [switch2, setSwitch2] = useState(true);
const [switch3, setSwitch3] = useState(true);

const state = {
switch1,
setSwitch1,
switch2,
setSwitch2,
switch3,
setSwitch3,
};

return (
Expand Down
22 changes: 13 additions & 9 deletions src/components/ToggleSwitch/ToggleSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface FlowbiteToggleSwitchToggleTheme {
export type ToggleSwitchProps = Omit<ComponentProps<'button'>, 'onChange'> & {
checked: boolean;
color?: keyof FlowbiteColors;
label: string;
label?: string;
onChange: (checked: boolean) => void;
theme?: DeepPartial<FlowbiteToggleSwitchTheme>;
};
Expand Down Expand Up @@ -58,7 +58,9 @@ export const ToggleSwitch: FC<ToggleSwitchProps> = ({

return (
<>
{name && checked && <input checked={checked} hidden name={name} readOnly type="checkbox" className="sr-only" />}
{name && checked ? (
<input checked={checked} hidden name={name} readOnly type="checkbox" className="sr-only" />
) : null}
<button
aria-checked={checked}
aria-labelledby={`${id}-flowbite-toggleswitch-label`}
Expand All @@ -80,13 +82,15 @@ export const ToggleSwitch: FC<ToggleSwitchProps> = ({
!disabled && checked && theme.toggle.checked.color[color],
)}
/>
<span
data-testid="flowbite-toggleswitch-label"
id={`${id}-flowbite-toggleswitch-label`}
className={theme.root.label}
>
{label}
</span>
{label?.length ? (
<span
data-testid="flowbite-toggleswitch-label"
id={`${id}-flowbite-toggleswitch-label`}
className={theme.root.label}
>
{label}
</span>
) : null}
</button>
</>
);
Expand Down