| 1 |
import { Component } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Radio/radio'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { PickOptional } from '../../helpers/typeUtils'; |
| 5 |
import { getOUIAProps, OUIAProps } from '../../helpers'; |
| 6 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 7 |
|
| 8 |
export interface RadioProps |
| 9 |
extends Omit<React.HTMLProps<HTMLInputElement>, 'disabled' | 'label' | 'onChange' | 'type'>, OUIAProps { |
| 10 |
/** Additional classes added to the radio wrapper. This wrapper will be div element by default. It will be a label element if |
| 11 |
* isLabelWrapped is true, or it can be overridden by any element specified in the component prop. |
| 12 |
*/ |
| 13 |
className?: string; |
| 14 |
/** Additional classes added to the radio input. */ |
| 15 |
inputClassName?: string; |
| 16 |
/** Id of the radio. */ |
| 17 |
id: string; |
| 18 |
/** Flag to indicate whether the radio wrapper element is a native label element for the radio input. Will not apply if a component prop (with a value other than a "label") is specified. */ |
| 19 |
isLabelWrapped?: boolean; |
| 20 |
/** Flag to show if the radio is checked. */ |
| 21 |
checked?: boolean; |
| 22 |
/** Flag to show if the radio is checked. */ |
| 23 |
isChecked?: boolean; |
| 24 |
/** Flag to show if the radio is disabled. */ |
| 25 |
isDisabled?: boolean; |
| 26 |
/** Flag to show if the radio selection is valid or invalid. */ |
| 27 |
isValid?: boolean; |
| 28 |
/** Label text of the radio. */ |
| 29 |
label?: React.ReactNode; |
| 30 |
/** Sets the position of the label. Defaults to 'end' (after the radio input). */ |
| 31 |
labelPosition?: 'start' | 'end'; |
| 32 |
/** Name for group of radios */ |
| 33 |
name: string; |
| 34 |
/** A callback for when the radio selection changes. */ |
| 35 |
onChange?: (event: React.FormEvent<HTMLInputElement>, checked: boolean) => void; |
| 36 |
/** Aria label for the radio. */ |
| 37 |
'aria-label'?: string; |
| 38 |
/** Description text of the radio. */ |
| 39 |
description?: React.ReactNode; |
| 40 |
/** Body of the radio. */ |
| 41 |
body?: React.ReactNode; |
| 42 |
/** Custom aria-describedby value for the radio input. If not provided and description is set, a unique ID will be generated automatically. */ |
| 43 |
'aria-describedby'?: string; |
| 44 |
/** Sets the radio wrapper component to render. Defaults to "div". If set to "label", behaves the same as if isLabelWrapped prop was specified. */ |
| 45 |
component?: React.ElementType; |
| 46 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 47 |
ouiaId?: number | string; |
| 48 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 49 |
ouiaSafe?: boolean; |
| 50 |
} |
| 51 |
|
| 52 |
class Radio extends Component<RadioProps> { |
| 53 |
static displayName = 'Radio'; |
| 54 |
static defaultProps: PickOptional<RadioProps> = { |
| 55 |
className: '', |
| 56 |
isDisabled: false, |
| 57 |
isValid: true, |
| 58 |
onChange: () => {} |
| 59 |
}; |
| 60 |
|
| 61 |
constructor(props: RadioProps) { |
| 62 |
super(props); |
| 63 |
if (!props.label && !props['aria-label']) { |
| 64 |
// eslint-disable-next-line no-console |
| 65 |
console.error('Radio:', 'Radio requires an aria-label to be specified'); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
handleChange = (event: React.FormEvent<HTMLInputElement>) => { |
| 70 |
this.props.onChange(event, event.currentTarget.checked); |
| 71 |
}; |
| 72 |
|
| 73 |
render() { |
| 74 |
const { |
| 75 |
'aria-label': ariaLabel, |
| 76 |
'aria-describedby': ariaDescribedBy, |
| 77 |
checked, |
| 78 |
className, |
| 79 |
inputClassName, |
| 80 |
defaultChecked, |
| 81 |
isLabelWrapped, |
| 82 |
labelPosition = 'end', |
| 83 |
isChecked, |
| 84 |
isDisabled, |
| 85 |
isValid, |
| 86 |
label, |
| 87 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 88 |
onChange, |
| 89 |
description, |
| 90 |
body, |
| 91 |
ouiaId, |
| 92 |
ouiaSafe = true, |
| 93 |
component, |
| 94 |
...props |
| 95 |
} = this.props; |
| 96 |
if (!props.id) { |
| 97 |
// eslint-disable-next-line no-console |
| 98 |
console.error('Radio:', 'id is required to make input accessible'); |
| 99 |
} |
| 100 |
|
| 101 |
return ( |
| 102 |
<SSRSafeIds prefix="pf-radio-description-" ouiaComponentType="Radio"> |
| 103 |
{(descriptionId, generatedOuiaId) => { |
| 104 |
let ariaDescribedByValue: string | undefined; |
| 105 |
if (ariaDescribedBy !== undefined) { |
| 106 |
ariaDescribedByValue = ariaDescribedBy === '' ? undefined : ariaDescribedBy; |
| 107 |
} else if (description) { |
| 108 |
ariaDescribedByValue = descriptionId; |
| 109 |
} |
| 110 |
|
| 111 |
const inputRendered = ( |
| 112 |
<input |
| 113 |
{...props} |
| 114 |
className={css(styles.radioInput, inputClassName)} |
| 115 |
type="radio" |
| 116 |
onChange={this.handleChange} |
| 117 |
aria-invalid={!isValid} |
| 118 |
aria-describedby={ariaDescribedByValue} |
| 119 |
disabled={isDisabled} |
| 120 |
checked={checked || isChecked} |
| 121 |
{...(checked === undefined && { defaultChecked })} |
| 122 |
{...(!label && { 'aria-label': ariaLabel })} |
| 123 |
{...getOUIAProps(Radio.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)} |
| 124 |
/> |
| 125 |
); |
| 126 |
|
| 127 |
const wrapWithLabel = (isLabelWrapped && !component) || component === 'label'; |
| 128 |
|
| 129 |
const Label = wrapWithLabel ? 'span' : 'label'; |
| 130 |
const labelRendered = label ? ( |
| 131 |
<Label |
| 132 |
className={css(styles.radioLabel, isDisabled && styles.modifiers.disabled)} |
| 133 |
htmlFor={!wrapWithLabel ? props.id : undefined} |
| 134 |
> |
| 135 |
{label} |
| 136 |
</Label> |
| 137 |
) : null; |
| 138 |
|
| 139 |
const WrapperComponent = component ?? (wrapWithLabel ? 'label' : 'div'); |
| 140 |
|
| 141 |
return ( |
| 142 |
<WrapperComponent |
| 143 |
className={css(styles.radio, !label && styles.modifiers.standalone, className)} |
| 144 |
htmlFor={wrapWithLabel ? props.id : undefined} |
| 145 |
> |
| 146 |
{labelPosition === 'start' ? ( |
| 147 |
<> |
| 148 |
{labelRendered} |
| 149 |
{inputRendered} |
| 150 |
</> |
| 151 |
) : ( |
| 152 |
<> |
| 153 |
{inputRendered} |
| 154 |
{labelRendered} |
| 155 |
</> |
| 156 |
)} |
| 157 |
{description && ( |
| 158 |
<span id={descriptionId} className={css(styles.radioDescription)}> |
| 159 |
{description} |
| 160 |
</span> |
| 161 |
)} |
| 162 |
{body && <span className={css(styles.radioBody)}>{body}</span>} |
| 163 |
</WrapperComponent> |
| 164 |
); |
| 165 |
}} |
| 166 |
</SSRSafeIds> |
| 167 |
); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
export { Radio }; |