I'm using Refine with Ant Design. I've a form for creating a new tag. The form has a property named background_color
, which should be the color in HEX format (i.e. #ff6347
).
The problem is that <ColorPicker>
component seems to output an object:
{"metaColor":{"originalInput":{"h":215.15151515151516,"s":0.8034188034188035,"a":1,"v":0.50625},"r":25.377324599999998,"g":68.32531138181821,"b":129.081,"a":1,"roundA":1,"format":"hsv","isValid":true}} {"metaColor":{"originalInput":{"h":309.34600515463916,"s":0.8021501068376068,"a":1,"v":0.58125},"r":148.206,"g":29.329967400000008,"b":129.68906644262245,"a":1,"roundA":1,"format":"hsv","isValid":true}}
Object has a toHexString()
method which I'm using inside getValueProps
. But it's not working, the value sent to the server is still the object above.
How can I transform the value before sending it to the server? And how to trasform the value back (in my edit form) to be correctly recognized by the <ColorPicker>
?
import { Create, useForm } from "@refinedev/antd";
import { IResourceComponentsProps } from "@refinedev/core";
import { Form, Input, ColorPicker, Row, Col } from "antd";
import React from "react";
export const TagCreate: React.FC<IResourceComponentsProps> = () => {
const { formProps, saveButtonProps } = useForm();
return (
<Create saveButtonProps={saveButtonProps}>
<Form {...formProps} layout="vertical">
<Form.Item label="name" name={["name"]}>
<Input />
</Form.Item>
<Form.Item
label="Background color"
name={["background_color"]}
getValueProps={(value) => ({
value: value ? value.toHexString() : undefined,
})}
>
<ColorPicker />
</Form.Item>
</Form>
</Create>
);
};
getValueFromEvent
to transform value. Better option is to pass format prop to colorpicker as mentioned by @NoNam4format={"hex"}
doesn't work. Still, the object is sent to the server. I think that has to do with the color format dropdown only.