Skip to content

Commit 285cb92

Browse files
committed
Remove step from colorfield as well and fix its useEffect same as numberfield
1 parent ac99810 commit 285cb92

File tree

5 files changed

+16
-34
lines changed

5 files changed

+16
-34
lines changed

packages/@react-spectrum/color/docs/ColorField.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ are calculated from the current `value` or zero if the `value` is empty. A `step
100100
defaulting to `1` if unspecified.
101101

102102
```tsx example
103-
<ColorField label="Primary Color" step={0x33} />
103+
<ColorField label="Primary Color" step={16} />
104104
```
105105

106106
## Validation

packages/@react-spectrum/color/test/ColorField.test.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,13 @@ describe('ColorField', function () {
283283

284284
it.each`
285285
Name | expected | key
286-
${'increment with arrow up key'} | ${parseColor('#AAAAAE')} | ${'ArrowUp'}
287-
${'decrement with arrow down key'} | ${parseColor('#AAAAA6')} | ${'ArrowDown'}
286+
${'increment with arrow up key'} | ${parseColor('#AAAAAB')} | ${'ArrowUp'}
287+
${'decrement with arrow down key'} | ${parseColor('#AAAAA9')} | ${'ArrowDown'}
288288
`('should handle $Name event', function ({expected, key}) {
289289
let onChangeSpy = jest.fn();
290290
let {getByLabelText} = renderComponent({
291291
defaultValue: '#aaa',
292-
onChange: onChangeSpy,
293-
step: 4
292+
onChange: onChangeSpy
294293
});
295294
let colorField = getByLabelText('Primary Color');
296295
expect(colorField.value).toBe('#AAAAAA');
@@ -303,14 +302,13 @@ describe('ColorField', function () {
303302

304303
it.each`
305304
Name | expected | deltaY
306-
${'increment with mouse wheel'} | ${parseColor('#AAAAAE')} | ${10}
307-
${'decrement with mouse wheel'} | ${parseColor('#AAAAA6')} | ${-10}
305+
${'increment with mouse wheel'} | ${parseColor('#AAAAAB')} | ${10}
306+
${'decrement with mouse wheel'} | ${parseColor('#AAAAA9')} | ${-10}
308307
`('should handle $Name event', function ({expected, deltaY}) {
309308
let onChangeSpy = jest.fn();
310309
let {getByLabelText} = renderComponent({
311310
defaultValue: '#aaa',
312-
onChange: onChangeSpy,
313-
step: 4
311+
onChange: onChangeSpy
314312
});
315313
let colorField = getByLabelText('Primary Color');
316314
expect(colorField.value).toBe('#AAAAAA');

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {Color, ColorFieldProps} from '@react-types/color';
1414
import {parseColor} from './Color';
1515
import {useColor} from './useColor';
1616
import {useControlledState} from '@react-stately/utils';
17-
import {useEffect, useMemo, useRef, useState} from 'react';
17+
import {useMemo, useRef, useState} from 'react';
1818

1919
export interface ColorFieldState {
2020
/**
@@ -63,11 +63,11 @@ export function useColorFieldState(
6363
props: ColorFieldProps
6464
): ColorFieldState {
6565
let {
66-
step = MIN_COLOR.getChannelRange('red').step,
6766
value,
6867
defaultValue,
6968
onChange
7069
} = props;
70+
let {step} = MIN_COLOR.getChannelRange('red');
7171

7272
let initialValue = useColor(value);
7373
let initialDefaultValue = useColor(defaultValue);
@@ -85,9 +85,12 @@ export function useColorFieldState(
8585
}
8686
};
8787

88-
useEffect(() => {
88+
let [prevValue, setPrevValue] = useState(colorValue);
89+
if (prevValue !== colorValue) {
8990
setInputValue(colorValue ? colorValue.toString('hex') : '');
90-
}, [colorValue, setInputValue]);
91+
setPrevValue(colorValue);
92+
}
93+
9194

9295
let parsedValue = useMemo(() => {
9396
let color;
@@ -97,7 +100,7 @@ export function useColorFieldState(
97100
color = null;
98101
}
99102
return color;
100-
}, [inputValue]);
103+
}, [parseColor, inputValue]);
101104
let parsed = useRef(null);
102105
parsed.current = parsedValue;
103106

packages/@react-stately/color/test/useColorFieldState.test.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,6 @@ describe('useColorFieldState tests', function () {
4242
expect(result.current.inputValue).toBe('#AABBCC');
4343
});
4444

45-
it.each`
46-
action | props
47-
${'increment'} | ${{defaultValue: '#000008', step: 4}}
48-
${'decrement'} | ${{defaultValue: '#000010', step: 4}}
49-
`('should $action', function ({action, props}) {
50-
let {result} = renderHook(() => useColorFieldState(props));
51-
act(() => result.current[action]());
52-
expect(result.current.colorValue.getChannelValue('red')).toBe(0);
53-
expect(result.current.colorValue.getChannelValue('green')).toBe(0);
54-
expect(result.current.colorValue.getChannelValue('blue')).toBe(12);
55-
expect(result.current.colorValue.getChannelValue('alpha')).toBe(1);
56-
expect(result.current.inputValue).toBe('#00000C');
57-
});
58-
5945
it.each`
6046
name | action | props
6147
${'not increment beyond max value'} | ${'increment'} | ${{defaultValue: '#ffffff'}}

packages/@react-types/color/src/index.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ export interface Color {
8080

8181
export interface ColorFieldProps extends Omit<ValueBase<string | Color | null>, 'onChange'>, InputBase, Validation, FocusableProps, TextInputBase, LabelableProps {
8282
/** Handler that is called when the value changes. */
83-
onChange?: (color: Color | null) => void,
84-
/**
85-
* The step value to increment and decrement the color by when using the arrow keys.
86-
* @default 1
87-
*/
88-
step?: number
83+
onChange?: (color: Color | null) => void
8984
}
9085

9186
export interface AriaColorFieldProps extends ColorFieldProps, AriaLabelingProps, FocusableDOMProps, Omit<TextInputDOMProps, 'minLength' | 'maxLength' | 'pattern' | 'type' | 'inputMode' | 'autoComplete'>, AriaValidationProps {}

0 commit comments

Comments
 (0)