Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 1x 1x 2x 2x 2x 6x 6x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 8x 2x 2x 8x 2x 2x 8x 4x 4x 1x | import React, {Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import InputLabel from './InputLabel'
import CheckableFieldset from './CheckableFieldset'
const BOOL_PROPS_TO_CLASS_NAMES = {
stacked: ['rev-Checkbox--stacked'],
stackedForSmall: ['rev-Checkbox--stackedForSmall'],
stackedForMedium: ['rev-Checkbox--stackedForMedium'],
}
const BOOL_PROPS = Object.keys(BOOL_PROPS_TO_CLASS_NAMES)
export default class Checkbox extends Component {
static propTypes = {
error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
label: PropTypes.string,
className: PropTypes.string,
}
render() {
const {error, className, label, ...props} = this.props
// Start building the className
const boolClassNames = []
BOOL_PROPS.forEach((name) => {
Iif (props[name]) {
boolClassNames.push(BOOL_PROPS_TO_CLASS_NAMES[name])
}
delete props[name]
})
const labelClassName = classNames(
className,
'rev-Checkbox',
boolClassNames,
{
'is-invalid': !!error,
}
)
const inputClassName = classNames('rev-Checkbox-input', {
'is-invalid-input': !!error,
'is-invalid': !!error,
})
return (
<InputLabel className={labelClassName} error={error}>
<input {...props} className={inputClassName} type="checkbox" />
<span className="rev-Checkbox-label">{label}</span>
</InputLabel>
)
}
}
class CheckboxFieldset extends Component {
static propTypes = {
options: PropTypes.array,
name: PropTypes.string,
value: PropTypes.any,
defaultValue: PropTypes.any,
label: PropTypes.string,
help: PropTypes.string,
error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
onChange: PropTypes.func,
readOnly: PropTypes.bool,
className: PropTypes.string,
}
render() {
const {
className,
options,
name,
value,
defaultValue,
label,
help,
error,
onChange,
readOnly,
...rest
} = this.props
const isControlled = !!value
const hasDefault = !!defaultValue
const valueArray = value || []
const defaultValueArray = defaultValue || []
const valueLookup = {}
const defaultValueLookup = {}
valueArray.forEach((val) => (valueLookup[val] = true))
defaultValueArray.forEach((val) => (defaultValueLookup[val] = true))
/* eslint complexity: [2, 4] */
const checkboxes = options.map((option) => {
const props = {
name,
error,
onChange,
readOnly,
label: option.label,
disabled: option.disabled,
}
if (isControlled) {
props.value = option.value
props.checked = !!valueLookup[option.value]
}
if (hasDefault) {
props.defaultValue = option.defaultValue
props.defaultChecked = !!defaultValueLookup[option.value]
}
return (
<Checkbox
key={option.key || option.value}
className="rev-CheckboxFieldset-checkbox"
{...props}
/>
)
})
const fieldsetClassName = classNames(className, 'rev-CheckboxFieldset')
return (
<CheckableFieldset
{...rest}
className={fieldsetClassName}
help={help}
error={error}
label={label}
>
{checkboxes}
</CheckableFieldset>
)
}
}
Checkbox.Fieldset = CheckboxFieldset
|