Added client edit support for multi colors
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { HttpError, IResourceComponentsProps, useTranslate } from "@refinedev/core";
|
import { HttpError, IResourceComponentsProps, useTranslate } from "@refinedev/core";
|
||||||
import { Edit, useForm, useSelect } from "@refinedev/antd";
|
import { Edit, useForm, useSelect } from "@refinedev/antd";
|
||||||
import { Form, Input, DatePicker, Select, InputNumber, ColorPicker, message, Alert, Typography } from "antd";
|
import { Form, Input, DatePicker, Select, InputNumber, ColorPicker, message, Alert, Typography, Radio } from "antd";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import TextArea from "antd/es/input/TextArea";
|
import TextArea from "antd/es/input/TextArea";
|
||||||
import { numberFormatter, numberParser } from "../../utils/parsing";
|
import { numberFormatter, numberParser } from "../../utils/parsing";
|
||||||
@@ -11,6 +11,7 @@ import { EntityType, useGetFields } from "../../utils/queryFields";
|
|||||||
import { ExtraFieldFormItem, StringifiedExtras } from "../../components/extraFields";
|
import { ExtraFieldFormItem, StringifiedExtras } from "../../components/extraFields";
|
||||||
import { ParsedExtras } from "../../components/extraFields";
|
import { ParsedExtras } from "../../components/extraFields";
|
||||||
import { getCurrencySymbol, useCurrency } from "../../utils/settings";
|
import { getCurrencySymbol, useCurrency } from "../../utils/settings";
|
||||||
|
import { MultiColorPicker } from "../../components/multiColorPicker";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The API returns the extra fields as JSON values, but we need to parse them into their real types
|
The API returns the extra fields as JSON values, but we need to parse them into their real types
|
||||||
@@ -25,6 +26,7 @@ export const FilamentEdit: React.FC<IResourceComponentsProps> = () => {
|
|||||||
const [hasChanged, setHasChanged] = useState(false);
|
const [hasChanged, setHasChanged] = useState(false);
|
||||||
const extraFields = useGetFields(EntityType.filament);
|
const extraFields = useGetFields(EntityType.filament);
|
||||||
const currency = useCurrency();
|
const currency = useCurrency();
|
||||||
|
const [colorType, setColorType] = React.useState<"single" | "multi">("single");
|
||||||
|
|
||||||
const { formProps, saveButtonProps } = useForm<IFilament, HttpError, IFilament, IFilament>({
|
const { formProps, saveButtonProps } = useForm<IFilament, HttpError, IFilament, IFilament>({
|
||||||
liveMode: "manual",
|
liveMode: "manual",
|
||||||
@@ -49,6 +51,16 @@ export const FilamentEdit: React.FC<IResourceComponentsProps> = () => {
|
|||||||
formProps.initialValues = ParsedExtras(formProps.initialValues);
|
formProps.initialValues = ParsedExtras(formProps.initialValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update colorType state
|
||||||
|
React.useEffect(() => {
|
||||||
|
console.log(formProps.initialValues?.multi_color_hexes);
|
||||||
|
if (formProps.initialValues?.multi_color_hexes) {
|
||||||
|
setColorType("multi");
|
||||||
|
} else {
|
||||||
|
setColorType("single");
|
||||||
|
}
|
||||||
|
}, [formProps.initialValues?.multi_color_hexes]);
|
||||||
|
|
||||||
// Override the form's onFinish method to stringify the extra fields
|
// Override the form's onFinish method to stringify the extra fields
|
||||||
const originalOnFinish = formProps.onFinish;
|
const originalOnFinish = formProps.onFinish;
|
||||||
formProps.onFinish = (allValues: IFilamentParsedExtras) => {
|
formProps.onFinish = (allValues: IFilamentParsedExtras) => {
|
||||||
@@ -132,20 +144,62 @@ export const FilamentEdit: React.FC<IResourceComponentsProps> = () => {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item
|
<Form.Item label={t("filament.fields.color_hex")}>
|
||||||
label={t("filament.fields.color_hex")}
|
<Radio.Group
|
||||||
name={["color_hex"]}
|
onChange={(value) => {
|
||||||
rules={[
|
setColorType(value.target.value);
|
||||||
{
|
}}
|
||||||
required: false,
|
defaultValue={colorType}
|
||||||
},
|
value={colorType}
|
||||||
]}
|
>
|
||||||
getValueFromEvent={(e) => {
|
<Radio.Button value={"single"}>{t("filament.fields.single_color")}</Radio.Button>
|
||||||
return e?.toHex();
|
<Radio.Button value={"multi"}>{t("filament.fields.multi_color")}</Radio.Button>
|
||||||
}}
|
</Radio.Group>
|
||||||
>
|
|
||||||
<ColorPicker format="hex" />
|
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
{colorType == "single" && (
|
||||||
|
<Form.Item
|
||||||
|
name={"color_hex"}
|
||||||
|
rules={[
|
||||||
|
{
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
getValueFromEvent={(e) => {
|
||||||
|
return e?.toHex();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ColorPicker format="hex" />
|
||||||
|
</Form.Item>
|
||||||
|
)}
|
||||||
|
{colorType == "multi" && (
|
||||||
|
<Form.Item
|
||||||
|
name={"multi_color_direction"}
|
||||||
|
help={t("filament.fields_help.multi_color_direction")}
|
||||||
|
rules={[
|
||||||
|
{
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
initialValue={"coaxial"}
|
||||||
|
>
|
||||||
|
<Radio.Group>
|
||||||
|
<Radio.Button value={"coaxial"}>{t("filament.fields.coaxial")}</Radio.Button>
|
||||||
|
<Radio.Button value={"longitudinal"}>{t("filament.fields.longitudinal")}</Radio.Button>
|
||||||
|
</Radio.Group>
|
||||||
|
</Form.Item>
|
||||||
|
)}
|
||||||
|
{colorType == "multi" && (
|
||||||
|
<Form.Item
|
||||||
|
name={"multi_color_hexes"}
|
||||||
|
rules={[
|
||||||
|
{
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<MultiColorPicker min={2} max={14} />
|
||||||
|
</Form.Item>
|
||||||
|
)}
|
||||||
<Form.Item
|
<Form.Item
|
||||||
label={t("filament.fields.material")}
|
label={t("filament.fields.material")}
|
||||||
help={t("filament.fields_help.material")}
|
help={t("filament.fields_help.material")}
|
||||||
|
|||||||
Reference in New Issue
Block a user