|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { css } from 'emotion'; |
| 4 | +import { colors, formColors } from '../../../Utils/colors'; |
| 5 | + |
| 6 | +const styles = { |
| 7 | + checkbox: css` |
| 8 | + appearance: none; |
| 9 | + border: 1px solid ${formColors.colorInputBorder}; |
| 10 | + width: 18px; |
| 11 | + height: 18px; |
| 12 | + background: ${formColors.colorInputBg}; |
| 13 | + border-radius: 2px; |
| 14 | + box-shadow: 0 0 0 4px transparent; |
| 15 | + box-sizing: border-box; |
| 16 | + &:focus { |
| 17 | + box-shadow: 0 0 0 4px ${formColors.colorInputFocusShadow}; |
| 18 | + outline: none; |
| 19 | + } |
| 20 | + &:hover { |
| 21 | + border-color: ${formColors.colorInputFg}; |
| 22 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputFg}; |
| 23 | + } |
| 24 | + &:focus:hover { |
| 25 | + box-shadow: 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 26 | + inset 0 0 0 1px ${formColors.colorInputFg}; |
| 27 | + } |
| 28 | + &:checked { |
| 29 | + background-color: ${formColors.colorInputBorderFocus}; |
| 30 | + background-image: url("data:image/svg+xml,%3Csvg width='12' height='10' viewBox='0 0 12 10' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M4.18182 6.96572L1.97655 4.64855L1.79545 4.45826L1.61436 4.64855L0.818904 5.48437L0.654878 5.65672L0.818904 5.82907L4.00072 9.17235L4.18182 9.36263L4.36291 9.17235L11.1811 2.00817L11.3451 1.83582L11.1811 1.66347L10.3856 .827651L10.2045 .637365L10.0234 .82765L4.18182 6.96572Z' fill='white' /%3E%3C/svg%3E"); |
| 31 | + background-position: center; |
| 32 | + background-repeat: no-repeat; |
| 33 | + background-size: 100% 100%; |
| 34 | + border-color: ${formColors.colorInputBorderFocus}; |
| 35 | + } |
| 36 | + &:checked:focus { |
| 37 | + border-color: ${formColors.colorInputBorderFocus}; |
| 38 | + box-shadow: 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 39 | + inset 0 0 0 1px ${formColors.colorInputBorderFocus}; |
| 40 | + } |
| 41 | + &:checked:hover { |
| 42 | + background-color: ${formColors.colorInputFg}; |
| 43 | + border-color: ${formColors.colorInputFg}; |
| 44 | + } |
| 45 | + `, |
| 46 | + checkboxWithError: css` |
| 47 | + background-color: ${formColors.colorInputBg}; |
| 48 | + border-color: ${formColors.colorInputBorderError}; |
| 49 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 50 | + &:hover { |
| 51 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 52 | + } |
| 53 | + &:focus:hover { |
| 54 | + box-shadow: 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 55 | + inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 56 | + } |
| 57 | + &:checked { |
| 58 | + background-color: ${formColors.colorInputBorderError}; |
| 59 | + border-color: ${formColors.colorInputBorderError}; |
| 60 | + } |
| 61 | + &:checked:focus { |
| 62 | + border-color: ${formColors.colorInputBorderError}; |
| 63 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 64 | + } |
| 65 | + &:checked:hover { |
| 66 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 67 | + background-color: ${formColors.colorInputBorderError}; |
| 68 | + border-color: ${formColors.colorInputBorderError}; |
| 69 | + } |
| 70 | + &:checked:focus:hover { |
| 71 | + box-shadow: 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 72 | + inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 73 | + border-color: ${formColors.colorInputBorderError}; |
| 74 | + } |
| 75 | + `, |
| 76 | + label: css` |
| 77 | + color: ${colors.text}; |
| 78 | + `, |
| 79 | + labelRequired: css` |
| 80 | + :after { |
| 81 | + content: ' *'; |
| 82 | + color: ${colors.maximumRed}; |
| 83 | + } |
| 84 | + `, |
| 85 | + labelWithError: css` |
| 86 | + color: ${colors.maximumRed}; |
| 87 | + `, |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * Renders an Input type checkbox. |
| 92 | + * |
| 93 | + * Example: |
| 94 | + * |
| 95 | + * @code |
| 96 | + * <InputCheckbox required={true}>Checkbox</InputCheckbox> |
| 97 | + * @endcode |
| 98 | + */ |
| 99 | +const InputCheckbox = props => { |
| 100 | + const { |
| 101 | + checked, |
| 102 | + children, |
| 103 | + error, |
| 104 | + required, |
| 105 | + htmlAttributes, |
| 106 | + onChange, |
| 107 | + } = props; |
| 108 | + const labelClasses = [styles.label]; |
| 109 | + const cbClasses = [styles.checkbox]; |
| 110 | + if (required) { |
| 111 | + labelClasses.push(styles.labelRequired); |
| 112 | + } |
| 113 | + if (error) { |
| 114 | + labelClasses.push(styles.labelWithError); |
| 115 | + cbClasses.push(styles.checkboxWithError); |
| 116 | + } |
| 117 | + |
| 118 | + const inputId = `${props.fieldName}-cb`; |
| 119 | + |
| 120 | + return ( |
| 121 | + <label |
| 122 | + id={`${props.fieldName}-label`} |
| 123 | + htmlFor={inputId} |
| 124 | + className={css` |
| 125 | + ${labelClasses} |
| 126 | + `} |
| 127 | + {...htmlAttributes} |
| 128 | + > |
| 129 | + <input |
| 130 | + type="checkbox" |
| 131 | + id={inputId} |
| 132 | + defaultChecked={checked} |
| 133 | + name={props.fieldName} |
| 134 | + className={css` |
| 135 | + ${cbClasses} |
| 136 | + `} |
| 137 | + onChange={event => onChange(event.target.checked)} |
| 138 | + /> |
| 139 | + {children} |
| 140 | + </label> |
| 141 | + ); |
| 142 | +}; |
| 143 | + |
| 144 | +InputCheckbox.propTypes = { |
| 145 | + children: PropTypes.oneOfType([ |
| 146 | + PropTypes.arrayOf(PropTypes.node), |
| 147 | + PropTypes.node, |
| 148 | + ]).isRequired, |
| 149 | + |
| 150 | + error: PropTypes.bool, |
| 151 | + |
| 152 | + htmlAttributes: PropTypes.objectOf([ |
| 153 | + PropTypes.string, |
| 154 | + PropTypes.number, |
| 155 | + PropTypes.bool, |
| 156 | + ]), |
| 157 | + |
| 158 | + required: PropTypes.bool, |
| 159 | +}; |
| 160 | + |
| 161 | +InputCheckbox.defaultProps = { |
| 162 | + error: false, |
| 163 | + htmlAttributes: {}, |
| 164 | + required: false, |
| 165 | +}; |
| 166 | + |
| 167 | +export default InputCheckbox; |
0 commit comments