UNPKG

@gravityforms/components

Version:

UI components for use in Gravity Forms development. Both React and vanilla js flavors.

178 lines (164 loc) 5.84 kB
import { React, PropTypes, classnames } from '@gravityforms/libraries'; import { spacerClasses, uniqueId } from '@gravityforms/utils'; import { useStateWithDep } from '@gravityforms/react-utils'; import Label from '../Label'; import HelpText from '../HelpText'; const { useState, forwardRef } = React; /** * @module Textarea * @description A textarea component. * * @since 1.1.18 * * @param {object} props Component props. * @param {boolean} props.controlled Whether the textarea is controlled or not. * @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 textarea is disabled. * @param {object} props.helpTextAttributes Custom attribute for the help text. * @param {string} props.helpTextPosition The position of the help text. * @param {string} props.id Optional id. Auto generated if not passed. * @param {object} props.labelAttributes Custom attributes for the label. * @param {string} props.name The name of the textarea. * @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.placeholder The optional placeholder attribute for the textarea. * @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 textarea. * @param {string} props.value The textarea's initial 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 textarea wrapper. Defaults to `div`. * @param {object|null} ref Ref to the component. * * @return {JSX.Element} The Textarea component. * * @example * import Textarea from '@gravityforms/components/react/admin/elements/Textarea'; * * return ( * <Textarea * id="textarea-id" * placeholder="Textarea placeholder" * onChange={ () => {} } * /> * ); * */ const Textarea = forwardRef( ( { controlled = false, customAttributes = {}, customClasses = [], disabled = false, helpTextAttributes = {}, helpTextPosition = 'above', id = '', labelAttributes = {}, name = '', onBlur = () => {}, onChange = () => {}, onFocus = () => {}, placeholder = '', spacing = '', theme = 'cosmos', value = '', wrapperAttributes = {}, wrapperClasses = [], wrapperTagName = 'div', }, ref ) => { const [ stateInputValue, setStateInputValue ] = useState( value ); const [ controlInputValue, setControlInputValue ] = useStateWithDep( value ); const inputValue = controlled ? controlInputValue : stateInputValue; const inputId = id || uniqueId( 'gform-textarea' ); const helpTextId = `${ inputId }-help-text`; const wrapperProps = { ...wrapperAttributes, className: classnames( { 'gform-input-wrapper': true, 'gform-input-wrapper--textarea': true, 'gform-input-wrapper--disabled': disabled, [ `gform-input-wrapper--theme-${ theme }` ]: true, ...spacerClasses( spacing ), }, wrapperClasses ), ref, }; const inputProps = { ...customAttributes, className: classnames( { 'gform-input': true, 'gform-input--textarea': true, }, customClasses ), disabled: disabled || labelAttributes?.locked === true, id: inputId, name, onBlur, onChange: ( e ) => { const { value: newInputValue } = e.target; setStateInputValue( newInputValue ); setControlInputValue( newInputValue ); onChange( newInputValue, e ); }, onFocus, value: inputValue, }; if ( placeholder ) { inputProps.placeholder = placeholder; } if ( helpTextAttributes.content ) { inputProps[ 'aria-describedby' ] = helpTextId; } const labelProps = { ...labelAttributes, htmlFor: inputId, }; const helpTextProps = { ...helpTextAttributes, id: helpTextId, }; const Container = wrapperTagName; return ( <Container { ...wrapperProps }> <Label { ...labelProps } /> { helpTextPosition === 'above' && <HelpText { ...helpTextProps } /> } <textarea { ...inputProps } /> { helpTextPosition === 'below' && <HelpText { ...helpTextProps } /> } </Container> ); } ); Textarea.propTypes = { controlled: PropTypes.bool, customAttributes: PropTypes.object, customClasses: PropTypes.oneOfType( [ PropTypes.string, PropTypes.array, PropTypes.object, ] ), disabled: PropTypes.bool, helpTextAttributes: PropTypes.object, helpTextPosition: PropTypes.string, id: PropTypes.string, labelAttributes: PropTypes.object, name: PropTypes.string, onBlur: PropTypes.func, onChange: PropTypes.func, onFocus: PropTypes.func, placeholder: 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, }; Textarea.displayName = 'Textarea'; export default Textarea;