Skip to content

Commit

Permalink
refactor(toggleswitch.tsx): toggleSwitch optional props (#928)
Browse files Browse the repository at this point in the history
* refactor(toggleswitch.tsx): toggleSwitch optional props

ToggleSwitch optional props

#925

* refactor(better sintax): improved quality of code

improved quality of code
  • Loading branch information
andreaTarascioSophany authored Sep 21, 2023
1 parent 3d1d62e commit 3e443fc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 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={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

1 comment on commit 3e443fc

@vercel
Copy link

@vercel vercel bot commented on 3e443fc Sep 21, 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.