add text area option to FormField component

This commit is contained in:
John Mitchell
2020-02-18 14:36:48 -05:00
parent ce909093c0
commit 44cd199078

View File

@@ -1,6 +1,12 @@
import React from 'react';
import PropTypes from 'prop-types';
import { useField } from 'formik';
import {
FormGroup,
TextInput,
TextArea,
Tooltip,
} from '@patternfly/react-core';
import { QuestionCircleIcon as PFQuestionCircleIcon } from '@patternfly/react-icons';
import styled from 'styled-components';
@@ -17,52 +23,81 @@ function FormField(props) {
tooltipMaxWidth,
validate,
isRequired,
type,
...rest
} = props;
const [field, meta] = useField({ name, validate });
const isValid = !(meta.touched && meta.error);
return (
<FormGroup
fieldId={id}
return (
<>
{(type === 'textarea' && (
<FormGroup
fieldId={id}
helperTextInvalid={meta.error}
isRequired={isRequired}
isValid={isValid}
label={label}
>
{tooltip && (
<Tooltip
content={tooltip}
maxWidth={tooltipMaxWidth}
position="right"
>
<QuestionCircleIcon />
</Tooltip>
)}
<TextArea
id={id}
isRequired={isRequired}
isValid={isValid}
label={label}
>
{tooltip && (
<Tooltip
content={tooltip}
maxWidth={tooltipMaxWidth}
position="right"
>
<QuestionCircleIcon />
</Tooltip>
)}
isValid={isValid}
resizeOrientation="vertical"
{...rest}
{...field}
onChange={(value, event) => {
field.onChange(event);
}}
/>
</FormGroup>
)) || (
<FormGroup
fieldId={id}
helperTextInvalid={meta.error}
<TextInput
id={id}
isRequired={isRequired}
isValid={isValid}
{...rest}
{...field}
onChange={(value, event) => {
field.onChange(event);
}}
/>
</FormGroup>
);
}}
</Field>
isRequired={isRequired}
isValid={isValid}
label={label}
>
{tooltip && (
<Tooltip
content={tooltip}
maxWidth={tooltipMaxWidth}
position="right"
>
<QuestionCircleIcon />
</Tooltip>
)}
<TextInput
id={id}
isRequired={isRequired}
isValid={isValid}
{...rest}
{...field}
onChange={(value, event) => {
field.onChange(event);
}}
/>
</FormGroup>
)}
</>
);
}
FormField.propTypes = {
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
label: PropTypes.oneOfType([PropTypes.object, PropTypes.string]).isRequired,
type: PropTypes.string,
validate: PropTypes.func,
isRequired: PropTypes.bool,