Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make InputGroup label reflect required prop (#510) #511

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/components/InputGroup/InputGroup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const InputGroup = ({
isLabelVisible,
label,
layout,
required,
size,
validationTexts,
...restProps
Expand Down Expand Up @@ -57,6 +58,7 @@ export const InputGroup = ({
? styles.isRootLayoutHorizontal
: styles.isRootLayoutVertical,
disabled && styles.isRootDisabled,
required && styles.isRootRequired,
getRootSizeClassName(size, styles),
getRootValidationStateClassName(validationState, styles),
)}
Expand Down Expand Up @@ -112,6 +114,7 @@ InputGroup.defaultProps = {
id: undefined,
isLabelVisible: true,
layout: 'vertical',
required: false,
size: 'medium',
validationTexts: null,
};
Expand Down Expand Up @@ -156,6 +159,11 @@ InputGroup.propTypes = {
* as the value is inherited in such case.
*/
layout: PropTypes.oneOf(['horizontal', 'vertical']),
/**
* If `true`, the `InputGroup`'s label appears as required. Underlying `<fieldset>`
* element does not take `required` attribute so there is no functional effect.
*/
required: PropTypes.bool,
/**
* Size of the `children` elements.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/components/InputGroup/InputGroup.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
@include foundation.help-text();
}

.isRootRequired .label {
@include foundation.label-required();
}

// States
.isRootStateInvalid {
@include variants.validation(invalid);
Expand Down
21 changes: 18 additions & 3 deletions src/components/InputGroup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@ not show any `validationText`, they only show their respective `validationState`
Validation messages passed to input elements' `validationText` prop will be
ignored.

👉 While there is a `required` property to visually denote the whole input group
is required, there is no functional effect as there is no such HTML attribute
for the underlying `<fieldset>` element.

```docoff-react-preview
<InputGroup
label="First and last name"
required
adamkudrna marked this conversation as resolved.
Show resolved Hide resolved
validationTexts={[
"First name must be filled in.",
"Last name must be filled in.",
Expand All @@ -187,44 +192,54 @@ ignored.
<TextField
label="First name"
placeholder="Eg. John"
required
validationState="invalid"
/>
<TextField
label="Last name"
placeholder="Eg. Doe"
required
validationState="invalid"
/>
<Button label="Submit" />
</InputGroup>
<InputGroup
label="First and last name"
required
validationTexts={[
"Last name should not include any digits.",
]}
>
<TextField
label="First name"
placeholder="Eg. John"
required
value="John"
/>
<TextField
label="Last name"
placeholder="Eg. Doe"
required
validationState="warning"
value="123Doe"
/>
<Button label="Submit" />
</InputGroup>
<InputGroup label="First and last name">
<InputGroup
label="First and last name"
required
>
<TextField
label="First name"
placeholder="Eg. John"
required
validationState="valid"
value="John"
/>
<TextField
label="Last name"
placeholder="Eg. Doe"
required
validationState="valid"
value="Doe"
/>
Expand All @@ -241,7 +256,7 @@ element which wraps elements to be grouped. This enables making the component
interactive and helps to improve its accessibility.

👉 Refer to the MDN reference for the full list of supported attributes of the
[div] element.
[fieldset][fieldset-attributes] element.

## API

Expand All @@ -257,4 +272,4 @@ interactive and helps to improve its accessibility.
[fieldset]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset
[accessibility]: https://www.w3.org/WAI/tutorials/forms/grouping/
[React synthetic events]: https://reactjs.org/docs/events.html
[div]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div#attributes
[fieldset-attributes]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset#attributes
6 changes: 6 additions & 0 deletions src/components/InputGroup/__tests__/InputGroup.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ describe('rendering', () => {
expect(within(rootElement).getByText('validation text 2'));
},
],
[
{ required: true },
(rootElement) => {
expect(rootElement).toHaveClass('isRootRequired');
},
],
])('renders with props: "%s"', (testedProps, assert) => {
const dom = render((
<InputGroup
Expand Down