Skip to content

Commit 5f4e66e

Browse files
andreaTarascioSophanymaman
authored andcommitted
refactor(toggleswitch.tsx): toggleSwitch optional props (themesberg#928)
* refactor(toggleswitch.tsx): toggleSwitch optional props ToggleSwitch optional props themesberg#925 * refactor(better sintax): improved quality of code improved quality of code
1 parent 510d5bc commit 5f4e66e

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

app/docs/components/forms/forms.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ Use the `<ToggleSwitch>` component to ask users to enable or disable an option s
456456
<ToggleSwitch checked={props.switch1} label="Toggle me" onChange={props.setSwitch1} />
457457
<ToggleSwitch checked={props.switch2} label="Toggle me (checked)" onChange={props.setSwitch2} />
458458
<ToggleSwitch checked={false} disabled label="Toggle me (disabled)" onChange={() => undefined} />
459+
<ToggleSwitch checked={props.switch3} onChange={props.setSwitch3} />
459460
</div>
460461
</CodePreview>
461462

app/docs/components/forms/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import FormDocs from './forms.mdx';
88
const FormsPageContent: FC = () => {
99
const [switch1, setSwitch1] = useState(false);
1010
const [switch2, setSwitch2] = useState(true);
11+
const [switch3, setSwitch3] = useState(true);
1112

1213
const state = {
1314
switch1,
1415
setSwitch1,
1516
switch2,
1617
setSwitch2,
18+
switch3,
19+
setSwitch3,
1720
};
1821

1922
return (

src/components/ToggleSwitch/ToggleSwitch.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface FlowbiteToggleSwitchToggleTheme {
2626
export type ToggleSwitchProps = Omit<ComponentProps<'button'>, 'onChange'> & {
2727
checked: boolean;
2828
color?: keyof FlowbiteColors;
29-
label: string;
29+
label?: string;
3030
onChange: (checked: boolean) => void;
3131
theme?: DeepPartial<FlowbiteToggleSwitchTheme>;
3232
};
@@ -58,7 +58,9 @@ export const ToggleSwitch: FC<ToggleSwitchProps> = ({
5858

5959
return (
6060
<>
61-
{name && checked && <input checked={checked} hidden name={name} readOnly type="checkbox" className="sr-only" />}
61+
{name && checked ? (
62+
<input checked={checked} hidden name={name} readOnly type="checkbox" className="sr-only" />
63+
) : null}
6264
<button
6365
aria-checked={checked}
6466
aria-labelledby={`${id}-flowbite-toggleswitch-label`}
@@ -80,13 +82,15 @@ export const ToggleSwitch: FC<ToggleSwitchProps> = ({
8082
!disabled && checked && theme.toggle.checked.color[color],
8183
)}
8284
/>
83-
<span
84-
data-testid="flowbite-toggleswitch-label"
85-
id={`${id}-flowbite-toggleswitch-label`}
86-
className={theme.root.label}
87-
>
88-
{label}
89-
</span>
85+
{label?.length ? (
86+
<span
87+
data-testid="flowbite-toggleswitch-label"
88+
id={`${id}-flowbite-toggleswitch-label`}
89+
className={theme.root.label}
90+
>
91+
{label}
92+
</span>
93+
) : null}
9094
</button>
9195
</>
9296
);

0 commit comments

Comments
 (0)