2022-11-25 20:25:03 +00:00
|
|
|
import type { ChangeEvent, ReactNode } from 'react';
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { nanoid } from 'nanoid';
|
|
|
|
import styled from 'styled-components';
|
2020-07-03 21:19:05 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-07-05 00:00:19 +00:00
|
|
|
import Label from '@/components/elements/Label';
|
|
|
|
import Input from '@/components/elements/Input';
|
2020-02-23 04:07:56 +00:00
|
|
|
|
|
|
|
const ToggleContainer = styled.div`
|
|
|
|
${tw`relative select-none w-12 leading-normal`};
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
& > input[type='checkbox'] {
|
2020-02-23 04:07:56 +00:00
|
|
|
${tw`hidden`};
|
|
|
|
|
|
|
|
&:checked + label {
|
|
|
|
${tw`bg-primary-500 border-primary-700 shadow-none`};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:checked + label:before {
|
|
|
|
right: 0.125rem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
& > label {
|
|
|
|
${tw`mb-0 block overflow-hidden cursor-pointer bg-neutral-400 border border-neutral-700 rounded-full h-6 shadow-inner`};
|
|
|
|
transition: all 75ms linear;
|
|
|
|
|
|
|
|
&::before {
|
|
|
|
${tw`absolute block bg-white border h-5 w-5 rounded-full`};
|
|
|
|
top: 0.125rem;
|
|
|
|
right: calc(50% + 0.125rem);
|
|
|
|
//width: 1.25rem;
|
|
|
|
//height: 1.25rem;
|
2022-06-26 19:13:52 +00:00
|
|
|
content: '';
|
2020-02-23 04:07:56 +00:00
|
|
|
transition: all 75ms ease-in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-04-26 00:37:03 +00:00
|
|
|
export interface SwitchProps {
|
2020-02-23 04:07:56 +00:00
|
|
|
name: string;
|
2020-04-26 00:52:32 +00:00
|
|
|
label?: string;
|
2020-04-26 00:37:03 +00:00
|
|
|
description?: string;
|
|
|
|
defaultChecked?: boolean;
|
2022-01-18 03:08:53 +00:00
|
|
|
readOnly?: boolean;
|
2022-11-25 20:25:03 +00:00
|
|
|
onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
|
|
|
|
children?: ReactNode;
|
2020-02-23 04:07:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 03:08:53 +00:00
|
|
|
const Switch = ({ name, label, description, defaultChecked, readOnly, onChange, children }: SwitchProps) => {
|
2022-11-25 20:25:03 +00:00
|
|
|
const uuid = useMemo(() => nanoid(), []);
|
2020-02-23 04:07:56 +00:00
|
|
|
|
|
|
|
return (
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`flex items-center`}>
|
|
|
|
<ToggleContainer css={tw`flex-none`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{children || (
|
|
|
|
<Input
|
|
|
|
id={uuid}
|
|
|
|
name={name}
|
|
|
|
type={'checkbox'}
|
2022-11-25 20:25:03 +00:00
|
|
|
onChange={e => onChange && onChange(e)}
|
2022-06-26 19:13:52 +00:00
|
|
|
defaultChecked={defaultChecked}
|
|
|
|
disabled={readOnly}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Label htmlFor={uuid} />
|
2020-04-26 00:37:03 +00:00
|
|
|
</ToggleContainer>
|
2022-06-26 19:13:52 +00:00
|
|
|
{(label || description) && (
|
|
|
|
<div css={tw`ml-4 w-full`}>
|
|
|
|
{label && (
|
|
|
|
<Label css={[tw`cursor-pointer`, !!description && tw`mb-0`]} htmlFor={uuid}>
|
|
|
|
{label}
|
|
|
|
</Label>
|
|
|
|
)}
|
|
|
|
{description && <p css={tw`text-neutral-400 text-sm mt-2`}>{description}</p>}
|
|
|
|
</div>
|
|
|
|
)}
|
2020-04-26 00:37:03 +00:00
|
|
|
</div>
|
2020-02-23 04:07:56 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Switch;
|