From f6bd9e50ff867d463ea860b51b1a4f5c9feb9479 Mon Sep 17 00:00:00 2001 From: ascariandrea Date: Wed, 1 Nov 2023 12:05:19 +0100 Subject: [PATCH] fix(ui): connect color input to parent record --- .../admin/common/inputs/ColorInput.tsx | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/@liexp/ui/src/components/admin/common/inputs/ColorInput.tsx b/packages/@liexp/ui/src/components/admin/common/inputs/ColorInput.tsx index f2d9d90cb3..d2435dcaac 100644 --- a/packages/@liexp/ui/src/components/admin/common/inputs/ColorInput.tsx +++ b/packages/@liexp/ui/src/components/admin/common/inputs/ColorInput.tsx @@ -4,7 +4,12 @@ import { } from "@liexp/shared/lib/utils/colors"; import get from "lodash/get"; import * as React from "react"; -import { Button, useRecordContext, type TextInputProps } from "react-admin"; +import { + Button, + useRecordContext, + type TextInputProps, + useInput, +} from "react-admin"; import { Box, TextField, FormControl } from "../../../mui"; export const ColorInput: React.FC = ({ @@ -13,10 +18,9 @@ export const ColorInput: React.FC = ({ ...props }) => { const record = useRecordContext(); - const _color = get(record, source) ?? value; - - const [{ color }, setColor] = React.useState({ - color: _color ?? generateRandomColor(), + const { field } = useInput({ + source, + defaultValue: value ?? get(record, source) ?? generateRandomColor(), }); return ( @@ -26,7 +30,7 @@ export const ColorInput: React.FC = ({ style={{ alignItems: "center", justifyContent: "center", - border: `2px solid ${toColorHash(color)}`, + border: `2px solid ${toColorHash(field.value)}`, padding: "10px 20px", }} > @@ -36,18 +40,16 @@ export const ColorInput: React.FC = ({ variant="contained" onClick={() => { const color = generateRandomColor(); - setColor({ color }); - props.onChange?.({ target: { value: color } }); + field.onChange({ target: { value: color } }); }} /> { - setColor({ color: e.target.value }); - props.onChange?.(e); + field.onChange({ target: { value: e.target.value } }); }} />