Skip to content

Commit d197fa4

Browse files
Fix error message validation logic (#1311)
* Fix error message validation logic * Fix error message logic * remove redundant prop
1 parent bdbbde0 commit d197fa4

File tree

10 files changed

+13
-9
lines changed

10 files changed

+13
-9
lines changed

.changeset/clear-rings-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@obosbbl/grunnmuren-react": patch
3+
---
4+
5+
Fixes the logic behind validation states for inputs, where passing `errorMessage=""` would set the field in an invalid state. This doesn't really make sense, and can cause strange validation behaviour when using libraries like Zod. With this change setting `errorMessage` to `"" | null | undefined` is now equivalent: the field is valid, unless combined with the `isInvalid` prop set to `true`.

packages/react/src/checkbox/checkbox-group.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function CheckboxGroup(props: CheckboxGroupProps) {
4747

4848
// the order of the conditions matter here, because providing a value for isInvalid makes the validation state "controlled",
4949
// which will override any built in validation
50-
const isInvalid = errorMessage != null || _isInvalid;
50+
const isInvalid = !!errorMessage || _isInvalid;
5151

5252
return (
5353
<RACCheckboxGroup

packages/react/src/checkbox/checkbox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function Checkbox(props: CheckboxProps) {
7474
const descriptionId = `desc${id}`;
7575
const errorMessageId = `error${id}`;
7676

77-
const isInvalid = errorMessage != null || _isInvalid;
77+
const isInvalid = !!errorMessage || _isInvalid;
7878

7979
return (
8080
<div>

packages/react/src/combobox/combobox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function Combobox<T extends object>(props: ComboboxProps<T>) {
7171

7272
// the order of the conditions matter here, because providing a value for isInvalid makes the validation state "controlled",
7373
// which will override any built in validation
74-
const isInvalid = errorMessage != null || _isInvalid;
74+
const isInvalid = !!errorMessage || _isInvalid;
7575

7676
return (
7777
<RACCombobox

packages/react/src/numberfield/numberfield.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function NumberField(props: NumberFieldProps) {
9191

9292
// the order of the conditions matter here, because providing a value for isInvalid makes the validation state "controlled",
9393
// which will override any built in validation
94-
const isInvalid = errorMessage != null || _isInvalid;
94+
const isInvalid = !!errorMessage || _isInvalid;
9595

9696
return (
9797
<RACNumberField

packages/react/src/radiogroup/radio-group.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ export const WithErrorMessage: Story = {
143143

144144
args: {
145145
...defaultProps,
146-
isInvalid: true,
147146
errorMessage: 'Det valgte alternativet er ikke gyldig',
148147
},
149148
};

packages/react/src/radiogroup/radio-group.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function RadioGroup(props: RadioGroupProps) {
4747

4848
// the order of the conditions matter here, because providing a value for isInvalid makes the validation state "controlled",
4949
// which will override any built in validation
50-
const isInvalid = errorMessage != null || _isInvalid;
50+
const isInvalid = !!errorMessage || _isInvalid;
5151

5252
return (
5353
<RACRadioGroup

packages/react/src/select/select.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function Select<T extends object>(props: SelectProps<T>) {
5656

5757
// the order of the conditions matter here, because providing a value for isInvalid makes the validation state "controlled",
5858
// which will override any built in validation
59-
const isInvalid = errorMessage != null || _isInvalid;
59+
const isInvalid = !!errorMessage || _isInvalid;
6060

6161
return (
6262
<RACSelect

packages/react/src/textarea/textarea.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function TextArea(props: TextAreaProps) {
4848
...restProps
4949
} = props;
5050

51-
const isInvalid = errorMessage != null || _isInvalid;
51+
const isInvalid = !!errorMessage || _isInvalid;
5252

5353
return (
5454
<RACTextField

packages/react/src/textfield/textfield.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function TextField(props: TextFieldProps) {
8181

8282
// the order of the conditions matter here, because providing a value for isInvalid makes the validation state "controlled",
8383
// which will override any built in validation
84-
const isInvalid = errorMessage != null || _isInvalid;
84+
const isInvalid = !!errorMessage || _isInvalid;
8585

8686
return (
8787
<RACTextField

0 commit comments

Comments
 (0)