2020-02-23 04:07:56 +00:00
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import v4 from 'uuid/v4';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
const ToggleContainer = styled.div`
|
|
|
|
${tw`relative select-none w-12 leading-normal`};
|
|
|
|
|
|
|
|
& > input[type="checkbox"] {
|
|
|
|
${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;
|
|
|
|
content: "";
|
|
|
|
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;
|
|
|
|
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
|
|
children?: React.ReactNode;
|
2020-02-23 04:07:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 00:37:03 +00:00
|
|
|
const Switch = ({ name, label, description, defaultChecked, onChange, children }: SwitchProps) => {
|
2020-02-23 04:07:56 +00:00
|
|
|
const uuid = useMemo(() => v4(), []);
|
|
|
|
|
|
|
|
return (
|
2020-04-26 00:37:03 +00:00
|
|
|
<div className={'flex items-center'}>
|
2020-04-26 00:52:32 +00:00
|
|
|
<ToggleContainer className={'flex-none'}>
|
2020-04-26 00:37:03 +00:00
|
|
|
{children
|
|
|
|
|| <input
|
|
|
|
id={uuid}
|
|
|
|
name={name}
|
|
|
|
type={'checkbox'}
|
|
|
|
onChange={e => onChange && onChange(e)}
|
|
|
|
defaultChecked={defaultChecked}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
<label htmlFor={uuid}/>
|
|
|
|
</ToggleContainer>
|
2020-04-26 00:52:32 +00:00
|
|
|
{(label || description) &&
|
|
|
|
<div className={'ml-4 w-full'}>
|
|
|
|
{label &&
|
2020-04-26 00:37:03 +00:00
|
|
|
<label
|
|
|
|
className={classNames('input-dark-label cursor-pointer', { 'mb-0': !!description })}
|
|
|
|
htmlFor={uuid}
|
|
|
|
>{label}</label>
|
2020-04-26 00:52:32 +00:00
|
|
|
}
|
2020-04-26 00:37:03 +00:00
|
|
|
{description &&
|
|
|
|
<p className={'input-help'}>
|
|
|
|
{description}
|
|
|
|
</p>
|
|
|
|
}
|
2020-02-23 04:07:56 +00:00
|
|
|
</div>
|
2020-04-26 00:52:32 +00:00
|
|
|
}
|
2020-04-26 00:37:03 +00:00
|
|
|
</div>
|
2020-02-23 04:07:56 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Switch;
|