import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import Box from '../box/box'; import { TextAlign, DISPLAY, TextVariant, AlignItems, TextColor, } from '../../../helpers/constants/design-system'; import NumericInput from '../numeric-input/numeric-input.component'; import InfoTooltip from '../info-tooltip/info-tooltip'; import { Text } from '../../component-library'; export default function FormField({ dataTestId, titleText = '', TitleTextCustomComponent, titleUnit = '', TitleUnitCustomComponent, tooltipText = '', TooltipCustomComponent, titleDetail = '', titleDetailWrapperProps, titleHeadingWrapperProps, error, onChange = undefined, value = 0, numeric, detailText = '', autoFocus = false, password = false, allowDecimals = false, disabled = false, placeholder, warning, passwordStrength, passwordStrengthText, id, inputProps, wrappingLabelProps, inputRef, }) { return (
{TitleTextCustomComponent || (titleText && ( {titleText} ))} {TitleUnitCustomComponent || (titleUnit && ( {titleUnit} ))} {TooltipCustomComponent || (tooltipText && ( ))} {titleDetail && ( {titleDetail} )}
{numeric ? ( ) : ( onChange(e.target.value)} value={value} type={password ? 'password' : 'text'} autoFocus={autoFocus} disabled={disabled} data-testid={dataTestId} placeholder={placeholder} id={id} ref={inputRef} {...inputProps} /> )} {error && ( {error} )} {warning && ( {warning} )} {passwordStrength && ( {passwordStrength} )} {passwordStrengthText && ( {passwordStrengthText} )}
); } FormField.propTypes = { /** * Identifier for testing purpose */ dataTestId: PropTypes.string, /** * Form Fields Title */ titleText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), /** * A custom component to replace the title text Typography component * titleText will be ignored if this is provided */ TitleTextCustomComponent: PropTypes.node, /** * Show unit (eg. ETH) */ titleUnit: PropTypes.string, /** * A custom component to replace the title unit Typography component * titleUnit will be ignored if this is provided */ TitleUnitCustomComponent: PropTypes.node, /** * Add Tooltip and text content */ tooltipText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), /** * A custom component to replace the tooltip component * tooltipText will be ignored if this is provided */ TooltipCustomComponent: PropTypes.node, /** * Show content (text, image, component) in title */ titleDetail: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), /** * Props to pass to wrapping Box component of the titleDetail component * Accepts all props of the Box component */ titleDetailWrapperProps: PropTypes.shape({ ...Box.propTypes, }), /** * Props to pass to wrapping Box component of the titleHeading component * Accepts all props of the Box component */ titleHeadingWrapperProps: PropTypes.shape({ ...Box.propTypes, }), /** * Show error message */ error: PropTypes.string, /** * Show warning message */ warning: PropTypes.string, /** * Handler when fields change */ onChange: PropTypes.func, /** * Field value */ value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), /** * Show detail text if field mode is numeric */ detailText: PropTypes.string, /** * Set autofocus on render */ autoFocus: PropTypes.bool, /** * Set numeric mode, the default is text */ numeric: PropTypes.bool, /** * Set password mode */ password: PropTypes.bool, /** * Allow decimals on the field */ allowDecimals: PropTypes.bool, /** * Check if the form disabled */ disabled: PropTypes.bool, /** * Set the placeholder text for the input field */ placeholder: PropTypes.string, /** * Show password strength according to the score */ passwordStrength: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), /** * Show password strength description */ passwordStrengthText: PropTypes.string, /** * The id of the input element. Should be used when the wrapping label is changed to a div to ensure accessibility. */ id: PropTypes.string, /** * Any additional input attributes or overrides not provided by exposed props */ inputProps: PropTypes.object, /** * The FormField is wrapped in a Box component that is rendered as a