Skip to content

Commit 3af1490

Browse files
rvsiaHyperkid123
authored andcommitted
feat(pf4): add validated to components supporting it
1 parent 70b14ed commit 3af1490

File tree

7 files changed

+45
-40
lines changed

7 files changed

+45
-40
lines changed

packages/pf4-component-mapper/src/common/form-group.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@ import React from 'react';
22
import { FormGroup as Pf4FormGroup, TextContent, Text } from '@patternfly/react-core';
33
import PropTypes from 'prop-types';
44

5-
const FormGroup = ({ label, isRequired, helperText, meta, description, hideLabel, children, id }) => {
6-
const { error, touched } = meta;
7-
const showError = touched && error;
8-
return (
9-
<Pf4FormGroup
10-
isRequired={isRequired}
11-
label={!hideLabel && label}
12-
fieldId={id}
13-
isValid={!showError}
14-
helperText={helperText}
15-
helperTextInvalid={meta.error}
16-
>
17-
{description && (
18-
<TextContent>
19-
<Text component="small">{description}</Text>
20-
</TextContent>
21-
)}
22-
{children}
23-
</Pf4FormGroup>
24-
);
25-
};
5+
import showError from './show-error';
6+
7+
const FormGroup = ({ label, isRequired, helperText, meta, description, hideLabel, children, id }) => (
8+
<Pf4FormGroup
9+
isRequired={isRequired}
10+
label={!hideLabel && label}
11+
fieldId={id}
12+
helperText={helperText}
13+
helperTextInvalid={meta.error}
14+
{...showError(meta)}
15+
>
16+
{description && (
17+
<TextContent>
18+
<Text component="small">{description}</Text>
19+
</TextContent>
20+
)}
21+
{children}
22+
</Pf4FormGroup>
23+
);
2624

2725
FormGroup.propTypes = {
2826
label: PropTypes.node,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const showError = ({ error, touched }) => ({ validated: touched && error ? 'error' : 'default' });
2+
3+
export default showError;

packages/pf4-component-mapper/src/files/date-picker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TextInput } from '@patternfly/react-core';
33
import PropTypes from 'prop-types';
44
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
55
import FormGroup from '../common/form-group';
6+
import showError from '../common/show-error';
67

78
const DatePicker = (props) => {
89
const { label, isRequired, helperText, meta, description, hideLabel, input, isReadOnly, isDisabled, id, ...rest } = useFieldApi(props);
@@ -16,7 +17,7 @@ const DatePicker = (props) => {
1617
hideLabel={hideLabel}
1718
id={id || input.name}
1819
>
19-
<TextInput {...input} {...rest} type="date" id={id || input.name} isReadOnly={isReadOnly} isDisabled={isDisabled} />
20+
<TextInput {...input} {...rest} type="date" id={id || input.name} isReadOnly={isReadOnly} isDisabled={isDisabled} {...showError(meta)} />
2021
</FormGroup>
2122
);
2223
};

packages/pf4-component-mapper/src/files/dual-list-select.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
Text,
1313
TextVariants,
1414
TextContent,
15-
DataToolbar,
16-
DataToolbarItem,
17-
DataToolbarContent
15+
Toolbar,
16+
ToolbarItem,
17+
ToolbarContent
1818
} from '@patternfly/react-core';
1919

