2020-01-23 18:43:04 +00:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
|
|
|
import Switch from 'react-switch';
|
2020-05-06 00:08:17 +00:00
|
|
|
import styled from 'styled-components';
|
2020-01-23 18:43:04 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
className?: string,
|
|
|
|
checked?: boolean,
|
|
|
|
disabled?: boolean,
|
|
|
|
onChange(checked: boolean): void | Promise<void>,
|
|
|
|
};
|
|
|
|
|
2020-05-06 00:08:17 +00:00
|
|
|
const ThemedSwitch: React.ComponentType<{ checked: boolean }> = styled.div`
|
|
|
|
.react-switch-bg {
|
2020-06-16 19:46:13 +00:00
|
|
|
background: ${({ checked }) => (checked ? 'var(--color-surprise)' : 'var(--hl-xl)')} !important;
|
2020-05-06 00:08:17 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-06-13 04:56:15 +00:00
|
|
|
const ToggleSwitch: React.StatelessFunctionalComponent<Props> = ({
|
|
|
|
className,
|
|
|
|
checked: checkedProp,
|
|
|
|
onChange,
|
|
|
|
disabled,
|
|
|
|
}) => {
|
|
|
|
const [checked, setChecked] = React.useState(!!checkedProp);
|
|
|
|
|
|
|
|
// If prop changes and differs from state, update state
|
|
|
|
React.useEffect(() => {
|
|
|
|
setChecked(!!checkedProp);
|
|
|
|
}, [checkedProp]);
|
|
|
|
|
|
|
|
const callback = React.useCallback(
|
|
|
|
c => {
|
|
|
|
setChecked(c);
|
|
|
|
onChange(c);
|
|
|
|
},
|
|
|
|
[onChange],
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ThemedSwitch checked={checked}>
|
|
|
|
<Switch
|
|
|
|
className={className}
|
|
|
|
checked={checked}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={callback}
|
|
|
|
height={20}
|
|
|
|
width={40}
|
|
|
|
/>
|
|
|
|
</ThemedSwitch>
|
|
|
|
);
|
2020-01-23 18:43:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ToggleSwitch;
|