Skip to content

Commit ef0ddfe

Browse files
fix: ensure NumberField validation displays correct errors in React 19 (#8491)
* fix: ensure NumberField validation displays correct errors in React 19 * fix: ColorField validation * Add controlled case to test * add controlled support for programmatic outside component changes --------- Co-authored-by: Robert Snow <[email protected]> Co-authored-by: Robert Snow <[email protected]>
1 parent f149209 commit ef0ddfe

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

packages/@react-stately/color/src/useColorFieldState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export function useColorFieldState(
136136
newColorValue = colorValue.toString('hex');
137137
}
138138
setInputValue(newColorValue);
139+
validation.commitValidation();
139140
};
140141

141142
let increment = () => {

packages/@react-stately/numberfield/src/useNumberFieldState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export function useNumberFieldState(
173173

174174
// in a controlled state, the numberValue won't change, so we won't go back to our old input without help
175175
setInputValue(format(value === undefined ? clampedValue : numberValue));
176+
validation.commitValidation();
176177
};
177178

178179
let safeNextStep = (operation: '+' | '-', minMax: number = 0) => {

packages/react-aria-components/stories/ColorField.stories.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {ColorField, Input, Label} from 'react-aria-components';
13+
import {ColorField, FieldError, Input, Label} from 'react-aria-components';
1414
import React from 'react';
1515

1616
export default {
@@ -28,9 +28,10 @@ export default {
2828
};
2929

3030
export const ColorFieldExample = (args) => (
31-
<ColorField {...args}>
31+
<ColorField {...args} validate={(v) => (v?.getChannelValue('red') === 0 ? 'Invalid value' : null)}>
3232
<Label>{args.label}</Label>
3333
<Input style={{display: 'block'}} />
34+
<FieldError />
3435
</ColorField>
3536
);
3637

packages/react-aria-components/stories/NumberField.stories.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {Button, Group, Input, Label, NumberField} from 'react-aria-components';
14-
import React from 'react';
13+
import {Button, FieldError, Group, Input, Label, NumberField, NumberFieldProps} from 'react-aria-components';
14+
import React, {useState} from 'react';
1515

1616
export default {
1717
title: 'React Aria Components'
@@ -27,13 +27,43 @@ export const NumberFieldExample = {
2727
isWheelDisabled: false
2828
},
2929
render: (args) => (
30-
<NumberField {...args}>
30+
<NumberField {...args} validate={(v) => (v & 1 ? 'Invalid value' : null)}>
3131
<Label>Test</Label>
3232
<Group style={{display: 'flex'}}>
3333
<Button slot="decrement">-</Button>
3434
<Input />
3535
<Button slot="increment">+</Button>
3636
</Group>
37+
<FieldError />
3738
</NumberField>
3839
)
3940
};
41+
42+
function NumberFieldControlled(props: NumberFieldProps) {
43+
const [value, setValue] = useState(props.defaultValue);
44+
return (
45+
<NumberField {...props} validate={(v) => (v & 1 ? 'Invalid value' : null)} value={value} onChange={setValue}>
46+
<Label>Test</Label>
47+
<Group style={{display: 'flex'}}>
48+
<Button slot="decrement">-</Button>
49+
<Input />
50+
<Button slot="increment">+</Button>
51+
</Group>
52+
<FieldError />
53+
</NumberField>
54+
);
55+
}
56+
57+
export const NumberFieldControlledExample = {
58+
args: {
59+
defaultValue: 0,
60+
minValue: 0,
61+
maxValue: 100,
62+
step: 1,
63+
formatOptions: {style: 'currency', currency: 'USD'},
64+
isWheelDisabled: false
65+
},
66+
render: (args) => (
67+
<NumberFieldControlled {...args} />
68+
)
69+
};

0 commit comments

Comments
 (0)