UNPKG

2.41 kBJavaScriptView Raw
1/* https://codepen.io/rafacdb/pen/jwRWmM */
2import React from 'react';
3import PropTypes from 'prop-types';
4import styled from 'styled-components';
5
6const CheckboxStyled = styled.div`
7 height: ${props => props.theme.bahFormInputDefaultHeight};
8 padding: ${props => props.theme.bahFormInputRadioCheckDefaultPadding};
9 vertical-align: middle;
10 width: 100%;
11 margin-bottom: ${props => props.theme.bahFormFieldsDefaultMarginBottom};
12
13 /* Hide base elements */
14 input[type="checkbox"] {
15 position: absolute;
16 left: -9999px;
17 }
18
19 /* Position the <label> */
20 input[type="checkbox"] + label {
21 position: relative;
22 display: inline-block;
23 height: 17px;
24 line-height: 17px;
25 padding-left: 30px;
26 cursor: pointer;
27 }
28
29 /* SHARED STYLES - Radio & Checkbox - Unchecked */
30 input[type="checkbox"] + label:before {
31 content: '';
32 position: absolute;
33 left: 0;
34 top: 50%;
35 transform: translateY(-50%);
36 width: 18px;
37 height: 18px;
38 border: 2px solid ${props => props.theme.bahFormInputBorderColor};
39 background-color: ${props => props.theme.bahFormInputBackgroundColor};
40 }
41
42 /* CHECKBOX - Unchecked */
43 input[type="checkbox"]:not(:checked) + label:before {
44 /* Add styles if needed */
45 }
46
47 /* CHECKBOX - Checked ( CSS Tick ) */
48 input[type="checkbox"]:checked + label:after {
49 position: absolute;
50 content: "";
51 left: 7px;
52 top: 0px;
53 -webkit-transform: rotate(45deg);
54 transform: rotate(45deg);
55 /* Short Arm */
56 width: 5px;
57 border-bottom: 3px solid ${props => props.theme.bahFormInputBorderColor};
58 /* Long Arm */
59 height: 11px;
60 border-right: 3px solid ${props => props.theme.bahFormInputBorderColor};
61 }
62
63 /* SHARED STYLES - Radio & Checkbox - Disabled */
64 input[type="checkbox"]:disabled + label:before {
65 border-color: #bbb;
66 background-color: #ddd;
67 }
68
69 input[type="checkbox"]:disabled + label {
70 color: #999;
71 }
72`;
73
74const Checkbox = ({ children, disabled, name, id }) =>
75 (<CheckboxStyled>
76 <input type={'checkbox'} disabled={disabled} name={name} id={id} />
77 <label htmlFor={id}>
78 {children}
79 </label>
80 </CheckboxStyled>);
81
82Checkbox.defaultProps = {
83 children: '',
84 name: '',
85 id: '',
86 disabled: false,
87};
88
89Checkbox.propTypes = {
90 children: PropTypes.string,
91 name: PropTypes.string,
92 id: PropTypes.string,
93 disabled: PropTypes.bool,
94};
95
96export default Checkbox;
97
\No newline at end of file