|
| 1 | +import React from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import classnames from 'classnames' |
| 4 | +import { convertNameToLabel } from '../helpers' |
| 5 | + |
| 6 | +/** |
| 7 | + * |
| 8 | + * A legend representing a caption for the content of its parent field set element |
| 9 | + * |
| 10 | + * This component must be used as a direct child and the only legend of the <fieldset> element that groups related controls |
| 11 | + * |
| 12 | + * |
| 13 | + * The text of the legend is set using the following rules: |
| 14 | + * - If the `label` prop is set to `false`, the legend is hidden visually via a class. _Note: It's your responsibility to make sure your styling rules respect the `visually-hidden` class_ |
| 15 | + * - Else If the `label` prop is set to a string, the label will display that text |
| 16 | + * - Otherwise, the label will be set using the `name` prop. |
| 17 | + * |
| 18 | + * |
| 19 | + * @name FieldsetLegend |
| 20 | + * @type Function |
| 21 | + * @param {String} name - The name of the associated group |
| 22 | + * @param {String} [hint] - A usage hint for the associated input |
| 23 | + * @param {String|Boolean} [label] - Custom text for the legend |
| 24 | + * @param {Boolean} [required=false] - A boolean value to indicate whether the field is required |
| 25 | + * @param {String} [requiredIndicator=''] - Custom character to denote a field is required |
| 26 | +
|
| 27 | + * @example |
| 28 | + * |
| 29 | + * |
| 30 | + * function ShippingAddress (props) { |
| 31 | + * const name = 'shippingAddress' |
| 32 | + * return ( |
| 33 | + * <fieldset> |
| 34 | + * <FieldsetLegend name={name} /> |
| 35 | + * <Input id={`${name}.name`} input={{name: 'name'}} /> |
| 36 | + * <Input id={`${name}.street`} input={{name: 'street'}} /> |
| 37 | + * <Input id={`${name}.city`}" input={{name: 'city'}} /> |
| 38 | + * <Input id={`${name}.state`} input={{name: 'state'}} /> |
| 39 | + * </fieldset> |
| 40 | + * ) |
| 41 | + * } |
| 42 | + * |
| 43 | + */ |
| 44 | + |
| 45 | +const propTypes = { |
| 46 | + label: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), |
| 47 | + name: PropTypes.string.isRequired, |
| 48 | + required: PropTypes.bool, |
| 49 | + requiredIndicator: PropTypes.string, |
| 50 | + className: PropTypes.string, |
| 51 | + hint: PropTypes.string, |
| 52 | +} |
| 53 | + |
| 54 | +const defaultProps = { |
| 55 | + children: null, |
| 56 | + hint: '', |
| 57 | + label: '', |
| 58 | + required: false, |
| 59 | + requiredIndicator: '', |
| 60 | + className: '', |
| 61 | +} |
| 62 | + |
| 63 | +function FieldsetLegend({ |
| 64 | + label, |
| 65 | + name, |
| 66 | + required, |
| 67 | + requiredIndicator, |
| 68 | + className, |
| 69 | + hint, |
| 70 | +}) { |
| 71 | + return ( |
| 72 | + <legend |
| 73 | + className={classnames(className, { 'visually-hidden': label === false })} |
| 74 | + > |
| 75 | + {label || convertNameToLabel(name)} |
| 76 | + {required && requiredIndicator && ( |
| 77 | + <span className="required-indicator" aria-hidden="true"> |
| 78 | + {requiredIndicator} |
| 79 | + </span> |
| 80 | + )} |
| 81 | + {hint && <i>{hint}</i>} |
| 82 | + </legend> |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +FieldsetLegend.propTypes = propTypes |
| 87 | +FieldsetLegend.defaultProps = defaultProps |
| 88 | + |
| 89 | +export default FieldsetLegend |
0 commit comments