elements_Radio_index.js

import { React, PropTypes, classnames } from '@gravityforms/libraries';
import { useStateWithDep } from '@gravityforms/react-utils';
import { spacerClasses, uniqueId } from '@gravityforms/utils';
import Label from '../Label';
import HelpText from '../HelpText';

const { useState, forwardRef } = React;

/**
 * @module Radio
 * @description A radio input component.
 *
 * @since 1.1.15
 *
 * @param {object}                     props                    Component props.
 * @param {JSX.Element}                props.children           React element children.
 * @param {object}                     props.customAttributes   Custom attributes for the component.
 * @param {string|Array|object}        props.customClasses      Custom classes for the component.
 * @param {boolean}                    props.disabled           If radio is disabled.
 * @param {boolean}                    props.externalChecked    If radio is checked or not, can change current checked state if changed.
 * @param {boolean}                    props.externalControl    If radio can be controlled externally.
 * @param {object}                     props.helpTextAttributes Custom attribute for the help text.
 * @param {string}                     props.id                 Optional id. Auto generated if not passed.
 * @param {boolean}                    props.initialChecked     Is it checked on render?
 * @param {object}                     props.labelAttributes    Any custom attributes for the label.
 * @param {string}                     props.name               The name attribute for the radio.
 * @param {Function}                   props.onBlur             On blur function handler.
 * @param {Function}                   props.onChange           On change function handler.
 * @param {Function}                   props.onFocus            On focus function handler.
 * @param {string}                     props.size               The radio size: small (`size-sm`) or medium (`size-md`).
 * @param {string|number|Array|object} props.spacing            The spacing for the component, as a string, number, array, or object.
 * @param {string}                     props.theme              The theme of the radio.
 * @param {string}                     props.type               The radio type. 'standard' or 'image'.
 * @param {string}                     props.value              The radio value.
 * @param {object}                     props.wrapperAttributes  Custom attributes for the wrapper element.
 * @param {string|Array|object}        props.wrapperClasses     Custom classes for the wrapper element.
 * @param {object}                     props.wrapperTagName     Tag to use for the radio wrapper. Defaults to `div`.
 * @param {object|null}                ref                      Ref to the component.
 *
 * @return {JSX.Element} The radio component.
 *
 * @example
 * import Radio from '@gravityforms/components/react/admin/elements/Radio';
 *
 * return <Radio id="radio-id" name="radio-name" onChange={ () => {} } value="radio-value" />;
 *
 */
const Radio = forwardRef( ( {
	children = null,
	customAttributes = {},
	customClasses = [],
	disabled = false,
	externalChecked = false,
	externalControl = false,
	helpTextAttributes = {},
	id = '',
	initialChecked = false,
	labelAttributes = {},
	name = '',
	onBlur = () => {},
	onChange = () => {},
	onFocus = () => {},
	size = 'size-sm',
	spacing = '',
	theme = 'cosmos',
	type = 'standard',
	value = '',
	wrapperAttributes = {},
	wrapperClasses = [],
	wrapperTagName = 'div',
}, ref ) => {
	const [ inputChecked, setInputChecked ] = useState( initialChecked );
	const [ controlChecked, setControlChecked ] = useStateWithDep( externalChecked ); // eslint-disable-line no-unused-vars
	const inputId = id || uniqueId( 'radio' );
	const helpTextId = `${ inputId }-help-text`;

	const wrapperProps = {
		...wrapperAttributes,
		className: classnames( {
			'gform-input-wrapper': true,
			[ `gform-input-wrapper--theme-${ theme }` ]: true,
			[ `gform-input-wrapper--type-${ type }` ]: true,
			'gform-input-wrapper--radio': true,
			'gform-input-wrapper--disabled': disabled,
			...spacerClasses( spacing ),
		}, wrapperClasses ),
		ref,
	};

	const inputProps = {
		...customAttributes,
		checked: externalControl ? controlChecked : inputChecked,
		className: classnames( {
			'gform-input--radio': true,
			[ `gform-input--${ size }` ]: true,
		}, customClasses ),
		disabled: disabled || labelAttributes?.locked === true,
		id: inputId,
		name,
		onBlur,
		onChange: ( e ) => {
			const { checked: inputCheckedState } = e.target;
			if ( ! externalControl ) {
				setInputChecked( inputCheckedState );
			}
			onChange( inputCheckedState, e );
		},
		onFocus,
		type: 'radio',
		value,
	};

	if ( helpTextAttributes.content ) {
		inputProps[ 'aria-describedby' ] = helpTextId;
	}

	const labelProps = {
		...labelAttributes,
		htmlFor: inputId,
	};

	const helpTextProps = {
		...helpTextAttributes,
		id: helpTextId,
	};

	const Container = wrapperTagName;

	return (
		<Container { ...wrapperProps }>
			<input { ...inputProps } />
			<Label { ...labelProps } />
			{
				type === 'image' && (
					<div className="gform-input__radio-image">
						{ children }
					</div>
				)
			}
			{ type !== 'image' && <HelpText { ...helpTextProps } /> }
		</Container>
	);
} );

Radio.propTypes = {
	customAttributes: PropTypes.object,
	customClasses: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.array,
		PropTypes.object,
	] ),
	disabled: PropTypes.bool,
	externalChecked: PropTypes.bool,
	externalControl: PropTypes.bool,
	helpTextAttributes: PropTypes.object,
	id: PropTypes.string,
	initialChecked: PropTypes.bool,
	labelAttributes: PropTypes.object,
	name: PropTypes.string,
	onBlur: PropTypes.func,
	onChange: PropTypes.func,
	onFocus: PropTypes.func,
	size: PropTypes.string,
	spacing: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.number,
		PropTypes.array,
		PropTypes.object,
	] ),
	theme: PropTypes.string,
	value: PropTypes.string,
	wrapperAttributes: PropTypes.object,
	wrapperClasses: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.array,
		PropTypes.object,
	] ),
	wrapperTagName: PropTypes.string,
};

Radio.displayName = 'Radio';

export default Radio;