From 1da3bbe29025e61d0006bf9bf615f896af47b3c1 Mon Sep 17 00:00:00 2001 From: goodmice Date: Mon, 18 Jul 2022 18:03:37 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=D0=B8=D0=B4=D0=B6=D0=B5=D1=82=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D0=B4=D0=B0=D1=88=D0=B1=D0=BE=D1=80=D0=B4?= =?UTF-8?q?=D0=B5=20=D0=9D=D0=9D=D0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/widgets/BaseWidget.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/components/widgets/BaseWidget.tsx b/src/components/widgets/BaseWidget.tsx index c17ff10..8ae09ad 100644 --- a/src/components/widgets/BaseWidget.tsx +++ b/src/components/widgets/BaseWidget.tsx @@ -17,10 +17,20 @@ export type WidgetSettings = { unitColor?: string, } +const emptyNumber = '----' + +const defaultFormatter = (v: any) => { + if (typeof v === 'undefined' || v === null || String(v) === 'NaN') return emptyNumber + if (Number.isNaN(+v)) return v + const f = parseFloat(v) + if (Number.isFinite(v)) return f.toFixed(2) + return `${f < 0 ? '-' : ''}\u221E` +} + export const defaultSettings: WidgetSettings = { unit: '----', label: 'Виджет', - formatter: v => isNaN(v) ? v : parseFloat(v).toFixed(2), + formatter: defaultFormatter, labelColor: '#000000', valueColor: '#000000', @@ -57,7 +67,7 @@ export const BaseWidget = memo(({ value, onRemove, onEdit, ...s
- {(sets.formatter === null ? value : sets.formatter?.(value)) ?? sets.defaultValue ?? '----'} + {(sets.formatter === null ? value : sets.formatter?.(value)) ?? sets.defaultValue ?? emptyNumber}
{sets.unit}