@ni/nimble-components 20.3.0 → 20.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16288,7 +16288,7 @@
16288
16288
 
16289
16289
  /**
16290
16290
  * Do not edit directly
16291
- * Generated on Fri, 15 Sep 2023 14:20:06 GMT
16291
+ * Generated on Fri, 15 Sep 2023 20:18:18 GMT
16292
16292
  */
16293
16293
 
16294
16294
  const Information100DarkUi = "#a46eff";
@@ -68038,6 +68038,298 @@ img.ProseMirror-separator {
68038
68038
  .register(nimbleTableColumnEnumText());
68039
68039
  DesignSystem.tagFor(TableColumnEnumText);
68040
68040
 
68041
+ /**
68042
+ * The group header view for displaying number fields as text.
68043
+ */
68044
+ class TableColumnNumberTextGroupHeaderView extends TableColumnTextGroupHeaderViewBase {
68045
+ columnConfigChanged() {
68046
+ this.updateText();
68047
+ }
68048
+ groupHeaderValueChanged() {
68049
+ this.updateText();
68050
+ }
68051
+ updateText() {
68052
+ this.text = this.columnConfig?.formatter?.formatValue(this.groupHeaderValue)
68053
+ ?? '';
68054
+ }
68055
+ }
68056
+ const tableColumnNumberTextGroupHeaderView = TableColumnNumberTextGroupHeaderView.compose({
68057
+ baseName: 'table-column-number-text-group-header-view',
68058
+ template: template$9,
68059
+ styles: styles$b
68060
+ });
68061
+ DesignSystem.getOrCreate()
68062
+ .withPrefix('nimble')
68063
+ .register(tableColumnNumberTextGroupHeaderView());
68064
+ const tableColumnNumberTextGroupHeaderTag = DesignSystem.tagFor(TableColumnNumberTextGroupHeaderView);
68065
+
68066
+ /**
68067
+ * A cell view for displaying number fields as text
68068
+ */
68069
+ class TableColumnNumberTextCellView extends TableColumnTextCellViewBase {
68070
+ columnConfigChanged() {
68071
+ this.updateText();
68072
+ this.alignment = this.columnConfig?.alignment ?? TextCellViewBaseAlignment.left;
68073
+ }
68074
+ cellRecordChanged() {
68075
+ this.updateText();
68076
+ }
68077
+ updateText() {
68078
+ this.text = this.columnConfig?.formatter?.formatValue(this.cellRecord?.value)
68079
+ ?? '';
68080
+ }
68081
+ }
68082
+ const numberTextCellView = TableColumnNumberTextCellView.compose({
68083
+ baseName: 'table-column-number-text-cell-view',
68084
+ template: template$8,
68085
+ styles: styles$a
68086
+ });
68087
+ DesignSystem.getOrCreate().withPrefix('nimble').register(numberTextCellView());
68088
+ const tableColumnNumberTextCellViewTag = DesignSystem.tagFor(TableColumnNumberTextCellView);
68089
+
68090
+ /**
68091
+ * Formatting scheme for the number-text table column
68092
+ */
68093
+ const NumberTextFormat = {
68094
+ default: undefined,
68095
+ decimal: 'decimal'
68096
+ };
68097
+ /**
68098
+ * The aligment of the value in the number-text table column.
68099
+ * The `default` alignment is determined by the column's `NumberTextFormat`.
68100
+ */
68101
+ const NumberTextAlignment = {
68102
+ default: undefined,
68103
+ left: 'left',
68104
+ right: 'right'
68105
+ };
68106
+
68107
+ /**
68108
+ * The base class for number formatters used by the number-text column.
68109
+ */
68110
+ class NumberFormatter {
68111
+ /**
68112
+ * Tries to format the passed value using the `format()` function implemented by a concrete implementation of the class.
68113
+ * Returns an empty string if the value is not a number or if `format()` throws an error.
68114
+ */
68115
+ formatValue(value) {
68116
+ if (typeof value !== 'number') {
68117
+ return '';
68118
+ }
68119
+ try {
68120
+ return this.format(value);
68121
+ }
68122
+ catch {
68123
+ return '';
68124
+ }
68125
+ }
68126
+ }
68127
+
68128
+ /**
68129
+ * The formatter for a number-text column whose format is configured to be 'default'.
68130
+ */
68131
+ class DefaultFormatter extends NumberFormatter {
68132
+ constructor(locale) {
68133
+ super();
68134
+ this.defaultFormatter = new Intl.NumberFormat(locale, {
68135
+ maximumSignificantDigits: DefaultFormatter.maximumDigits,
68136
+ useGrouping: true
68137
+ });
68138
+ this.leadingZeroFormatter = new Intl.NumberFormat(locale, {
68139
+ maximumFractionDigits: DefaultFormatter.maximumDigits - 1,
68140
+ useGrouping: true
68141
+ });
68142
+ this.exponentialFormatter = new Intl.NumberFormat(locale, {
68143
+ maximumSignificantDigits: DefaultFormatter.maximumDigits,
68144
+ notation: 'scientific'
68145
+ });
68146
+ }
68147
+ format(number) {
68148
+ // The NumberFormat option of `signDisplay: "negative"` is not supported in all browsers nimble supports.
68149
+ // Because that option cannot be used to avoid rendering "-0", coerce the value -0 to 0 prior to formatting.
68150
+ const valueToFormat = number === 0 ? 0 : number;
68151
+ const formatter = this.getFormatterForNumber(valueToFormat);
68152
+ return formatter.format(valueToFormat);
68153
+ }
68154
+ getFormatterForNumber(number) {
68155
+ if (number === 0) {
68156
+ return this.defaultFormatter;
68157
+ }
68158
+ const absoluteValue = Math.abs(number);
68159
+ if (absoluteValue >= DefaultFormatter.exponentialUpperBound
68160
+ || absoluteValue < DefaultFormatter.exponentialLowerBound) {
68161
+ return this.exponentialFormatter;
68162
+ }
68163
+ // Ideally, we could set 'roundingPriority: "lessPrecision"' with a formatter that has both 'maximumSignificantDigits' and
68164
+ // 'maximumFractionDigits' configured instead of having two different formatters that we conditionally choose between. However,
68165
+ // 'roundingPrioirty' is not supported yet in all browsers nimble supports.
68166
+ if (absoluteValue < 1) {
68167
+ return this.leadingZeroFormatter;
68168
+ }
68169
+ return this.defaultFormatter;
68170
+ }
68171
+ }
68172
+ // The maximum number of digits that should be rendered for any given value.
68173
+ DefaultFormatter.maximumDigits = 6;
68174
+ // Use exponential notation for numbers that will be rendered with 3 leading 0s or more.
68175
+ // Because a maximum of 6 digits are rendered, showing more than 3 leading 0s is not ideal
68176
+ // because then at least half of the displayed digits will be leading 0s.
68177
+ DefaultFormatter.exponentialLowerBound = 0.000995;
68178
+ // Use exponential formatting for numbers whose magnitude cannot otherwise be displayed
68179
+ // with 6 digits or less.
68180
+ DefaultFormatter.exponentialUpperBound = 999999.5;
68181
+
68182
+ /**
68183
+ * The formatter for a number-text column whose format is configured to be 'decimal'.
68184
+ */
68185
+ class DecimalFormatter extends NumberFormatter {
68186
+ constructor(locale, decimalsToDisplay) {
68187
+ super();
68188
+ this.formatter = new Intl.NumberFormat(locale, {
68189
+ maximumFractionDigits: decimalsToDisplay,
68190
+ minimumFractionDigits: decimalsToDisplay,
68191
+ useGrouping: true
68192
+ });
68193
+ this.tenPowDecimalDigits = 10 ** decimalsToDisplay;
68194
+ }
68195
+ format(number) {
68196
+ // The NumberFormat option of `signDisplay: "negative"` is not supported in all browsers nimble supports.
68197
+ // Because that option cannot be used to avoid rendering "-0", coerce the value -0 to 0 prior to formatting.
68198
+ const valueToFormat = this.willRoundToZero(number) ? 0 : number;
68199
+ return this.formatter.format(valueToFormat);
68200
+ }
68201
+ willRoundToZero(number) {
68202
+ // Multiply the value by 10 raised to decimal-digits so that Math.round can be used to emulate rounding to
68203
+ // decimal-digits decimal places. If that rounded value is 0, then the value will be rendered with only 0s.
68204
+ return Math.round(number * this.tenPowDecimalDigits) === 0;
68205
+ }
68206
+ }
68207
+
68208
+ const numberTextValidityFlagNames = ['invalidDecimalDigits'];
68209
+ // The maximum and minimum allowed configuration for 'maximumFractionDigits'
68210
+ // and 'minimumFractionDigits' on the NumberFormat.
68211
+ const minimumValidDecimalDigits = 0;
68212
+ const maximumValidDecimalDigits = 20;
68213
+ /**
68214
+ * Validator for TableColumnNumberText.
68215
+ */
68216
+ class TableColumnNumberTextValidator extends ColumnValidator {
68217
+ constructor(columnInternals) {
68218
+ super(columnInternals, numberTextValidityFlagNames);
68219
+ }
68220
+ validateDecimalDigits(format, decimalDigits) {
68221
+ const shouldValidateDecimalDigitsValue = format === NumberTextFormat.decimal
68222
+ && typeof decimalDigits === 'number';
68223
+ const invalid = shouldValidateDecimalDigitsValue
68224
+ ? this.isInvalidDecimalDigitsValue(decimalDigits)
68225
+ : false;
68226
+ this.setConditionValue('invalidDecimalDigits', invalid);
68227
+ }
68228
+ isInvalidDecimalDigitsValue(decimalDigits) {
68229
+ return (decimalDigits < minimumValidDecimalDigits
68230
+ || decimalDigits > maximumValidDecimalDigits);
68231
+ }
68232
+ }
68233
+
68234
+ const defaultDecimalDigits = 2;
68235
+ /**
68236
+ * The table column for displaying numbers as text.
68237
+ */
68238
+ class TableColumnNumberText extends TableColumnTextBase {
68239
+ constructor() {
68240
+ super(...arguments);
68241
+ /** @internal */
68242
+ this.validator = new TableColumnNumberTextValidator(this.columnInternals);
68243
+ this.langSubscriber = {
68244
+ handleChange: () => {
68245
+ this.updateColumnConfig();
68246
+ }
68247
+ };
68248
+ }
68249
+ connectedCallback() {
68250
+ super.connectedCallback();
68251
+ lang$1.subscribe(this.langSubscriber, this);
68252
+ this.updateColumnConfig();
68253
+ }
68254
+ disconnectedCallback() {
68255
+ super.disconnectedCallback();
68256
+ lang$1.unsubscribe(this.langSubscriber, this);
68257
+ }
68258
+ get validity() {
68259
+ return this.validator.getValidity();
68260
+ }
68261
+ getColumnInternalsOptions() {
68262
+ return {
68263
+ cellRecordFieldNames: ['value'],
68264
+ cellViewTag: tableColumnNumberTextCellViewTag,
68265
+ groupHeaderViewTag: tableColumnNumberTextGroupHeaderTag,
68266
+ delegatedEvents: [],
68267
+ sortOperation: TableColumnSortOperation.basic
68268
+ };
68269
+ }
68270
+ formatChanged() {
68271
+ this.updateColumnConfig();
68272
+ }
68273
+ alignmentChanged() {
68274
+ this.updateColumnConfig();
68275
+ }
68276
+ decimalDigitsChanged() {
68277
+ this.updateColumnConfig();
68278
+ }
68279
+ updateColumnConfig() {
68280
+ this.validator.validateDecimalDigits(this.format, this.decimalDigits);
68281
+ if (this.validator.isValid()) {
68282
+ const columnConfig = {
68283
+ formatter: this.createFormatter(),
68284
+ alignment: this.determineCellContentAlignment()
68285
+ };
68286
+ this.columnInternals.columnConfig = columnConfig;
68287
+ }
68288
+ else {
68289
+ this.columnInternals.columnConfig = undefined;
68290
+ }
68291
+ }
68292
+ createFormatter() {
68293
+ switch (this.format) {
68294
+ case NumberTextFormat.decimal:
68295
+ return new DecimalFormatter(lang$1.getValueFor(this), this.decimalDigits ?? defaultDecimalDigits);
68296
+ default:
68297
+ return new DefaultFormatter(lang$1.getValueFor(this));
68298
+ }
68299
+ }
68300
+ determineCellContentAlignment() {
68301
+ if (this.alignment === NumberTextAlignment.left) {
68302
+ return TextCellViewBaseAlignment.left;
68303
+ }
68304
+ if (this.alignment === NumberTextAlignment.right) {
68305
+ return TextCellViewBaseAlignment.right;
68306
+ }
68307
+ // Look at format to determine the default alignment
68308
+ if (this.format === NumberTextFormat.decimal) {
68309
+ return TextCellViewBaseAlignment.right;
68310
+ }
68311
+ return TextCellViewBaseAlignment.left;
68312
+ }
68313
+ }
68314
+ __decorate$1([
68315
+ attr
68316
+ ], TableColumnNumberText.prototype, "format", void 0);
68317
+ __decorate$1([
68318
+ attr
68319
+ ], TableColumnNumberText.prototype, "alignment", void 0);
68320
+ __decorate$1([
68321
+ attr({ attribute: 'decimal-digits', converter: nullableNumberConverter })
68322
+ ], TableColumnNumberText.prototype, "decimalDigits", void 0);
68323
+ const nimbleTableColumnNumberText = TableColumnNumberText.compose({
68324
+ baseName: 'table-column-number-text',
68325
+ template: template$b,
68326
+ styles: styles$d
68327
+ });
68328
+ DesignSystem.getOrCreate()
68329
+ .withPrefix('nimble')
68330
+ .register(nimbleTableColumnNumberText());
68331
+ DesignSystem.tagFor(TableColumnNumberText);
68332
+
68041
68333
  const iconValidityFlagNames = [
68042
68334
  ...enumBaseValidityFlagNames,
68043
68335
  'invalidIconName'