2022-11-22 22:04:27 +01:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
import classnames from 'classnames' ;
import {
DISPLAY ,
FLEX _DIRECTION ,
2023-02-16 21:42:15 +01:00
SEVERITIES ,
2023-02-02 21:15:26 +01:00
Size ,
2022-11-22 22:04:27 +01:00
} from '../../../helpers/constants/design-system' ;
import Box from '../../ui/box/box' ;
import { TextField } from '../text-field' ;
import { HelpText } from '../help-text' ;
import { Label } from '../label' ;
export const FormTextField = ( {
autoComplete ,
autoFocus ,
className ,
defaultValue ,
disabled ,
error ,
helpText ,
helpTextProps ,
id ,
inputProps ,
inputRef ,
label ,
labelProps ,
2023-02-16 00:43:51 +01:00
startAccessory ,
2022-11-22 22:04:27 +01:00
maxLength ,
name ,
onBlur ,
onChange ,
onFocus ,
placeholder ,
readOnly ,
required ,
2023-02-16 00:43:51 +01:00
endAccessory ,
2023-02-02 21:15:26 +01:00
size = Size . MD ,
2022-11-22 22:04:27 +01:00
textFieldProps ,
truncate ,
type = 'text' ,
value ,
... props
} ) => (
< Box
className = { classnames (
'mm-form-text-field' ,
{ 'mm-form-text-field--disabled' : disabled } ,
className ,
) }
display = { DISPLAY . FLEX }
flexDirection = { FLEX _DIRECTION . COLUMN }
{ ... props }
>
{ label && (
< Label
htmlFor = { id }
{ ... labelProps }
2023-02-15 15:29:26 +01:00
className = { classnames (
'mm-form-text-field__label' ,
labelProps ? . className ,
) }
2022-11-22 22:04:27 +01:00
>
{ label }
< / L a b e l >
) }
< TextField
className = { classnames (
'mm-form-text-field__text-field' ,
textFieldProps ? . className ,
) }
id = { id }
{ ... {
autoComplete ,
autoFocus ,
defaultValue ,
disabled ,
error ,
id ,
inputProps ,
inputRef ,
2023-02-16 00:43:51 +01:00
startAccessory ,
2022-11-22 22:04:27 +01:00
maxLength ,
name ,
onBlur ,
onChange ,
onFocus ,
placeholder ,
readOnly ,
required ,
2023-02-16 00:43:51 +01:00
endAccessory ,
2022-11-22 22:04:27 +01:00
size ,
truncate ,
type ,
value ,
... textFieldProps ,
} }
/ >
{ helpText && (
< HelpText
2023-02-16 21:42:15 +01:00
severity = { error && SEVERITIES . DANGER }
2023-02-15 15:29:26 +01:00
marginTop = { 1 }
{ ... helpTextProps }
2022-11-22 22:04:27 +01:00
className = { classnames (
'mm-form-text-field__help-text' ,
helpTextProps ? . className ,
) }
>
{ helpText }
< / H e l p T e x t >
) }
< / B o x >
) ;
FormTextField . propTypes = {
/ * *
* An additional className to apply to the form - text - field
* /
className : PropTypes . string ,
/ * *
* The id of the FormTextField
* Required if label prop exists to ensure accessibility
*
* @ param { object } props - The props passed to the component .
* @ param { string } propName - The prop name in this case 'id' .
* @ param { string } componentName - The name of the component .
* /
id : ( props , propName , componentName ) => {
if ( props . label && ! props [ propName ] ) {
return new Error (
` If a label prop exists you must provide an ${ propName } prop for the label's htmlFor attribute for accessibility. Warning coming from ${ componentName } ui/components/component-library/form-text-field/form-text-field.js ` ,
) ;
}
return null ;
} ,
/ * *
* The content of the Label component
* /
label : PropTypes . string ,
/ * *
* Props that are applied to the Label component
* /
labelProps : PropTypes . object ,
/ * *
* The content of the HelpText component
* /
helpText : PropTypes . string ,
/ * *
* Props that are applied to the HelpText component
* /
helpTextProps : PropTypes . object ,
/ * *
* Props that are applied to the TextField component
* /
textFieldProps : PropTypes . object ,
/ * *
* FormTextField accepts all the props from TextField and Box
* /
... TextField . propTypes ,
} ;