1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/ui/components/component-library/form-text-field/form-text-field.js
Garrett Bear e923110092
Fix/helptext to ts (#20108)
* migrate help-text to TS

delete story with js format

Update ui/components/component-library/help-text/help-text.types.ts

Co-authored-by: George Marshall <georgewrmarshall@gmail.com>

add forwardRef

add ref in forwardRef

final updates to HelpText to TS

update HelpText props

* form text field

* Removing some unused example code

---------

Co-authored-by: taeslee <64485716+taeslee@users.noreply.github.com>
Co-authored-by: georgewrmarshall <george.marshall@consensys.net>
2023-07-21 10:11:35 -07:00

164 lines
3.5 KiB
JavaScript

import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import {
Display,
FlexDirection,
Size,
} from '../../../helpers/constants/design-system';
import Box from '../../ui/box/box';
import { TextField } from '../text-field';
import { HelpText, HelpTextSeverity } from '../help-text';
import { Label } from '../label';
export const FormTextField = ({
autoComplete,
autoFocus,
className,
defaultValue,
disabled,
error,
helpText,
helpTextProps,
id,
inputProps,
inputRef,
label,
labelProps,
startAccessory,
maxLength,
name,
onBlur,
onChange,
onFocus,
placeholder,
readOnly,
required,
endAccessory,
size = Size.MD,
textFieldProps,
truncate,
type = 'text',
value,
...props
}) => (
<Box
className={classnames(
'mm-form-text-field',
{ 'mm-form-text-field--disabled': disabled },
className,
)}
display={Display.Flex}
flexDirection={FlexDirection.Column}
{...props}
>
{label && (
<Label
htmlFor={id}
{...labelProps}
className={classnames(
'mm-form-text-field__label',
labelProps?.className,
)}
>
{label}
</Label>
)}
<TextField
className={classnames(
'mm-form-text-field__text-field',
textFieldProps?.className,
)}
id={id}
{...{
autoComplete,
autoFocus,
defaultValue,
disabled,
error,
id,
inputProps,
inputRef,
startAccessory,
maxLength,
name,
onBlur,
onChange,
onFocus,
placeholder,
readOnly,
required,
endAccessory,
size,
truncate,
type,
value,
...textFieldProps,
}}
/>
{helpText && (
<HelpText
severity={error && HelpTextSeverity.Danger}
marginTop={1}
{...helpTextProps}
className={classnames(
'mm-form-text-field__help-text',
helpTextProps?.className,
)}
>
{helpText}
</HelpText>
)}
</Box>
);
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,
};