2017-05-18 21:21:33 +03:00
|
|
|
import React from 'react';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import { Input } from 'normalized-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';
|
|
|
|
import unitcalc from 'unitcalc';
|
|
|
|
import rndId from 'rnd-id';
|
|
|
|
import is from 'styled-is';
|
2017-10-09 21:01:34 +03:00
|
|
|
|
2017-10-13 23:02:58 +03:00
|
|
|
import { border, borderRadius } from '../boxes';
|
2017-05-18 21:21:33 +03:00
|
|
|
import Baseline from '../baseline';
|
|
|
|
import BaseInput from './base/input';
|
|
|
|
import typography from '../typography';
|
|
|
|
|
2017-05-25 17:59:58 +03:00
|
|
|
const StyledInput = Input.extend`
|
2017-05-18 21:21:33 +03:00
|
|
|
display: none;
|
|
|
|
|
|
|
|
&:checked + label {
|
|
|
|
color: ${props => props.theme.secondary};
|
2017-10-10 13:15:35 +03:00
|
|
|
font-weight: bold;
|
|
|
|
width: 100%;
|
|
|
|
position: relative;
|
2017-05-18 21:21:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&:selected + label {
|
|
|
|
color: ${props => props.theme.secondary};
|
2017-10-10 13:15:35 +03:00
|
|
|
font-weight: bold;
|
|
|
|
width: 100%;
|
2017-05-18 21:21:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&:disabled + label {
|
|
|
|
color: ${props => props.theme.grey};
|
2017-10-10 13:15:35 +03:00
|
|
|
|
|
|
|
& + .background {
|
|
|
|
display: none;
|
|
|
|
}
|
2017-05-18 21:21:33 +03:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Label = styled.label`
|
|
|
|
${typography.normal};
|
|
|
|
|
|
|
|
box-sizing: border-box;
|
2017-10-10 13:15:35 +03:00
|
|
|
height: 100%;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
2017-05-18 21:21:33 +03:00
|
|
|
color: ${props => props.theme.text};
|
2017-10-10 13:15:35 +03:00
|
|
|
cursor: pointer;
|
|
|
|
user-select: none;
|
|
|
|
position: relative;
|
|
|
|
z-index: 1;
|
2017-05-18 21:21:33 +03:00
|
|
|
|
|
|
|
${is('error')`
|
2017-07-11 17:11:52 +03:00
|
|
|
border-color: ${props => props.theme.redDark}
|
2017-05-18 21:21:33 +03:00
|
|
|
`};
|
|
|
|
|
|
|
|
${is('warning')`
|
2017-07-11 17:11:52 +03:00
|
|
|
border-color: ${props => props.theme.orangeDark}
|
2017-05-18 21:21:33 +03:00
|
|
|
`};
|
|
|
|
|
|
|
|
${is('success')`
|
2017-07-11 17:11:52 +03:00
|
|
|
border-color: ${props => props.theme.greenDark}
|
2017-05-18 21:21:33 +03:00
|
|
|
`};
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
color: ${props => props.theme.secondaryHover};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const InputContainer = styled.div`
|
|
|
|
position: relative;
|
|
|
|
vertical-align: text-bottom;
|
|
|
|
width: ${unitcalc(20)};
|
|
|
|
height: ${unitcalc(8)};
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Li = BaseInput(styled.li`
|
|
|
|
display: inline-block;
|
|
|
|
list-style-type: none;
|
2017-10-10 13:15:35 +03:00
|
|
|
box-sizing: border-box;
|
2017-05-18 21:21:33 +03:00
|
|
|
`);
|
|
|
|
|
2017-10-10 13:15:35 +03:00
|
|
|
const Background = styled.span`
|
|
|
|
border-right: ${border.unchecked};
|
|
|
|
background-color: ${props => props.theme.white};
|
|
|
|
display: block;
|
|
|
|
border-radius: ${borderRadius};
|
|
|
|
position: absolute;
|
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
z-index: 0;
|
|
|
|
transition: ${props => props.theme.transition};
|
|
|
|
border: ${props => `${remcalc(1)} solid ${props.theme.grey}`};
|
|
|
|
top: ${remcalc(-1)};
|
|
|
|
`;
|
|
|
|
|
2017-05-18 21:21:33 +03:00
|
|
|
const Ul = styled.ul`
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
2017-10-10 13:15:35 +03:00
|
|
|
height: ${remcalc(48)};
|
|
|
|
display: inline-flex;
|
|
|
|
align-items: flex-end;
|
|
|
|
background-color: ${props => props.theme.disabled};
|
|
|
|
border-radius: ${borderRadius};
|
|
|
|
border: ${border.unchecked};
|
|
|
|
position: relative;
|
|
|
|
margin-top: ${unitcalc(1)};
|
|
|
|
|
|
|
|
li:first-of-type {
|
|
|
|
input + label + .background {
|
|
|
|
transform: translateX(calc(100% - ${remcalc(3)}));
|
|
|
|
}
|
|
|
|
|
|
|
|
input:checked + label + .background {
|
|
|
|
transform: translateX(${remcalc(-1)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
li:last-of-type .background {
|
|
|
|
display: none;
|
|
|
|
}
|
2017-05-18 21:21:33 +03:00
|
|
|
`;
|
|
|
|
|
|
|
|
const BaseToggle = BaseInput(({ children, ...rest }) => {
|
|
|
|
const render = value => {
|
|
|
|
const id = rndId();
|
|
|
|
return (
|
|
|
|
<Li>
|
|
|
|
<InputContainer>
|
2017-10-10 13:15:35 +03:00
|
|
|
<StyledInput {...value} {...rest} id={id} type="radio" />
|
2017-05-18 21:21:33 +03:00
|
|
|
<Label
|
|
|
|
htmlFor={id}
|
|
|
|
error={rest.error}
|
|
|
|
warning={rest.warning}
|
|
|
|
success={rest.success}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Label>
|
2017-10-10 13:15:35 +03:00
|
|
|
<Background className="background" />
|
2017-05-18 21:21:33 +03:00
|
|
|
</InputContainer>
|
|
|
|
</Li>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
*/
|
2017-08-28 22:21:08 +03:00
|
|
|
const Toggle = ({ children, ...rest }) => (
|
|
|
|
<BaseToggle {...rest}>{children}</BaseToggle>
|
|
|
|
);
|
2017-05-18 21:21:33 +03:00
|
|
|
|
|
|
|
export default Baseline(Toggle);
|
|
|
|
|
|
|
|
export const ToggleList = Baseline(Ul);
|