2020
import {
@@ -72,10 +72,10 @@ List.defaultProps = {
7272
value: []
7373
};
7474

75-
const Toolbar = ({ sortTitle, onFilter, onSort, sortDirection, value, placeholder, id }) => (
76-
<DataToolbar className="pf-u-p-0 ddorg__pf4-component-mapper__dual-list-select-toolbar" id={id}>
77-
<DataToolbarContent className="pf-u-p-0 pf-u-pb-md">
78-
<DataToolbarItem>
75+
const InternalToolbar = ({ sortTitle, onFilter, onSort, sortDirection, value, placeholder, id }) => (
76+
<Toolbar className="pf-u-p-0 ddorg__pf4-component-mapper__dual-list-select-toolbar" id={id}>
77+
<ToolbarContent className="pf-u-p-0 pf-u-pb-md">
78+
<ToolbarItem>
7979
<InputGroup>
8080
<TextInput
8181
name="filterOptions"
@@ -90,17 +90,17 @@ const Toolbar = ({ sortTitle, onFilter, onSort, sortDirection, value, placeholde
9090
<SearchIcon />
9191
</Button>
9292
</InputGroup>
93-
</DataToolbarItem>
94-
<DataToolbarItem>
93+
</ToolbarItem>
94+
<ToolbarItem>
9595
<Button onClick={onSort} title={sortTitle} variant="plain">
9696
{!sortDirection ? <PficonSortCommonAscIcon size="md" /> : <PficonSortCommonDescIcon size="md" />}
9797
</Button>
98-
</DataToolbarItem>
99-
</DataToolbarContent>
100-
</DataToolbar>
98+
</ToolbarItem>
99+
</ToolbarContent>
100+
</Toolbar>
101101
);
102102

103-
Toolbar.propTypes = {
103+
InternalToolbar.propTypes = {
104104
sortTitle: PropTypes.node,
105105
onFilter: PropTypes.func.isRequired,
106106
onSort: PropTypes.func.isRequired,
@@ -167,7 +167,7 @@ const DualList = ({
167167
</TextContent>
168168
</GridItem>
169169
<GridItem md={12}>
170-
<Toolbar
170+
<InternalToolbar
171171
sortDirection={state.sortLeftDesc}
172172
onSort={sortOptions}
173173
onFilter={filterOptions}
@@ -238,7 +238,7 @@ const DualList = ({
238238
</TextContent>
239239
</GridItem>
240240
<GridItem md={12}>
241-
<Toolbar
241+
<InternalToolbar
242242
sortDirection={state.sortRightDesc}
243243
onSort={sortValues}
244244
onFilter={filterValues}

packages/pf4-component-mapper/src/files/text-field.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import FormGroup from '../common/form-group';
33
import { TextInput } from '@patternfly/react-core';
44
import PropTypes from 'prop-types';
55
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
6+
import showError from '../common/show-error';
67

78
const TextField = (props) => {
89
const { label, isRequired, helperText, meta, description, hideLabel, input, isReadOnly, isDisabled, id, ...rest } = useFieldApi(props);
@@ -16,7 +17,7 @@ const TextField = (props) => {
1617
hideLabel={hideLabel}
1718
id={id || input.name}
1819
>
19-
<TextInput {...input} {...rest} id={id || input.name} isReadOnly={isReadOnly} isDisabled={isDisabled} />
20+
<TextInput {...input} {...rest} id={id || input.name} isReadOnly={isReadOnly} isDisabled={isDisabled} {...showError(meta)} />
2021
</FormGroup>
2122
);
2223
};

packages/pf4-component-mapper/src/files/textarea.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TextArea as Pf4TextArea } from '@patternfly/react-core';
33
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
44
import PropTypes from 'prop-types';
55
import FormGroup from '../common/form-group';
6+
import showError from '../common/show-error';
67

78
const Textarea = (props) => {
89
const { label, isRequired, helperText, meta, description, hideLabel, input, isReadOnly, isDisabled, id, ...rest } = useFieldApi(props);
@@ -16,7 +17,7 @@ const Textarea = (props) => {
1617
hideLabel={hideLabel}
1718
id={id || input.name}
1819
>
19-
<Pf4TextArea disabled={isDisabled || isReadOnly} {...input} id={id || input.name} {...rest} />
20+
<Pf4TextArea disabled={isDisabled || isReadOnly} {...input} id={id || input.name} {...rest} {...showError(meta)} />
2021
</FormGroup>
2122
);
2223
};

packages/pf4-component-mapper/src/files/time-picker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import FormGroup from '../common/form-group';
33
import { TextInput } from '@patternfly/react-core';
44
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
55
import PropTypes from 'prop-types';
6+
import showError from '../common/show-error';
67

78
const TimePicker = (props) => {
89
const { label, isRequired, helperText, meta, description, hideLabel, input, isReadOnly, isDisabled, id, ...rest } = useFieldApi(props);
@@ -16,7 +17,7 @@ const TimePicker = (props) => {
1617
hideLabel={hideLabel}
1718
id={id || input.name}
1819
>
19-
<TextInput {...input} {...rest} type="time" id={id || input.name} isReadOnly={isReadOnly} isDisabled={isDisabled} />
20+
<TextInput {...input} {...rest} type="time" id={id || input.name} isReadOnly={isReadOnly} isDisabled={isDisabled} {...showError(meta)} />
2021
</FormGroup>
2122
);
2223
};

0 commit comments

Comments
 (0)