Улучшено форматирование значения виджета на дашборде ННБ

This commit is contained in:
goodmice 2022-07-18 18:03:37 +05:00
parent 4606d96c88
commit 1da3bbe290

View File

@ -17,10 +17,20 @@ export type WidgetSettings<T = any> = {
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<BaseWidgetProps>(({ value, onRemove, onEdit, ...s
</div>
</div>
<div className={'widget_value'} style={{ color: sets.valueColor }}>
{(sets.formatter === null ? value : sets.formatter?.(value)) ?? sets.defaultValue ?? '----'}
{(sets.formatter === null ? value : sets.formatter?.(value)) ?? sets.defaultValue ?? emptyNumber}
</div>
<div className={'widget_units'} style={{ color: sets.unitColor }}>{sets.unit}</div>
</div>