Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import { memo, type ComponentPropsWithRef } from 'react'; import { Typography } from '@allshares/studio-design-system'; import { FormatsEnum, isUserModelRefValue, type PropertyRef } from '@amalia/data-capture/fields/types'; import { type MergeAll } from '@amalia/ext/typescript'; import { FormattedAmount, FormattedValue } from '@amalia/kernel/intl/components'; import { isCurrencyValue, type CurrencyValue } from '@amalia/kernel/monetary/types'; import { type KeysOfUserWithStringValues } from '@amalia/tenants/users/types'; import { BooleanPrettyFormat, toBoolean } from './boolean'; import { accentuatedLabel, defaultLabel, dimmedLabel, IS_COMPUTING_CLASSNAME, label, } from './FieldValuePrettyFormat.styles'; import { LabelSize, LabelVariant, TypographyVariantMap } from './types'; import { UserContainer } from './user'; export { isFieldValuePrettyFormatCompatible } from './FieldValuePrettyFormat.compatibility'; export { LabelVariant } from './types'; export type ValueWithFormat<TFormat extends FormatsEnum, Value> = { format?: TFormat; value?: Value | null; }; export const getPrettyFormatProps = ( format?: FormatsEnum, value?: unknown, valueRef?: PropertyRef, ): ValuesWithFormats & { valueRef?: PropertyRef } => { if (isUserModelRefValue(value, valueRef)) { return { format: FormatsEnum.text, value, valueRef }; } if (format === FormatsEnum.boolean) { return { format, value: value as boolean }; } if (format === FormatsEnum.currency) { return { format, value: value as CurrencyValue }; } if (format === FormatsEnum.percent || format === FormatsEnum.number) { return { format, value: value as number }; } return { format: format ?? FormatsEnum.text, value: value as string }; }; export type ValuesWithFormats = | ValueWithFormat<(typeof FormatsEnum)['date-time'], string> | ValueWithFormat<FormatsEnum.boolean, boolean | string> | ValueWithFormat<FormatsEnum.currency, CurrencyValue | number | string> | ValueWithFormat<FormatsEnum.date, string> | ValueWithFormat<FormatsEnum.number, number> | ValueWithFormat<FormatsEnum.percent, number> | ValueWithFormat<FormatsEnum.table, string> | ValueWithFormat<FormatsEnum.text, string>; export type FieldValuePrettyFormatProps = MergeAll< [ ComponentPropsWithRef<'div'>, ValuesWithFormats, { readonly variant?: LabelVariant; readonly valueRef?: PropertyRef; readonly userField?: KeysOfUserWithStringValues; readonly size?: LabelSize; readonly isComputing?: boolean; }, ] >; export const FieldValuePrettyFormat = memo(function FieldValuePrettyFormat({ format, value, valueRef, userField, variant = LabelVariant.DEFAULT, size = LabelSize.MEDIUM, isComputing, ref, ...restProps }: FieldValuePrettyFormatProps) { const cssStyles = [ variant === LabelVariant.DEFAULT && defaultLabel, variant === LabelVariant.ACCENTUATED && accentuatedLabel, variant === LabelVariant.DIMMED && dimmedLabel, ]; const className = isComputing ? IS_COMPUTING_CLASSNAME : undefined; return ( <div {...restProps} ref={ref} css={label} > {(() => { // PropertyRef has higher priority than format to display the correct component if (isUserModelRefValue(value, valueRef)) { return ( <UserContainer isComputing={isComputing} property={userField || 'externalId'} value={value} variant={variant} /> ); } switch (format) { case FormatsEnum.boolean: return ( <BooleanPrettyFormat isComputing={isComputing} size={size} value={toBoolean(value as boolean | string | null | undefined)} variant={variant} /> ); case FormatsEnum.currency: return ( <Typography className={className} css={cssStyles} variant={TypographyVariantMap[variant][size]} > {isCurrencyValue(value) ? ( <FormattedAmount currencySymbol={value.symbol} value={value.value} /> ) : ( // This is a fallback for the case where we don't have the currency symbol. // I'm not sure this would happen. <FormattedValue format={FormatsEnum.text} value={value} /> )} </Typography> ); // For all other formats, render the value in a Typography with the correct style. default: return ( <Typography className={className} css={cssStyles} variant={TypographyVariantMap[variant][size]} > <FormattedValue format={format ?? FormatsEnum.text} value={value} /> </Typography> ); } })()} </div> ); }); |