Client: Fixed incorrect units for some numbers

This commit is contained in:
Donkie
2023-05-17 22:03:19 +02:00
parent 9ae6bafd35
commit 40dc48668b
5 changed files with 68 additions and 50 deletions

View File

@@ -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<Props> = ({
value,
locale,
options,
...rest
}) => {
const number = Number(value);
return (
<Text {...rest}>
{toLocaleStringSupportsOptions()
? number.toLocaleString(locale, options)
: number}{" "}
{rest.unit}
</Text>
);
};

View File

@@ -6,9 +6,9 @@ import {
EditButton, EditButton,
ShowButton, ShowButton,
DateField, DateField,
NumberField,
} from "@refinedev/antd"; } from "@refinedev/antd";
import { Table, Space } from "antd"; import { Table, Space } from "antd";
import { NumberFieldUnit } from "../../components/numberField";
export const FilamentList: React.FC<IResourceComponentsProps> = () => { export const FilamentList: React.FC<IResourceComponentsProps> = () => {
const { tableProps } = useTable({ const { tableProps } = useTable({
@@ -27,12 +27,10 @@ export const FilamentList: React.FC<IResourceComponentsProps> = () => {
dataIndex="density" dataIndex="density"
title="Density" title="Density"
render={(value) => ( render={(value) => (
<NumberField <NumberFieldUnit
value={value} value={value}
unit="g/cm³"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 2, maximumFractionDigits: 2,
}} }}
/> />
@@ -42,12 +40,10 @@ export const FilamentList: React.FC<IResourceComponentsProps> = () => {
dataIndex="diameter" dataIndex="diameter"
title="Diameter" title="Diameter"
render={(value) => ( render={(value) => (
<NumberField <NumberFieldUnit
value={value} value={value}
unit="mm"
options={{ options={{
unitDisplay: "short",
unit: "millimeter",
style: "unit",
maximumFractionDigits: 2, maximumFractionDigits: 2,
}} }}
/> />
@@ -61,12 +57,10 @@ export const FilamentList: React.FC<IResourceComponentsProps> = () => {
return <></>; return <></>;
} }
return ( return (
<NumberField <NumberFieldUnit
value={value} value={value}
unit="g"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 1, maximumFractionDigits: 1,
}} }}
/> />
@@ -81,12 +75,10 @@ export const FilamentList: React.FC<IResourceComponentsProps> = () => {
return <></>; return <></>;
} }
return ( return (
<NumberField <NumberFieldUnit
value={value} value={value}
unit="g"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 1, maximumFractionDigits: 1,
}} }}
/> />

View File

@@ -2,6 +2,7 @@ import React from "react";
import { IResourceComponentsProps, useShow } from "@refinedev/core"; import { IResourceComponentsProps, useShow } from "@refinedev/core";
import { Show, NumberField, DateField, TextField } from "@refinedev/antd"; import { Show, NumberField, DateField, TextField } from "@refinedev/antd";
import { Typography } from "antd"; import { Typography } from "antd";
import { NumberFieldUnit } from "../../components/numberField";
const { Title } = Typography; const { Title } = Typography;
@@ -26,42 +27,34 @@ export const FilamentShow: React.FC<IResourceComponentsProps> = () => {
<Title level={5}>Price</Title> <Title level={5}>Price</Title>
<NumberField value={record?.price ?? ""} /> <NumberField value={record?.price ?? ""} />
<Title level={5}>Density</Title> <Title level={5}>Density</Title>
<NumberField <NumberFieldUnit
value={record?.density ?? ""} value={record?.density ?? ""}
unit="g/cm³"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 2, maximumFractionDigits: 2,
}} }}
/> />
<Title level={5}>Diameter</Title> <Title level={5}>Diameter</Title>
<NumberField <NumberFieldUnit
value={record?.diameter ?? ""} value={record?.diameter ?? ""}
unit="mm"
options={{ options={{
unitDisplay: "short",
unit: "millimeter",
style: "unit",
maximumFractionDigits: 2, maximumFractionDigits: 2,
}} }}
/> />
<Title level={5}>Weight</Title> <Title level={5}>Weight</Title>
<NumberField <NumberFieldUnit
value={record?.weight ?? ""} value={record?.weight ?? ""}
unit="g"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 1, maximumFractionDigits: 1,
}} }}
/> />
<Title level={5}>Spool Weight</Title> <Title level={5}>Spool Weight</Title>
<NumberField <NumberFieldUnit
value={record?.spool_weight ?? ""} value={record?.spool_weight ?? ""}
unit="g"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 1, maximumFractionDigits: 1,
}} }}
/> />

View File

@@ -6,10 +6,10 @@ import {
EditButton, EditButton,
ShowButton, ShowButton,
DateField, DateField,
NumberField,
TextField, TextField,
} from "@refinedev/antd"; } from "@refinedev/antd";
import { Table, Space } from "antd"; import { Table, Space } from "antd";
import { NumberFieldUnit } from "../../components/numberField";
export const SpoolList: React.FC<IResourceComponentsProps> = () => { export const SpoolList: React.FC<IResourceComponentsProps> = () => {
const { tableProps } = useTable({ const { tableProps } = useTable({
@@ -34,12 +34,10 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
title="Used Weight" title="Used Weight"
render={(value) => { render={(value) => {
return ( return (
<NumberField <NumberFieldUnit
value={value} value={value}
unit="g"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 1, maximumFractionDigits: 1,
}} }}
/> />
@@ -54,12 +52,10 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
return <TextField value="Unknown" />; return <TextField value="Unknown" />;
} }
return ( return (
<NumberField <NumberFieldUnit
value={Math.max(value, 0)} value={Math.max(value, 0)}
unit="g"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 1, maximumFractionDigits: 1,
}} }}
/> />

View File

@@ -2,6 +2,7 @@ import React from "react";
import { IResourceComponentsProps, useShow } from "@refinedev/core"; import { IResourceComponentsProps, useShow } from "@refinedev/core";
import { Show, NumberField, DateField, TextField } from "@refinedev/antd"; import { Show, NumberField, DateField, TextField } from "@refinedev/antd";
import { Typography } from "antd"; import { Typography } from "antd";
import { NumberFieldUnit } from "../../components/numberField";
const { Title } = Typography; const { Title } = Typography;
@@ -28,22 +29,18 @@ export const SpoolShow: React.FC<IResourceComponentsProps> = () => {
<>{filamentData?.data?.id}</> <>{filamentData?.data?.id}</>
)} */} )} */}
<Title level={5}>Remaining Weight</Title> <Title level={5}>Remaining Weight</Title>
<NumberField <NumberFieldUnit
value={record?.remaining_weight ?? ""} value={record?.remaining_weight ?? ""}
unit="g"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 1, maximumFractionDigits: 1,
}} }}
/> />
<Title level={5}>Used Weight</Title> <Title level={5}>Used Weight</Title>
<NumberField <NumberFieldUnit
value={record?.used_weight ?? ""} value={record?.used_weight ?? ""}
unit="g"
options={{ options={{
unitDisplay: "short",
unit: "gram",
style: "unit",
maximumFractionDigits: 1, maximumFractionDigits: 1,
}} }}
/> />