diff --git a/packages/react-core/src/components/Form/FormGroup.tsx b/packages/react-core/src/components/Form/FormGroup.tsx index 77139da4011..6a63fa728b2 100644 --- a/packages/react-core/src/components/Form/FormGroup.tsx +++ b/packages/react-core/src/components/Form/FormGroup.tsx @@ -3,6 +3,7 @@ import styles from '@patternfly/react-styles/css/components/Form/form'; import { ASTERISK } from '../../helpers/htmlConstants'; import { css } from '@patternfly/react-styles'; import { ValidatedOptions } from '../../helpers/constants'; +import { GenerateId } from '../../helpers/GenerateId/GenerateId'; export interface FormGroupProps extends Omit, 'label'> { /** Anything that can be rendered as FormGroup content. */ @@ -39,8 +40,10 @@ export interface FormGroupProps extends Omit, 'l helperTextIcon?: React.ReactNode; /** Icon displayed to the left of the helper text when the field is invalid. */ helperTextInvalidIcon?: React.ReactNode; - /** ID of the included field. It has to be the same for proper working. */ - fieldId: string; + /** ID of an individual field or a group of multiple fields. Required when a role of "group" or "radiogroup" is passed in. + * If only one field is included, its ID attribute and this prop must be the same. + */ + fieldId?: string; /** Sets the role of the form group. Pass in "radiogroup" when the form group contains multiple * radio inputs, or pass in "group" when the form group contains multiple of any other input type. */ @@ -120,38 +123,46 @@ export const FormGroup: React.FunctionComponent = ({ ); return ( -
- {label && ( + + {randomId => (
- {labelInfo && ( - -
{labelContent}
-
{labelInfo}
-
+ {label && ( +
+ {labelInfo && ( + +
{labelContent}
+
{labelInfo}
+
+ )} + {!labelInfo && labelContent} +
)} - {!labelInfo && labelContent} +
+ {isHelperTextBeforeField && helperTextToDisplay} + {children} + {!isHelperTextBeforeField && helperTextToDisplay} +
)} -
- {isHelperTextBeforeField && helperTextToDisplay} - {children} - {!isHelperTextBeforeField && helperTextToDisplay} -
-
+ ); }; FormGroup.displayName = 'FormGroup'; diff --git a/packages/react-core/src/components/Form/examples/Form.md b/packages/react-core/src/components/Form/examples/Form.md index 3f70a41d01d..5a2edfe76d9 100644 --- a/packages/react-core/src/components/Form/examples/Form.md +++ b/packages/react-core/src/components/Form/examples/Form.md @@ -42,1319 +42,60 @@ import TrashIcon from '@patternfly/react-icons/dist/esm/icons/trash-icon'; ### Basic -```js -import React from 'react'; -import { Form, FormGroup, TextInput, Checkbox, Popover, ActionGroup, Button, Radio } from '@patternfly/react-core'; -import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon'; - -class SimpleForm extends React.Component { - constructor(props) { - super(props); - this.state = { - value1: '', - value2: '', - value3: '' - }; - this.handleTextInputChange1 = value1 => { - this.setState({ value1 }); - }; - this.handleTextInputChange2 = value2 => { - this.setState({ value2 }); - }; - this.handleTextInputChange3 = value3 => { - this.setState({ value3 }); - }; - } - - render() { - const { value1, value2, value3 } = this.state; - - return ( -
- - The{' '} - - name - {' '} - of a{' '} - - Person - - - } - bodyContent={ -
- Often composed of{' '} - - givenName - {' '} - and{' '} - - familyName - - . -
- } - > - - - } - isRequired - fieldId="simple-form-name-01" - helperText="Include your middle name if you have one." - > - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
- ); - } -} +```ts file="./FormBasic.tsx" ``` ### Horizontal -```js -import React from 'react'; -import { - Form, - FormGroup, - TextInput, - TextArea, - FormSelect, - FormSelectOption, - Checkbox, - ActionGroup, - Button, - Radio -} from '@patternfly/react-core'; - -class HorizontalForm extends React.Component { - constructor(props) { - super(props); - this.state = { - value: 'please choose', - value1: '', - value2: '', - value3: '' - }; - this.onChange = (value, event) => { - this.setState({ value }); - }; - this.handleTextInputChange1 = value1 => { - this.setState({ value1 }); - }; - this.handleTextInputChange2 = value2 => { - this.setState({ value2 }); - }; - this.handleTextInputChange3 = value3 => { - this.setState({ value3 }); - }; - this.options = [ - { value: 'select one', label: 'Select one', disabled: false }, - { value: 'mr', label: 'Mr', disabled: false }, - { value: 'miss', label: 'Miss', disabled: false }, - { value: 'mrs', label: 'Mrs', disabled: false }, - { value: 'ms', label: 'Ms', disabled: false }, - { value: 'dr', label: 'Dr', disabled: false }, - { value: 'other', label: 'Other', disabled: false } - ]; - } - - render() { - const { value1, value2, value3 } = this.state; - - return ( -
- - - - - - - - - {this.options.map((option, index) => ( - - ))} - - - -