Skip to content

Commit

Permalink
Merge pull request #345 from OleksiiMyzgin/hotfix/563167-missing-vali…
Browse files Browse the repository at this point in the history
…dation-error-for-radio-button-groups

hotfix(563167): add missing validation error for radio button groups
  • Loading branch information
FlorianRappl authored Aug 23, 2021
2 parents 4223580 + 51b0349 commit bd7ff33
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Precise UI Changelog

## 1.6.3

- fix(563167): add validation error

## 1.6.2

- Change to `flex-base: auto` for `RadioButtonCircle` to work on different box-sizing models
Expand Down
Binary file modified integration/__image_snapshots__/RadioButtonGroup_13-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified integration/__image_snapshots__/RadioButtonGroup_15-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified integration/__image_snapshots__/RadioButtonGroup_3-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified integration/__image_snapshots__/RadioButtonGroup_5-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified integration/__image_snapshots__/RadioButtonGroup_7-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified integration/__image_snapshots__/RadioButtonGroup_9-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "precise-ui",
"version": "1.6.2",
"version": "1.6.3",
"description": "Precise UI React component library powered by Styled Components.",
"keywords": [
"react",
Expand Down
14 changes: 13 additions & 1 deletion src/components/RadioButtonGroup/Example.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ const { RadioButtonGroup, RadioButton } = require('precise-ui');
</RadioButtonGroup>
```

The `RadioButtonGroup` can also be used to show error massage underneath the radio buttons if you have validation.

```jsx
const { RadioButtonGroup, RadioButton } = require('precise-ui');

<RadioButtonGroup error="Validation error">
<RadioButton>First</RadioButton>
<RadioButton>Second</RadioButton>
<RadioButton>Third</RadioButton>
</RadioButtonGroup>
```

The style of the radio buttons can be varied. Here we use some wrapping to prevent an overflow. Bravo radio button is selected by default.

```jsx
Expand Down Expand Up @@ -123,4 +135,4 @@ const { RadioButtonGroup, Checkbox, Form } = require('precise-ui');
<Checkbox name="third">Third</Checkbox>
</RadioButtonGroup>
</Form>
```
```
24 changes: 21 additions & 3 deletions src/components/RadioButtonGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
RadioButtonGroupContextType,
RadioButtonGroupNotifier,
} from '../../contexts/RadioButtonGroupContext';
import { PaddedContainer } from '../PaddedContainer';
import { InputError } from '../InputError';

export type RadioButtonGroupChangeEvent = InputChangeEvent<string>;

Expand All @@ -22,6 +24,7 @@ export type StateValue = Array<string> | string | undefined;
export interface RadioButtonGroupState {
controlled: boolean;
value: StateValue;
error?: React.ReactChild;
}

class RadioButtonGroupInt extends React.PureComponent<RadioButtonGroupProps & FormContextProps, RadioButtonGroupState> {
Expand All @@ -31,12 +34,13 @@ class RadioButtonGroupInt extends React.PureComponent<RadioButtonGroupProps & Fo
constructor(props: RadioButtonGroupProps) {
super(props);
const controlled = props.value !== undefined;
const { value: propValue, defaultValue } = props;
const { value: propValue, defaultValue, error } = props;
const value = controlled ? propValue : defaultValue;

this.state = {
controlled,
value,
error,
};
}

Expand Down Expand Up @@ -71,14 +75,15 @@ class RadioButtonGroupInt extends React.PureComponent<RadioButtonGroupProps & Fo
}
}

UNSAFE_componentWillReceiveProps({ value }: RadioButtonGroupProps) {
UNSAFE_componentWillReceiveProps({ value, error }: RadioButtonGroupProps) {
const { controlled } = this.state;

if (controlled) {
this.setState({
value,
});
}
this.setState({ error });
}

private getNextValue = (groupItemName?: string) => {
Expand Down Expand Up @@ -145,7 +150,20 @@ class RadioButtonGroupInt extends React.PureComponent<RadioButtonGroupProps & Fo
}

render() {
return <RadioButtonGroupContext.Provider value={this.ctx}>{this.props.children}</RadioButtonGroupContext.Provider>;
const { error } = this.state;

const Error = error && (
<PaddedContainer top="xsmall" bottom="xsmall">
<InputError>{error}</InputError>
</PaddedContainer>
);

return (
<RadioButtonGroupContext.Provider value={this.ctx}>
{this.props.children}
{Error}
</RadioButtonGroupContext.Provider>
);
}
}

Expand Down

0 comments on commit bd7ff33

Please sign in to comment.