joyent-portal/ui/src/components/widget/index.js

107 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-10-28 16:59:28 +03:00
const React = require('react');
2017-02-15 03:19:26 +02:00
const composers = require('../../shared/composers');
2016-12-09 12:40:57 +02:00
const constants = require('../../shared/constants');
const fns = require('../../shared/functions');
2016-12-09 12:40:57 +02:00
const Styled = require('styled-components');
const {
boxes
} = constants;
2017-02-15 03:19:26 +02:00
const {
Baseline
} = composers;
const {
2017-01-12 21:04:52 +02:00
rndId,
remcalc
} = fns;
2016-12-09 12:40:57 +02:00
const {
2017-02-20 16:51:54 +02:00
default: styled
2016-12-09 12:40:57 +02:00
} = Styled;
const classNames = {
content: rndId()
};
2016-12-09 12:40:57 +02:00
const StyledInput = styled.input`
display: none;
visibility: hidden;
&:checked {
& ~ .${classNames.content} {
2016-12-09 12:40:57 +02:00
border: ${boxes.border.checked};
}
}
&:disabled {
& ~ .${classNames.content} {
2016-12-09 12:40:57 +02:00
cursor: not-allowed;
filter: grayscale(80%);
opacity: 0.4;
}
}
`;
const StyledContent = styled.div`
border: ${boxes.border.unchecked};
2017-01-12 21:04:52 +02:00
border-radius: ${remcalc(4)};
2016-12-09 12:40:57 +02:00
cursor: pointer;
padding: ${remcalc(36)};
2016-12-09 12:40:57 +02:00
& img {
max-width: 100%;
}
`;
2016-10-28 16:59:28 +03:00
const Widget = ({
checked = false,
2016-10-28 16:59:28 +03:00
children,
className,
disabled = false,
id,
name,
selectable = 'single',
style,
value = name
2016-10-28 16:59:28 +03:00
}) => {
const type = selectable === 'single' ? 'radio' : 'checkbox';
return (
<label
className={className}
htmlFor={value}
id={id}
>
2016-12-09 12:40:57 +02:00
<StyledInput
checked={checked}
disabled={disabled}
id={value}
2016-10-28 16:59:28 +03:00
name={name}
type={type}
value={value}
2016-10-28 16:59:28 +03:00
/>
<StyledContent className={classNames.content}>
2016-10-28 16:59:28 +03:00
{children}
2016-12-09 12:40:57 +02:00
</StyledContent>
2016-10-28 16:59:28 +03:00
</label>
);
};
Widget.propTypes = {
checked: React.PropTypes.bool,
2016-10-28 16:59:28 +03:00
children: React.PropTypes.object,
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
id: React.PropTypes.string,
2016-10-28 16:59:28 +03:00
name: React.PropTypes.string,
selectable: React.PropTypes.string,
style: React.PropTypes.object,
value: React.PropTypes.string
2016-10-28 16:59:28 +03:00
};
2017-02-15 03:19:26 +02:00
module.exports = Baseline(
Widget
);