Skip to content

Commit

Permalink
Add currency and accounting number formats to module fields
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinDY committed Apr 11, 2024
1 parent 1a7265e commit dd420d0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@
</b-form-group>
</b-col>

<b-col
cols="12"
lg="6"
>
<b-form-group
:label="$t('kind.number.presetFormats.label')"
label-class="text-primary"
style="white-space: pre-line;"
:description="formattedOptionsDescription"
>
<b-form-select
v-model="f.options.presetFormat"
:options="formatOptions"
/>
</b-form-group>
</b-col>

<b-col
cols="12"
>
Expand All @@ -101,9 +118,15 @@
<b-table-simple class="w-100 table-sm">
<thead>
<tr>
<th>{{ $t('kind.number.exampleInput') }}</th>
<th>{{ $t('kind.number.exampleFormat') }}</th>
<th>{{ $t('kind.number.exampleResult') }}</th>
<th id="example-input">
{{ $t('kind.number.exampleInput') }}
</th>
<th id="example-format">
{{ $t('kind.number.exampleFormat') }}
</th>
<th id="example-result">
{{ $t('kind.number.exampleResult') }}
</th>
</tr>
</thead>

Expand Down Expand Up @@ -370,9 +393,25 @@ export default {
record: undefined,
errors: new validator.Validated(),
},
formatOptions: [
{ value: 'currencyFormat', text: this.$t('kind.number.presetFormats.options.currency') },
{ value: 'accountingNumber', text: this.$t('kind.number.presetFormats.options.accountingNumber') },
],
}
},
computed: {
formattedOptionsDescription () {
const currency = this.$t('kind.number.presetFormats.description.currency')
const accountingNumber = this.$t('kind.number.presetFormats.description.accountingNumber')
let translation = [currency, accountingNumber].join('\r\n')
return translation
},
},
watch: {
'field.options.display': {
handler (display) {
Expand Down Expand Up @@ -430,6 +469,7 @@ export default {
this.displayOptions = []
this.variants = []
this.mock = {}
this.formatOptions = []
},
},
}
Expand Down
24 changes: 22 additions & 2 deletions lib/js/src/compose/types/module-field/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Threshold {
}

interface NumberOptions extends Options {
presetFormat: string;
format: string;
prefix: string;
suffix: string;
Expand All @@ -29,6 +30,7 @@ interface NumberOptions extends Options {

const defaults = (): Readonly<NumberOptions> => Object.freeze({
...defaultOptions(),
presetFormat: 'currencyFormat',
precision: 3,
multiDelimiter: '\n',
display: 'number', // Either number or progress (progress bar)
Expand Down Expand Up @@ -61,7 +63,7 @@ export class ModuleFieldNumber extends ModuleField {
if (!o) return
super.applyOptions(o)

Apply(this.options, o, String, 'format', 'prefix', 'suffix', 'multiDelimiter', 'display', 'variant')
Apply(this.options, o, String, 'format', 'prefix', 'suffix', 'multiDelimiter', 'display', 'variant', 'presetFormat')
Apply(this.options, o, Number, 'precision', 'min', 'max')
Apply(this.options, o, Boolean, 'showValue', 'showRelative', 'showProgress', 'animated')

Expand All @@ -84,15 +86,33 @@ export class ModuleFieldNumber extends ModuleField {
default:
n = 0
}

let out = `${n}`

if (o.format && o.format.length > 0) {
out = numeral(n).format(o.format)
} else {
out = fmt.number(n)
}

return '' + o.prefix + out + o.suffix
if (o.presetFormat === 'accountingNumber') {
out = formatChartValueAsAccountingNumber(Number(n))
}

return '' + o.prefix + (out || n) + o.suffix
}
}

export function formatChartValueAsAccountingNumber (value: number): string {
let result = ''

if (value < 0) {
result = `(${Math.abs(value)})`
} else if (value === 0) {
result = '-'
}

return result
}

Registry.set(kind, ModuleFieldNumber)
8 changes: 8 additions & 0 deletions locale/en/corteza-webapp-compose/field.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ kind:
thresholds:
label: Thresholds
description: Set which variant to show if value is equal or larger than the threshold
presetFormats:
label: 'Format values using:'
options:
currency: Currency (Default)
accountingNumber: Accounting number
description:
currency: Currency format displays the data using the default or set by user configuration
accountingNumber: 'Accounting number displays the data in the same way as the currency format with the two differences: 1) negative numbers are transformed into positive and added in brackets and 2) the value 0 is substituted by a dash'
record:
currentUnnamedModule: (Current unnamed module)
label: Record
Expand Down

0 comments on commit dd420d0

Please sign in to comment.