|
| 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 | + radio: css` |
| 8 | + appearance: none; |
| 9 | + border: 1px solid ${formColors.colorInputBorder}; |
| 10 | + width: 18px; |
| 11 | + height: 18px; |
| 12 | + background-color: ${formColors.colorInputBg}; |
| 13 | + background-position: center; |
| 14 | + background-repeat: no-repeat; |
| 15 | + background-size: auto auto; |
| 16 | + border-radius: 50%; |
| 17 | + box-shadow: 0 0 0 4px transparent; |
| 18 | + box-sizing: border-box; |
| 19 | + margin: 0 10px 0 0; |
| 20 | + cursor: pointer; |
| 21 | + display: inline-block; |
| 22 | + vertical-align: text-top; |
| 23 | + &:hover { |
| 24 | + border-color: ${formColors.colorInputFg}; |
| 25 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputFg}; |
| 26 | + } |
| 27 | + &:focus { |
| 28 | + box-shadow: 0 0 0 1px ${formColors.colorInputBg}, |
| 29 | + 0 0 0 4px ${formColors.colorInputFocusShadow}; |
| 30 | + outline: none; |
| 31 | + } |
| 32 | + &:focus:hover { |
| 33 | + box-shadow: 0 0 0 1px ${formColors.colorInputBg}, |
| 34 | + 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 35 | + inset 0 0 0 1px ${formColors.colorInputFg}; |
| 36 | + } |
| 37 | + &:checked { |
| 38 | + background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='10' width='10'%3E%3Ccircle cx='5' cy='5' r='5' fill='%23004ADC' /%3E%3C/svg%3E"); |
| 39 | + border-color: ${formColors.colorInputBorderFocus}; |
| 40 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputBorderFocus}; |
| 41 | + } |
| 42 | + &:checked:focus { |
| 43 | + box-shadow: 0 0 0 1px ${formColors.colorInputBg}, |
| 44 | + 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 45 | + inset 0 0 0 1px ${formColors.colorInputFg}; |
| 46 | + } |
| 47 | + &:checked:hover { |
| 48 | + background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='10' width='10'%3E%3Ccircle cx='5' cy='5' r='5' fill='%23222330' /%3E%3C/svg%3E"); |
| 49 | + border-color: ${formColors.colorInputFg}; |
| 50 | + } |
| 51 | + `, |
| 52 | + radioWithError: css` |
| 53 | + border-color: ${formColors.colorInputBorderError}; |
| 54 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 55 | + &:hover { |
| 56 | + border-color: ${formColors.colorInputFg}; |
| 57 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputFg}; |
| 58 | + } |
| 59 | + &:focus { |
| 60 | + box-shadow: 0 0 0 1px ${formColors.colorInputBg}, |
| 61 | + 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 62 | + inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 63 | + } |
| 64 | + &:focus:hover { |
| 65 | + border-color: ${formColors.colorInputFg}; |
| 66 | + box-shadow: 0 0 0 1px ${formColors.colorInputBg}, |
| 67 | + 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 68 | + inset 0 0 0 1px ${formColors.colorInputFg}; |
| 69 | + } |
| 70 | + &:checked { |
| 71 | + background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='10' width='10'%3E%3Ccircle cx='5' cy='5' r='5' fill='%23D72222' /%3E%3C/svg%3E"); |
| 72 | + border-color: ${formColors.colorInputBorderError}; |
| 73 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 74 | + } |
| 75 | + &:checked:focus { |
| 76 | + border-color: ${formColors.colorInputBorderError}; |
| 77 | + box-shadow: 0 0 0 1px ${formColors.colorInputBg}, |
| 78 | + 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 79 | + inset 0 0 0 1px ${formColors.colorInputBorderError}; |
| 80 | + } |
| 81 | + &:checked:hover { |
| 82 | + background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='10' width='10'%3E%3Ccircle cx='5' cy='5' r='5' fill='%23222330' /%3E%3C/svg%3E"); |
| 83 | + box-shadow: inset 0 0 0 1px ${formColors.colorInputFg}; |
| 84 | + border-color: ${formColors.colorInputFg}; |
| 85 | + } |
| 86 | + &:checked:focus:hover { |
| 87 | + box-shadow: 0 0 0 1px ${formColors.colorInputBg}, |
| 88 | + 0 0 0 4px ${formColors.colorInputFocusShadow}, |
| 89 | + inset 0 0 0 1px ${formColors.colorInputFg}; |
| 90 | + border-color: ${formColors.colorInputFg}; |
| 91 | + } |
| 92 | + `, |
| 93 | + label: css` |
| 94 | + color: ${colors.text}; |
| 95 | + cursor: pointer; |
| 96 | + `, |
| 97 | + labelRequired: css` |
| 98 | + :after { |
| 99 | + content: ' *'; |
| 100 | + color: ${colors.maximumRed}; |
| 101 | + } |
| 102 | + `, |
| 103 | +}; |
| 104 | + |
| 105 | +/** |
| 106 | + * Renders an Input type radio. |
| 107 | + * |
| 108 | + * Example: |
| 109 | + * |
| 110 | + * @code |
| 111 | + * <InputRadio required /> |
| 112 | + * @endcode |
| 113 | + */ |
| 114 | +const InputRadio = props => { |
| 115 | + const { |
| 116 | + checked, |
| 117 | + children, |
| 118 | + error, |
| 119 | + required, |
| 120 | + htmlAttributes, |
| 121 | + onChange, |
| 122 | + } = props; |
| 123 | + const labelClasses = [styles.label]; |
| 124 | + const cbClasses = [styles.radio]; |
| 125 | + if (required) { |
| 126 | + labelClasses.push(styles.labelRequired); |
| 127 | + } |
| 128 | + if (error) { |
| 129 | + cbClasses.push(styles.radioWithError); |
| 130 | + } |
| 131 | + |
| 132 | + const inputId = `${props.fieldName}-cb`; |
| 133 | + |
| 134 | + return ( |
| 135 | + <label |
| 136 | + id={`${props.fieldName}-label`} |
| 137 | + htmlFor={inputId} |
| 138 | + className={css` |
| 139 | + ${labelClasses} |
| 140 | + `} |
| 141 | + {...htmlAttributes} |
| 142 | + > |
| 143 | + <input |
| 144 | + type="radio" |
| 145 | + id={inputId} |
| 146 | + defaultChecked={checked} |
| 147 | + name={props.fieldName} |
| 148 | + className={css` |
| 149 | + ${cbClasses} |
| 150 | + `} |
| 151 | + onChange={event => onChange(event.target.checked)} |
| 152 | + /> |
| 153 | + {children} |
| 154 | + </label> |
| 155 | + ); |
| 156 | +}; |
| 157 | + |
| 158 | +InputRadio.propTypes = { |
| 159 | + /** Content contained within the label element. */ |
| 160 | + children: PropTypes.oneOfType([ |
| 161 | + PropTypes.arrayOf(PropTypes.node), |
| 162 | + PropTypes.node, |
| 163 | + ]).isRequired, |
| 164 | + |
| 165 | + /** true if the label relates to an element with an error. */ |
| 166 | + error: PropTypes.bool, |
| 167 | + |
| 168 | + /** Any additional HTML properties to add to the label element. */ |
| 169 | + htmlAttributes: PropTypes.objectOf([ |
| 170 | + PropTypes.string, |
| 171 | + PropTypes.number, |
| 172 | + PropTypes.bool, |
| 173 | + ]), |
| 174 | + |
| 175 | + /** Content of the html "for" attribute to use for this label. */ |
| 176 | + htmlFor: PropTypes.string.isRequired, |
| 177 | + |
| 178 | + /** True if this label relates to an element that is required. */ |
| 179 | + required: PropTypes.bool, |
| 180 | +}; |
| 181 | + |
| 182 | +InputRadio.defaultProps = { |
| 183 | + error: false, |
| 184 | + htmlAttributes: {}, |
| 185 | + required: false, |
| 186 | +}; |
| 187 | + |
| 188 | +export default InputRadio; |
0 commit comments