Skip to content

Commit ecc50ee

Browse files
Color Picker Input | Fix warning and clear value (#165)
FIX 1: usual MUI warning when value is not defined if value is undefined Fix the warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. if value is null `value` prop on `input` should not be null. Consider using an empty string to clear the component or `undefined` for uncontrolled components. FIX 2: color picker couldn't be cleared if value was already present (field.value not consistent with current watched value)
1 parent 4941c25 commit ecc50ee

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- `ColorPicker` controlled/uncontrolled state warning by ensuring value prop is always defined.
13+
- `ColorPicker` clear value when field value is already set.
14+
1015
## [4.1.0] - 2026-03-16
1116

1217
### Added

src/lib/components/ColorPicker/ColorPicker.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const ColorPicker = <T extends FieldValues>(props: ColorPickerInputProps<T>) =>
4747
disabled: formDisabled,
4848
getFieldState,
4949
setValue,
50+
watch,
5051
requiredFields,
5152
formState: { errors },
5253
hideValidationMessages,
@@ -65,7 +66,8 @@ const ColorPicker = <T extends FieldValues>(props: ColorPickerInputProps<T>) =>
6566
});
6667

6768
const isDisabled = formDisabled || disabled;
68-
const color = useMemo(() => new TinyColor(field.value), [field.value]);
69+
const fieldValue = watch(name) as string | undefined;
70+
const color = useMemo(() => new TinyColor(fieldValue), [fieldValue]);
6971
const fieldError = get(errors, name) as FieldError | undefined;
7072
const hideErrorMessage = useMemo(() => hideValidationMessages || hideValidationMessage, [hideValidationMessages, hideValidationMessage]);
7173
const hasError = useMemo(() => !!fieldError, [fieldError]);
@@ -107,7 +109,7 @@ const ColorPicker = <T extends FieldValues>(props: ColorPickerInputProps<T>) =>
107109

108110
field.onBlur();
109111
}}
110-
value={field.value}
112+
value={fieldValue ?? ""}
111113
slotProps={{
112114
input: {
113115
startAdornment: (

0 commit comments

Comments
 (0)