Skip to content

Commit

Permalink
Allow check fields and selectable fields to render as required #487
Browse files Browse the repository at this point in the history
Users may find themselves in a situation where the input is
not required (i.e. making the input checked), but they also
don't want to render the field as optional because not
choosing an option can be perfectly valid. For this case,
there is the `renderAsRequired` prop.

This affects `CheckboxField`, `Radio`, `SelectField`, and `Toggle`.

Closes #487
  • Loading branch information
adamkudrna committed Jan 24, 2025
1 parent ca1c36a commit 0f2698d
Show file tree
Hide file tree
Showing 16 changed files with 435 additions and 15 deletions.
10 changes: 8 additions & 2 deletions src/components/CheckboxField/CheckboxField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const CheckboxField = React.forwardRef((props, ref) => {
isLabelVisible,
label,
labelPosition,
renderAsRequired,
required,
validationState,
validationText,
Expand All @@ -30,7 +31,7 @@ export const CheckboxField = React.forwardRef((props, ref) => {
context && context.layout === 'horizontal' ? styles.isRootLayoutHorizontal : styles.isRootLayoutVertical,
labelPosition === 'before' && styles.hasRootLabelBefore,
disabled && styles.isRootDisabled,
required && styles.isRootRequired,
(renderAsRequired || required) && styles.isRootRequired,
getRootValidationStateClassName(validationState, styles),
)}
htmlFor={id}
Expand Down Expand Up @@ -82,6 +83,7 @@ CheckboxField.defaultProps = {
id: undefined,
isLabelVisible: true,
labelPosition: 'after',
renderAsRequired: false,
required: false,
validationState: null,
validationText: null,
Expand Down Expand Up @@ -120,7 +122,11 @@ CheckboxField.propTypes = {
*/
labelPosition: PropTypes.oneOf(['before', 'after']),
/**
* If `true`, the input will be required.
* If `true`, the input will be rendered as if it was required.
*/
renderAsRequired: PropTypes.bool,
/**
* If `true`, the input will be made and rendered as required, regardless of the `renderAsRequired` prop.
*/
required: PropTypes.bool,
/**
Expand Down
75 changes: 75 additions & 0 deletions src/components/CheckboxField/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,81 @@ React.createElement(() => {
});
```

### Required State

The required state indicates that the input is mandatory. Required fields
display an asterisk `*` after the label by default.

```docoff-react-preview
React.createElement(() => {
const [agree, setAgree] = React.useState(true);
return (
<CheckboxField
checked={agree}
label="I agree"
onChange={() => setAgree(!agree)}
required
/>
);
});
```

#### Styling the Required State

All form fields in React UI can be
[styled](/docs/customize/theming/forms/#required-state)
to indicate the required state.

However, you may find yourself in a misleading situation where a form field
is valid in both checked and unchecked states, for example to turn on or off a
feature. If your project uses the label color as the primary means to indicate
the required state of input fields and the usual asterisk `*` is omitted,
you may want to keep the label color consistent for both states.

For this edge case, there is the `renderAsRequired` prop:

```docoff-react-preview
React.createElement(() => {
const [optional, setOptional] = React.useState(false);
const [renderAsRequired, setRenderAsRequired] = React.useState(false);
return (
<React.Fragment>
<style>
{`
.example {
display: flex;
flex-wrap: wrap;
gap: 1rem 0.5rem;
}
.example--themed-form-fields {
--rui-FormField__label__color: var(--rui-color-text-secondary);
--rui-FormField--required__label__color: var(--rui-color-text-primary);
--rui-FormField--required__sign: '';
}
`}
</style>
<div class="example example--themed-form-fields">
<CheckboxField
checked={optional}
label="This field is optional"
onChange={() => setOptional(!optional)}
/>
<CheckboxField
checked={renderAsRequired}
label="This field is optional but looks like required"
onChange={() => setRenderAsRequired(!renderAsRequired)}
renderAsRequired
/>
</div>
</React.Fragment>
);
});
```

It renders the field as if it was required, but doesn't add the `required`
attribute to the actual input.

### Disabled State

Disabled state makes the input unavailable.
Expand Down
2 changes: 2 additions & 0 deletions src/components/CheckboxField/__tests__/CheckboxField.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { helpTextPropTest } from '../../../../tests/propTests/helpTextPropTest';
import { formLayoutProviderTest } from '../../../../tests/providerTests/formLayoutProviderTest';
import { isLabelVisibleTest } from '../../../../tests/propTests/isLabelVisibleTest';
import { labelPropTest } from '../../../../tests/propTests/labelPropTest';
import { renderAsRequiredPropTest } from '../../../../tests/propTests/renderAsRequiredPropTest';
import { requiredPropTest } from '../../../../tests/propTests/requiredPropTest';
import { validationStatePropTest } from '../../../../tests/propTests/validationStatePropTest';
import { validationTextPropTest } from '../../../../tests/propTests/validationTextPropTest';
Expand Down Expand Up @@ -43,6 +44,7 @@ describe('rendering', () => {
],
...isLabelVisibleTest(),
...labelPropTest(),
...renderAsRequiredPropTest,
...requiredPropTest,
...validationStatePropTest,
...validationTextPropTest,
Expand Down
103 changes: 103 additions & 0 deletions src/components/Radio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,109 @@ have.
})
```

### Required State

The required state indicates that the input is mandatory.

```docoff-react-preview
React.createElement(() => {
const [fruit, setFruit] = React.useState('apple');
return (
<Radio
label="Your favourite fruit"
onChange={(e) => setFruit(e.target.value)}
options={[
{
label: 'Apple',
value: 'apple',
},
{
label: 'Banana',
value: 'banana',
},
{
label: 'Grapefruit',
value: 'grapefruit',
},
]}
value={fruit}
required
/>
);
})
```

#### Styling the Required State

All form fields in React UI can be
[styled](/docs/customize/theming/forms/#required-state)
to indicate the required state.

However, you may find yourself in a misleading situation where a form field
is valid in both selected and unselected states, for example to turn on or off a
feature. If your project uses the label color as the primary means to indicate
the required state of input fields and the usual asterisk `*` is omitted,
you may want to keep the label color consistent for both states.

For this edge case, there is the `renderAsRequired` prop:

```docoff-react-preview
React.createElement(() => {
const [fruit, setFruit] = React.useState('apple');
const options = [
{
label: 'Apple',
value: 'apple',
},
{
label: 'Banana',
value: 'banana',
},
{
label: 'Grapefruit',
value: 'grapefruit',
},
];
return (
<React.Fragment>
<style>
{`
.example {
display: flex;
flex-wrap: wrap;
gap: 1rem 0.5rem;
}
.example--themed-form-fields {
--rui-FormField__label__color: var(--rui-color-text-secondary);
--rui-FormField--required__label__color: var(--rui-color-text-primary);
--rui-FormField--required__sign: '';
}
`}
</style>
<div class="example example--themed-form-fields">
<Radio
label="This field is optional"
onChange={(e) => setFruit(e.target.value)}
options={options}
value={fruit}
/>
<Radio
label="This field is optional but looks like required"
onChange={(e) => setFruit(e.target.value)}
options={options}
value={fruit}
renderAsRequired
/>
</div>
</React.Fragment>
);
})
```

It renders the field as if it was required, but doesn't add the `required`
attribute to the actual input.

### Disabled State

It's possible to disable just some options or the whole set.
Expand Down
10 changes: 8 additions & 2 deletions src/components/Radio/Radio.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const Radio = ({
label,
layout,
options,
renderAsRequired,
required,
validationState,
validationText,
Expand All @@ -33,7 +34,7 @@ export const Radio = ({
? styles.isRootLayoutHorizontal
: styles.isRootLayoutVertical,
disabled && styles.isRootDisabled,
required && styles.isRootRequired,
(renderAsRequired || required) && styles.isRootRequired,
getRootValidationStateClassName(validationState, styles),
)}
disabled={disabled}
Expand Down Expand Up @@ -116,6 +117,7 @@ Radio.defaultProps = {
id: undefined,
isLabelVisible: true,
layout: 'vertical',
renderAsRequired: false,
required: false,
validationState: null,
validationText: null,
Expand Down Expand Up @@ -181,7 +183,11 @@ Radio.propTypes = {
]),
})).isRequired,
/**
* If `true`, the input will be required.
* If `true`, the input will be rendered as if it was required.
*/
renderAsRequired: PropTypes.bool,
/**
* If `true`, the input will be made and rendered as required, regardless of the `renderAsRequired` prop.
*/
required: PropTypes.bool,
/**
Expand Down
4 changes: 4 additions & 0 deletions src/components/Radio/Radio.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
@include foundation.label-required();
}

.isRootRequired .optionLabel {
@include foundation.label-required($show-require-sign: false);
}

// States
.isRootStateInvalid {
@include variants.validation(invalid);
Expand Down
2 changes: 2 additions & 0 deletions src/components/Radio/__tests__/Radio.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { formLayoutProviderTest } from '../../../../tests/providerTests/formLayo
import { isLabelVisibleTest } from '../../../../tests/propTests/isLabelVisibleTest';
import { labelPropTest } from '../../../../tests/propTests/labelPropTest';
import { layoutPropTest } from '../../../../tests/propTests/layoutPropTest';
import { renderAsRequiredPropTest } from '../../../../tests/propTests/renderAsRequiredPropTest';
import { requiredPropTest } from '../../../../tests/propTests/requiredPropTest';
import { validationStatePropTest } from '../../../../tests/propTests/validationStatePropTest';
import { validationTextPropTest } from '../../../../tests/propTests/validationTextPropTest';
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('rendering', () => {
expect(within(rootElement).getByLabelText('option 2')).toBeDisabled();
},
],
...renderAsRequiredPropTest,
...requiredPropTest,
...validationStatePropTest,
...validationTextPropTest,
Expand Down
Loading

0 comments on commit 0f2698d

Please sign in to comment.