2019-07-03 16:49:31 +00:00
|
|
|
import { React, T } from 'Components'
|
2019-09-11 08:06:51 +00:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2019-09-23 18:04:34 +00:00
|
|
|
import { formatValue } from 'Engine/format'
|
2019-06-11 10:03:45 +00:00
|
|
|
|
2019-07-03 16:49:31 +00:00
|
|
|
// let booleanTranslations = { true: '✅', false: '❌' }
|
2019-06-20 16:51:40 +00:00
|
|
|
|
2019-06-11 10:03:45 +00:00
|
|
|
let booleanTranslations = {
|
|
|
|
fr: { true: 'Oui', false: 'Non' },
|
|
|
|
en: { true: 'Yes', false: 'No' }
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:30:31 +00:00
|
|
|
let style = customStyle => `
|
2019-06-20 16:51:40 +00:00
|
|
|
font-family: 'Courier New', Courier, monospace;
|
2019-07-12 10:30:31 +00:00
|
|
|
|
|
|
|
${customStyle}
|
2019-06-20 16:51:40 +00:00
|
|
|
`
|
2019-06-11 10:03:45 +00:00
|
|
|
|
2019-09-23 18:04:34 +00:00
|
|
|
export default function Value({
|
2019-09-11 08:06:51 +00:00
|
|
|
nodeValue: value,
|
|
|
|
unit,
|
|
|
|
nilValueSymbol,
|
|
|
|
maximumFractionDigits,
|
|
|
|
minimumFractionDigits,
|
|
|
|
children,
|
|
|
|
negative,
|
|
|
|
customCSS = ''
|
2019-09-23 18:04:34 +00:00
|
|
|
}) {
|
|
|
|
const { language } = useTranslation().i18n
|
2019-07-09 08:57:36 +00:00
|
|
|
|
2019-09-11 08:06:51 +00:00
|
|
|
/* Either an entire rule object is passed, or just the right attributes and the value as a JSX child*/
|
|
|
|
let nodeValue = value === undefined ? children : value
|
2019-06-20 16:51:40 +00:00
|
|
|
|
2019-09-11 08:06:51 +00:00
|
|
|
if (
|
|
|
|
(nilValueSymbol !== undefined && nodeValue === 0) ||
|
|
|
|
Number.isNaN(nodeValue) ||
|
|
|
|
nodeValue === null
|
|
|
|
)
|
|
|
|
return (
|
2019-07-12 10:30:31 +00:00
|
|
|
<span css={style(customCSS)} className="value">
|
2019-09-11 08:06:51 +00:00
|
|
|
-
|
2019-07-01 15:59:57 +00:00
|
|
|
</span>
|
|
|
|
)
|
2019-09-11 08:06:51 +00:00
|
|
|
let valueType = typeof nodeValue,
|
|
|
|
formattedValue =
|
|
|
|
valueType === 'string' ? (
|
|
|
|
<T>{nodeValue}</T>
|
|
|
|
) : valueType === 'object' ? (
|
|
|
|
nodeValue.nom
|
|
|
|
) : valueType === 'boolean' ? (
|
|
|
|
booleanTranslations[language][nodeValue]
|
|
|
|
) : (
|
2019-09-23 18:04:34 +00:00
|
|
|
formatValue({
|
|
|
|
minimumFractionDigits,
|
|
|
|
maximumFractionDigits,
|
|
|
|
language,
|
2019-10-12 18:23:54 +00:00
|
|
|
unit,
|
|
|
|
value: nodeValue
|
2019-09-23 18:04:34 +00:00
|
|
|
})
|
2019-09-11 08:06:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return nodeValue == undefined ? null : (
|
|
|
|
<span css={style(customCSS)} className="value">
|
|
|
|
{negative ? '-' : ''}
|
|
|
|
{formattedValue}
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|