|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import requiredIf from 'react-required-if'; |
| 4 | +import Constraints from '../../constraints'; |
| 5 | +import Spacings from '../../spacings'; |
| 6 | +import FieldLabel from '../../field-label'; |
| 7 | +import RadioInput from '../../inputs/radio-input'; |
| 8 | +import getFieldId from '../../../utils/get-field-id'; |
| 9 | +import createSequentialId from '../../../utils/create-sequential-id'; |
| 10 | +import FieldErrors from '../../field-errors'; |
| 11 | +import filterDataAttributes from '../../../utils/filter-data-attributes'; |
| 12 | + |
| 13 | +const sequentialId = createSequentialId('radio-field-'); |
| 14 | + |
| 15 | +const hasErrors = errors => errors && Object.values(errors).some(Boolean); |
| 16 | + |
| 17 | +class RadioField extends React.Component { |
| 18 | + static displayName = 'RadioField'; |
| 19 | + |
| 20 | + static propTypes = { |
| 21 | + // RadioField |
| 22 | + id: PropTypes.string, |
| 23 | + horizontalConstraint: PropTypes.oneOf(['m', 'l', 'xl', 'scale']), |
| 24 | + errors: PropTypes.shape({ |
| 25 | + missing: PropTypes.bool, |
| 26 | + }), |
| 27 | + renderError: PropTypes.func, |
| 28 | + isRequired: PropTypes.bool, |
| 29 | + touched: PropTypes.bool, |
| 30 | + |
| 31 | + // RadioInput |
| 32 | + name: PropTypes.string, |
| 33 | + value: PropTypes.string.isRequired, |
| 34 | + onChange: requiredIf(PropTypes.func, props => !props.isReadOnly), |
| 35 | + onBlur: PropTypes.func, |
| 36 | + onFocus: PropTypes.func, |
| 37 | + isDisabled: PropTypes.bool, |
| 38 | + isReadOnly: PropTypes.bool, |
| 39 | + direction: PropTypes.oneOf(['stack', 'inline']), |
| 40 | + directionProps: PropTypes.object, |
| 41 | + children: PropTypes.node.isRequired, |
| 42 | + |
| 43 | + // LabelField |
| 44 | + title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired, |
| 45 | + hint: requiredIf( |
| 46 | + PropTypes.oneOfType([PropTypes.string, PropTypes.node]), |
| 47 | + props => props.hintIcon |
| 48 | + ), |
| 49 | + description: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), |
| 50 | + onInfoButtonClick: PropTypes.func, |
| 51 | + hintIcon: PropTypes.node, |
| 52 | + badge: PropTypes.node, |
| 53 | + }; |
| 54 | + |
| 55 | + static defaultProps = { |
| 56 | + horizontalConstraint: 'scale', |
| 57 | + }; |
| 58 | + |
| 59 | + state = { |
| 60 | + // We generate an id in case no id is provided by the parent to attach the |
| 61 | + // label to the input component. |
| 62 | + id: this.props.id, |
| 63 | + }; |
| 64 | + |
| 65 | + static getDerivedStateFromProps = (props, state) => ({ |
| 66 | + id: getFieldId(props, state, sequentialId), |
| 67 | + }); |
| 68 | + |
| 69 | + render() { |
| 70 | + const hasError = this.props.touched && hasErrors(this.props.errors); |
| 71 | + return ( |
| 72 | + <Constraints.Horizontal constraint={this.props.horizontalConstraint}> |
| 73 | + <Spacings.Stack scale="xs"> |
| 74 | + <FieldLabel |
| 75 | + title={this.props.title} |
| 76 | + hint={this.props.hint} |
| 77 | + description={this.props.description} |
| 78 | + onInfoButtonClick={this.props.onInfoButtonClick} |
| 79 | + hintIcon={this.props.hintIcon} |
| 80 | + badge={this.props.badge} |
| 81 | + hasRequiredIndicator={this.props.isRequired} |
| 82 | + htmlFor={this.state.id} |
| 83 | + /> |
| 84 | + <RadioInput.Group |
| 85 | + id={this.state.id} |
| 86 | + name={this.props.name} |
| 87 | + value={this.props.value} |
| 88 | + onChange={this.props.onChange} |
| 89 | + onBlur={this.props.onBlur} |
| 90 | + onFocus={this.props.onFocus} |
| 91 | + isDisabled={this.props.isDisabled} |
| 92 | + isReadOnly={this.props.isReadOnly} |
| 93 | + hasError={hasError} |
| 94 | + horizontalConstraint={this.props.horizontalConstraint} |
| 95 | + direction={this.props.direction} |
| 96 | + directionProps={this.props.directionProps} |
| 97 | + {...filterDataAttributes(this.props)} |
| 98 | + > |
| 99 | + {this.props.children} |
| 100 | + </RadioInput.Group> |
| 101 | + <FieldErrors |
| 102 | + errors={this.props.errors} |
| 103 | + isVisible={hasError} |
| 104 | + renderError={this.props.renderError} |
| 105 | + /> |
| 106 | + </Spacings.Stack> |
| 107 | + </Constraints.Horizontal> |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export default RadioField; |
0 commit comments