2017-05-18 21:21:33 +03:00
|
|
|
import React from 'react';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import { Input } from 'normalized-styled-components';
|
|
|
|
import remcalc from 'remcalc';
|
|
|
|
import unitcalc from 'unitcalc';
|
|
|
|
import rndId from 'rnd-id';
|
|
|
|
import is from 'styled-is';
|
|
|
|
import { Subscriber } from 'react-broadcast';
|
|
|
|
import { bottomShaddow, border, borderRadius } from '../boxes';
|
|
|
|
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 {
|
|
|
|
border-radius: ${remcalc(4)};
|
|
|
|
background-color: ${props => props.theme.white};
|
|
|
|
box-shadow: ${bottomShaddow};
|
|
|
|
color: ${props => props.theme.secondary};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:selected + label {
|
|
|
|
border-radius: ${remcalc(4)};
|
|
|
|
background-color: ${props => props.theme.white};
|
|
|
|
box-shadow: ${bottomShaddow};
|
|
|
|
color: ${props => props.theme.secondary};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:disabled + label {
|
|
|
|
color: ${props => props.theme.grey};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Label = styled.label`
|
|
|
|
${typography.normal};
|
|
|
|
|
|
|
|
position: absolute;
|
|
|
|
box-sizing: border-box;
|
|
|
|
width: ${unitcalc(20)};
|
|
|
|
height: ${unitcalc(8)};
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
text-align: center;
|
|
|
|
padding-top: ${unitcalc(2)};
|
|
|
|
color: ${props => props.theme.text};
|
|
|
|
|
|
|
|
${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;
|
|
|
|
vertical-align: text-bottom;
|
|
|
|
border-top: ${border.unchecked};
|
|
|
|
border-bottom: ${border.unchecked};
|
|
|
|
border-left: ${border.unchecked};
|
|
|
|
&:first-of-type {
|
|
|
|
border-radius: ${borderRadius} 0 0 ${borderRadius};
|
|
|
|
}
|
|
|
|
&:last-of-type {
|
|
|
|
border-radius: 0 ${borderRadius} ${borderRadius} 0;
|
|
|
|
border-right: ${border.unchecked};
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const Ul = styled.ul`
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
box-shadow: ${bottomShaddow};
|
|
|
|
`;
|
|
|
|
|
|
|
|
const BaseToggle = BaseInput(({ children, ...rest }) => {
|
|
|
|
const render = value => {
|
|
|
|
const id = rndId();
|
|
|
|
return (
|
|
|
|
<Li>
|
|
|
|
<InputContainer>
|
|
|
|
<StyledInput
|
|
|
|
checked={value.value === rest.value}
|
|
|
|
{...value}
|
|
|
|
{...rest}
|
|
|
|
id={id}
|
|
|
|
type="radio"
|
|
|
|
/>
|
|
|
|
<Label
|
|
|
|
htmlFor={id}
|
|
|
|
error={rest.error}
|
|
|
|
warning={rest.warning}
|
|
|
|
success={rest.success}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Label>
|
|
|
|
</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);
|