2017-05-18 21:21:33 +03:00
|
|
|
import React from 'react';
|
|
|
|
import styled from 'styled-components';
|
2017-10-10 13:15:35 +03:00
|
|
|
import { Subscriber } from 'joy-react-broadcast';
|
2017-05-18 21:21:33 +03:00
|
|
|
import remcalc from 'remcalc';
|
2018-01-09 16:57:00 +02:00
|
|
|
import isUndefined from 'lodash.isundefined';
|
2017-12-06 17:03:40 +02:00
|
|
|
import Label from './label';
|
2017-05-18 21:21:33 +03:00
|
|
|
import is from 'styled-is';
|
2017-10-09 21:01:34 +03:00
|
|
|
|
2017-05-18 21:21:33 +03:00
|
|
|
import BaseInput from './base/input';
|
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
const InputContainer = styled.div`
|
|
|
|
position: relative;
|
|
|
|
vertical-align: text-bottom;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
`;
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
const Input = styled.input`
|
|
|
|
display: none;
|
2017-10-10 13:15:35 +03:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:checked + label {
|
|
|
|
background: #3B46CC;
|
|
|
|
border: ${remcalc(1)} solid ${props => props.theme.primary};
|
|
|
|
|
|
|
|
&:after {
|
|
|
|
left: 50%;
|
|
|
|
box-shadow: 0 0 0 ${remcalc(1)} ${props => props.theme.primary};
|
|
|
|
}
|
2018-01-09 16:57:00 +02:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:active {
|
|
|
|
box-shadow: none;
|
|
|
|
&:after {
|
|
|
|
margin-left: -0${remcalc(12)}
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 13:15:35 +03:00
|
|
|
}
|
2017-05-18 21:21:33 +03:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
const InputLabel = styled.label`
|
|
|
|
outline: 0;
|
|
|
|
display: block;
|
|
|
|
width: ${remcalc(46)};
|
|
|
|
height: ${remcalc(24)};
|
|
|
|
position: relative;
|
2017-10-10 13:15:35 +03:00
|
|
|
cursor: pointer;
|
|
|
|
user-select: none;
|
2017-12-06 17:03:40 +02:00
|
|
|
box-sizing: border-box;
|
|
|
|
background: ${props => props.theme.background};
|
|
|
|
border-radius: ${remcalc(23)};
|
|
|
|
transition: all 0.3s ease;
|
|
|
|
border: ${remcalc(1)} solid ${props => props.theme.grey};
|
|
|
|
margin-right: ${remcalc(6)};
|
|
|
|
|
|
|
|
&::selection {
|
|
|
|
background: none;
|
|
|
|
}
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:active {
|
|
|
|
box-shadow: inset 0 0 0 ${remcalc(24)} ${props => props.theme.grey};
|
|
|
|
&:after {
|
|
|
|
padding-right: ${remcalc(12)};
|
|
|
|
}
|
2017-05-18 21:21:33 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:hover {
|
|
|
|
border: ${remcalc(1)} solid ${props => props.theme.primary};
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:after {
|
|
|
|
box-shadow: 0 0 0 ${remcalc(1)} ${props => props.theme.primary};
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:after,
|
|
|
|
&:before {
|
|
|
|
position: relative;
|
|
|
|
display: block;
|
|
|
|
content: '';
|
|
|
|
width: 50%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
2017-10-10 13:15:35 +03:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:after {
|
|
|
|
left: 0;
|
|
|
|
border-radius: 2em;
|
|
|
|
background: ${props => props.theme.white};
|
2018-01-17 21:10:39 +02:00
|
|
|
transition: left 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275),
|
|
|
|
padding 0.3s ease, margin 0.3s ease, box-shadow 0.3s ease;
|
2017-10-10 13:15:35 +03:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
box-shadow: 0 0 0 ${remcalc(1)} ${props => props.theme.grey};
|
|
|
|
}
|
2017-10-10 13:15:35 +03:00
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:active {
|
|
|
|
box-shadow: inset 0 0 0 2em ${props => props.theme.grey};
|
|
|
|
&:after {
|
|
|
|
padding-right: ${remcalc(12)};
|
2017-10-10 13:15:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:before {
|
2017-10-10 13:15:35 +03:00
|
|
|
display: none;
|
|
|
|
}
|
2017-12-06 17:03:40 +02:00
|
|
|
${is('disabled')`
|
2018-01-16 20:21:16 +02:00
|
|
|
cursor: not-allowed;
|
|
|
|
|
2017-12-06 17:03:40 +02:00
|
|
|
&:active {
|
|
|
|
box-shadow: none;
|
|
|
|
&:after {
|
|
|
|
padding-right: 0;
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 20:21:16 +02:00
|
|
|
&:before {
|
|
|
|
cursor: not-allowed;
|
|
|
|
}
|
2017-12-06 17:03:40 +02:00
|
|
|
&:after {
|
2018-01-16 20:21:16 +02:00
|
|
|
cursor: not-allowed;
|
2017-12-06 17:03:40 +02:00
|
|
|
background: ${props => props.theme.whiteActive};
|
|
|
|
}
|
|
|
|
&:hover {
|
|
|
|
border: ${remcalc(1)} solid ${props => props.theme.grey};
|
|
|
|
|
2018-01-09 16:57:00 +02:00
|
|
|
&:after {
|
2017-12-06 17:03:40 +02:00
|
|
|
box-shadow: 0 0 0 ${remcalc(1)} ${props => props.theme.grey};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`};
|
2017-05-18 21:21:33 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const BaseToggle = BaseInput(({ children, ...rest }) => {
|
|
|
|
const render = value => {
|
2018-01-09 16:57:00 +02:00
|
|
|
const checked =
|
|
|
|
isUndefined(rest.value) && isUndefined(rest.checked)
|
|
|
|
? undefined
|
|
|
|
: rest.value === true || rest.checked === true;
|
|
|
|
|
2017-05-18 21:21:33 +03:00
|
|
|
return (
|
2017-12-06 17:03:40 +02:00
|
|
|
<InputContainer>
|
2018-01-09 16:57:00 +02:00
|
|
|
<Input {...rest} checked={checked} type="checkbox" />
|
2017-12-06 17:03:40 +02:00
|
|
|
<InputLabel
|
2018-01-16 20:21:16 +02:00
|
|
|
{...rest}
|
2018-01-09 16:57:00 +02:00
|
|
|
htmlFor={rest.id}
|
2017-12-06 17:03:40 +02:00
|
|
|
error={rest.error}
|
|
|
|
warning={rest.warning}
|
|
|
|
success={rest.success}
|
|
|
|
/>
|
2018-01-16 20:21:16 +02:00
|
|
|
<Label {...rest}> {children}</Label>
|
2017-12-06 17:03:40 +02:00
|
|
|
</InputContainer>
|
2017-05-18 21:21:33 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-08-28 22:21:08 +03:00
|
|
|
return <Subscriber channel="input-group">{render}</Subscriber>;
|
2017-05-18 21:21:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @example ./usage-toggle.md
|
|
|
|
*/
|
2018-01-17 21:10:39 +02:00
|
|
|
const Toggle = ({ children, ...rest }) => (
|
|
|
|
<BaseToggle {...rest}>{children}</BaseToggle>
|
|
|
|
);
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-10-23 17:09:49 +03:00
|
|
|
export default Toggle;
|