Skip to content

Commit 8efb03d

Browse files
authored
fix: tanstack fields integration (#992)
* `Checkbox` bind `checked` instead `defaultChecked` to fix form reset * `ColorPicker` use `handleChange` instead `setValue` and bind `onBlur` * `NumericInput` bind `onBlur` * `Switch` also bind `value` like `Checkbox` NB about onBlur: tanstack `field.handleBlur` updates metadata of the field. It helps to compute if a field had been touched, So it is needed to ensure form state is coherent.
1 parent 892b4cd commit 8efb03d

File tree

11 files changed

+50
-13
lines changed

11 files changed

+50
-13
lines changed

src/components/color-picker/color-picker-dropdown/ColorPickerDropdown.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import * as colorHelper from '../react-color/helpers/color.js';
99

1010
export interface ColorPickerDropdownProps extends Pick<
1111
ColorPickerProps,
12-
'color' | 'presetColors' | 'disableAlpha' | 'onChange' | 'onChangeComplete'
12+
| 'color'
13+
| 'presetColors'
14+
| 'disableAlpha'
15+
| 'onChange'
16+
| 'onChangeComplete'
17+
| 'onBlur'
1318
> {
1419
popoverProps?: Omit<PopoverProps, 'content'>;
1520

src/components/color-picker/react-color/ColorPicker.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CSSProperties } from 'react';
1+
import type { CSSProperties, FocusEvent } from 'react';
22
import { useCallback, useRef, useState } from 'react';
33

44
import { debounce } from '../../utils/index.js';
@@ -51,6 +51,7 @@ export interface ColorPickerProps {
5151
disableAlpha?: boolean;
5252
onChange?: (props: ChangeCallbackProps, event?: Event) => void;
5353
onChangeComplete?: (props: ChangeCallbackProps, event?: Event) => void;
54+
onBlur?: (event: FocusEvent<HTMLElement>) => void;
5455
onSwatchHover?: (props: ChangeCallbackProps, event?: Event) => void;
5556
style?: CSSProperties;
5657
}
@@ -149,6 +150,7 @@ export function ColorPicker(props: ColorPickerProps) {
149150
className = '',
150151
color = defaultColor,
151152
onChangeComplete,
153+
onBlur,
152154
style = {},
153155
} = props;
154156

@@ -200,19 +202,30 @@ export function ColorPicker(props: ColorPickerProps) {
200202
return (
201203
<div style={{ ...styles.picker(width), ...style }} className={className}>
202204
<div style={styles.saturationContainer}>
203-
<Saturation hsl={hsl} hsv={hsv} onChange={handleChange} />
205+
<Saturation
206+
hsl={hsl}
207+
hsv={hsv}
208+
onChange={handleChange}
209+
onBlur={onBlur}
210+
/>
204211
</div>
205212
<div style={styles.controls}>
206213
<div style={styles.sliders}>
207214
<div style={styles.hueContainer}>
208-
<Hue style={styles.hueElement} hsl={hsl} onChange={handleChange} />
215+
<Hue
216+
style={styles.hueElement}
217+
hsl={hsl}
218+
onChange={handleChange}
219+
onBlur={onBlur}
220+
/>
209221
</div>
210222
<div style={styles.alphaContainer(disableAlpha)}>
211223
<Alpha
212224
style={styles.alphaElement}
213225
rgb={rgb}
214226
hsl={hsl}
215227
onChange={handleChange}
228+
onBlur={onBlur}
216229
/>
217230
</div>
218231
</div>
@@ -227,6 +240,7 @@ export function ColorPicker(props: ColorPickerProps) {
227240
hex={hex}
228241
rgb={rgb}
229242
onChange={handleChange}
243+
onBlur={onBlur}
230244
disableAlpha={disableAlpha}
231245
/>
232246
<SketchPresetColors

src/components/color-picker/react-color/common/Alpha.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const Alpha = (props) => {
107107
onMouseDown={mouseDownHandler}
108108
onTouchMove={handleChange}
109109
onTouchStart={handleChange}
110+
onBlur={props.onBlur}
110111
>
111112
<div style={styles.pointer(direction, rgb)}>
112113
{props.pointer ? (

src/components/color-picker/react-color/common/EditableInput.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,16 @@ const EditableInput = (props) => {
5050
[props.label],
5151
);
5252

53-
const handleBlur = useCallback(() => {
54-
if (state.blurValue) {
55-
setState({ value: state.blurValue, blurValue: null });
56-
}
57-
}, [state.blurValue]);
53+
const onBlur = props.onBlur;
54+
const handleBlur = useCallback(
55+
(event: FocusEvent<HTMLInputElement>) => {
56+
if (state.blurValue) {
57+
setState({ value: state.blurValue, blurValue: null });
58+
}
59+
onBlur?.(event);
60+
},
61+
[state.blurValue, onBlur],
62+
);
5863

5964
const setUpdatedValue = useCallback(
6065
(value, e) => {

src/components/color-picker/react-color/common/Hue.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const Hue = (props) => {
8989
onMouseDown={handleMouseDown}
9090
onTouchMove={handleChange}
9191
onTouchStart={handleChange}
92+
onBlur={props.onBlur}
9293
>
9394
<div style={styles.pointer(direction, hsl)}>
9495
{pointer ? (

src/components/color-picker/react-color/common/Saturation.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const styles = {
5353
};
5454

5555
const Saturation = (props) => {
56-
const { onChange, hsl, hsv, pointer } = props;
56+
const { hsl, hsv, pointer, onChange, onBlur } = props;
5757

5858
const throttleRef = useRef(
5959
throttle((fn, data, e) => {
@@ -85,6 +85,7 @@ const Saturation = (props) => {
8585
onMouseDown={handleMouseDown}
8686
onTouchMove={handleChange}
8787
onTouchStart={handleChange}
88+
onBlur={onBlur}
8889
>
8990
<div style={styles.white()}>
9091
<div style={styles.black()} />

src/components/color-picker/react-color/sketch/SketchFields.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CSSProperties } from 'react';
1+
import type { CSSProperties, FocusEvent } from 'react';
22
import { useCallback } from 'react';
33

44
import type { HSL, RGB } from '../ColorPicker.js';
@@ -10,6 +10,7 @@ interface SketchFieldsProps {
1010
data: (RGB | HSL | { hex: string }) & { source: string },
1111
e: Event,
1212
) => void;
13+
onBlur?: (event: FocusEvent<HTMLElement>) => void;
1314
hsl: HSL;
1415
hex: string;
1516
rgb: RGB;
@@ -57,6 +58,7 @@ const styles: Record<
5758

5859
const SketchFields = ({
5960
onChange,
61+
onBlur,
6062
rgb,
6163
hsl,
6264
hex,
@@ -116,6 +118,7 @@ const SketchFields = ({
116118
label="hex"
117119
value={hex.replace('#', '')}
118120
onChange={handleChange}
121+
onBlur={onBlur}
119122
/>
120123
</div>
121124
<div style={styles.single}>
@@ -124,6 +127,7 @@ const SketchFields = ({
124127
label="r"
125128
value={rgb.r}
126129
onChange={handleChange}
130+
onBlur={onBlur}
127131
dragLabel="true"
128132
dragMax="255"
129133
/>
@@ -134,6 +138,7 @@ const SketchFields = ({
134138
label="g"
135139
value={rgb.g}
136140
onChange={handleChange}
141+
onBlur={onBlur}
137142
dragLabel="true"
138143
dragMax="255"
139144
/>
@@ -144,6 +149,7 @@ const SketchFields = ({
144149
label="b"
145150
value={rgb.b}
146151
onChange={handleChange}
152+
onBlur={onBlur}
147153
dragLabel="true"
148154
dragMax="255"
149155
/>
@@ -154,6 +160,7 @@ const SketchFields = ({
154160
label="a"
155161
value={Math.round(rgb.a * 100)}
156162
onChange={handleChange}
163+
onBlur={onBlur}
157164
dragLabel="true"
158165
dragMax="100"
159166
/>

src/components/form/components/input/checkbox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export function Checkbox(props: CheckboxProps) {
4545
label={label}
4646
name={field.name}
4747
value={String(field.state.value)}
48+
checked={field.state.value}
4849
onChange={onChange}
4950
onBlur={field.handleBlur}
50-
defaultChecked={field.state.value}
5151
/>
5252
</FormGroup>
5353
);

src/components/form/components/input/color_picker.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function ColorPicker(props: ColorPickerProps) {
2222
.at(0);
2323

2424
function onChange(color: ChangeCallbackComponentProps) {
25-
field.setValue(color.hex);
25+
field.handleChange(color.hex);
2626
}
2727

2828
return (
@@ -40,6 +40,7 @@ export function ColorPicker(props: ColorPickerProps) {
4040
id={field.name}
4141
color={{ hex: field.state.value }}
4242
onChange={onChange}
43+
onBlur={field.handleBlur}
4344
presetColors={presetColors}
4445
disableAlpha={disableAlpha}
4546
popoverProps={popoverProps}

src/components/form/components/input/numeric_input.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function NumericInput(props: NumericInputProps) {
5858
max={max}
5959
value={field.state.value ?? ''}
6060
onValueChange={onChange}
61+
onBlur={field.handleBlur}
6162
intent={intent}
6263
placeholder={placeholder}
6364
required={required}

0 commit comments

Comments
 (0)