diff --git a/client/src/components/numberField.tsx b/client/src/components/numberField.tsx new file mode 100644 index 0000000..c5788a4 --- /dev/null +++ b/client/src/components/numberField.tsx @@ -0,0 +1,40 @@ +import React from "react"; +import { Typography } from "antd"; +import { NumberFieldProps } from "@refinedev/antd/dist/components/fields/types"; + +const { Text } = Typography; + +function toLocaleStringSupportsOptions() { + return !!( + typeof Intl == "object" && + Intl && + typeof Intl.NumberFormat == "function" + ); +} + +type Props = NumberFieldProps & { + unit: string; +}; + +/** + * This field is used to display a number formatted according to the browser locale, right aligned. and uses {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl `Intl`} to display date format. + * + * @see {@link https://refine.dev/docs/ui-frameworks/antd/components/fields/number} for more details. + */ +export const NumberFieldUnit: React.FC = ({ + value, + locale, + options, + ...rest +}) => { + const number = Number(value); + + return ( + + {toLocaleStringSupportsOptions() + ? number.toLocaleString(locale, options) + : number}{" "} + {rest.unit} + + ); +}; diff --git a/client/src/pages/filaments/list.tsx b/client/src/pages/filaments/list.tsx index 8461f0e..1ea3682 100644 --- a/client/src/pages/filaments/list.tsx +++ b/client/src/pages/filaments/list.tsx @@ -6,9 +6,9 @@ import { EditButton, ShowButton, DateField, - NumberField, } from "@refinedev/antd"; import { Table, Space } from "antd"; +import { NumberFieldUnit } from "../../components/numberField"; export const FilamentList: React.FC = () => { const { tableProps } = useTable({ @@ -27,12 +27,10 @@ export const FilamentList: React.FC = () => { dataIndex="density" title="Density" render={(value) => ( - @@ -42,12 +40,10 @@ export const FilamentList: React.FC = () => { dataIndex="diameter" title="Diameter" render={(value) => ( - @@ -61,12 +57,10 @@ export const FilamentList: React.FC = () => { return <>; } return ( - @@ -81,12 +75,10 @@ export const FilamentList: React.FC = () => { return <>; } return ( - diff --git a/client/src/pages/filaments/show.tsx b/client/src/pages/filaments/show.tsx index a02e884..fe09c47 100644 --- a/client/src/pages/filaments/show.tsx +++ b/client/src/pages/filaments/show.tsx @@ -2,6 +2,7 @@ import React from "react"; import { IResourceComponentsProps, useShow } from "@refinedev/core"; import { Show, NumberField, DateField, TextField } from "@refinedev/antd"; import { Typography } from "antd"; +import { NumberFieldUnit } from "../../components/numberField"; const { Title } = Typography; @@ -26,42 +27,34 @@ export const FilamentShow: React.FC = () => { Price Density - Diameter - Weight - Spool Weight - diff --git a/client/src/pages/spools/list.tsx b/client/src/pages/spools/list.tsx index 71c481a..9d95ac7 100644 --- a/client/src/pages/spools/list.tsx +++ b/client/src/pages/spools/list.tsx @@ -6,10 +6,10 @@ import { EditButton, ShowButton, DateField, - NumberField, TextField, } from "@refinedev/antd"; import { Table, Space } from "antd"; +import { NumberFieldUnit } from "../../components/numberField"; export const SpoolList: React.FC = () => { const { tableProps } = useTable({ @@ -34,12 +34,10 @@ export const SpoolList: React.FC = () => { title="Used Weight" render={(value) => { return ( - @@ -54,12 +52,10 @@ export const SpoolList: React.FC = () => { return ; } return ( - diff --git a/client/src/pages/spools/show.tsx b/client/src/pages/spools/show.tsx index d589982..1b40fba 100644 --- a/client/src/pages/spools/show.tsx +++ b/client/src/pages/spools/show.tsx @@ -2,6 +2,7 @@ import React from "react"; import { IResourceComponentsProps, useShow } from "@refinedev/core"; import { Show, NumberField, DateField, TextField } from "@refinedev/antd"; import { Typography } from "antd"; +import { NumberFieldUnit } from "../../components/numberField"; const { Title } = Typography; @@ -28,22 +29,18 @@ export const SpoolShow: React.FC = () => { <>{filamentData?.data?.id} )} */} Remaining Weight - Used Weight -