Skip to content

Commit ce51229

Browse files
authored
fix: Formatting Rule Doesn't use default set by user (#1547)
* Default Formatting Rule should change depending on the defaults set by the user * Changing columnType should fill the input with the default string
1 parent a273e64 commit ce51229

3 files changed

Lines changed: 62 additions & 18 deletions

File tree

packages/code-studio/src/settings/ColumnSpecificSectionContent.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ it('displays an error when the formatting rule is empty', async () => {
138138
expect(screen.getByDisplayValue('test')).toBeInTheDocument();
139139

140140
await user.selectOptions(screen.getByLabelText('Column Type'), 'Decimal');
141-
await user.type(screen.getByLabelText('Formatting Rule'), ' {backspace}');
141+
await user.type(
142+
screen.getByLabelText('Formatting Rule'),
143+
' {Control>}A{/Control}{backspace}'
144+
);
142145
expect(screen.queryByText(/Empty formatting rule\./)).toBeInTheDocument();
143146
});
144147

@@ -215,7 +218,7 @@ it('should change the input value when column type is Integer', async () => {
215218
await user.selectOptions(screen.getByLabelText('Column Type'), 'Integer');
216219
await user.type(
217220
screen.getByLabelText('Formatting Rule'),
218-
`{selectall}${newFormat}`
221+
`{Control>}A{/Control}${newFormat}`
219222
);
220223

221224
expect(screen.getByDisplayValue(newFormat)).toBeInTheDocument();

packages/code-studio/src/settings/ColumnSpecificSectionContent.tsx

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ interface ColumnSpecificSectionContentState {
7171
showTimeZone: boolean;
7272
showTSeparator: boolean;
7373
timeZone: string;
74-
defaultDateTimeFormat: string;
75-
defaultDecimalFormatOptions: FormatOption;
76-
defaultIntegerFormatOptions: FormatOption;
7774
truncateNumbersWithPound: boolean;
7875
timestampAtMenuOpen: Date;
7976
}
@@ -116,9 +113,6 @@ export class ColumnSpecificSectionContent extends PureComponent<
116113

117114
const {
118115
formatter,
119-
defaultDateTimeFormat,
120-
defaultDecimalFormatOptions,
121-
defaultIntegerFormatOptions,
122116
showTimeZone,
123117
showTSeparator,
124118
timeZone,
@@ -141,9 +135,6 @@ export class ColumnSpecificSectionContent extends PureComponent<
141135
showTimeZone,
142136
showTSeparator,
143137
timeZone,
144-
defaultDateTimeFormat,
145-
defaultDecimalFormatOptions,
146-
defaultIntegerFormatOptions,
147138
truncateNumbersWithPound,
148139
timestampAtMenuOpen: new Date(),
149140
};
@@ -221,7 +212,7 @@ export class ColumnSpecificSectionContent extends PureComponent<
221212
let resetKeys = {};
222213
if (key === 'columnType') {
223214
resetKeys = {
224-
format: '',
215+
format: this.makeDefaultFormatterItemByType(value as string),
225216
};
226217
}
227218
const newEntry = {
@@ -284,14 +275,18 @@ export class ColumnSpecificSectionContent extends PureComponent<
284275
commitChanges(): void {
285276
const {
286277
formatSettings,
287-
defaultDateTimeFormat,
288278
showTimeZone,
289279
showTSeparator,
290280
timeZone,
291-
defaultDecimalFormatOptions,
292-
defaultIntegerFormatOptions,
293281
truncateNumbersWithPound,
294282
} = this.state;
283+
284+
const {
285+
defaultDateTimeFormat,
286+
defaultDecimalFormatOptions,
287+
defaultIntegerFormatOptions,
288+
} = this.props;
289+
295290
const { dh } = this.props;
296291

297292
const formatter =
@@ -390,6 +385,41 @@ export class ColumnSpecificSectionContent extends PureComponent<
390385
return error;
391386
}
392387

388+
makeDefaultFormatterItemByType(
389+
columnType: string
390+
): TableColumnFormat | string {
391+
switch (TableUtils.getNormalizedType(columnType)) {
392+
case TableUtils.dataType.INT: {
393+
const { defaultIntegerFormatOptions } = this.props;
394+
const { defaultFormatString: defaultIntegerFormatString } =
395+
defaultIntegerFormatOptions;
396+
return IntegerColumnFormatter.makeFormat(
397+
'',
398+
defaultIntegerFormatString ??
399+
IntegerColumnFormatter.DEFAULT_FORMAT_STRING,
400+
IntegerColumnFormatter.TYPE_GLOBAL,
401+
undefined
402+
);
403+
}
404+
405+
case TableUtils.dataType.DECIMAL: {
406+
const { defaultDecimalFormatOptions } = this.props;
407+
const { defaultFormatString: defaultDecimalFormatString } =
408+
defaultDecimalFormatOptions;
409+
return DecimalColumnFormatter.makeFormat(
410+
'',
411+
defaultDecimalFormatString ??
412+
DecimalColumnFormatter.DEFAULT_FORMAT_STRING,
413+
DecimalColumnFormatter.TYPE_GLOBAL,
414+
undefined
415+
);
416+
}
417+
default: {
418+
return '';
419+
}
420+
}
421+
}
422+
393423
renderFormatRule(i: number, rule: FormatterItem): ReactElement {
394424
const columnNameId = `input-${i}-columnName`;
395425
const columnTypeId = `input-${i}-columnType`;
@@ -401,6 +431,7 @@ export class ColumnSpecificSectionContent extends PureComponent<
401431
this.handleFormatRuleChange(i, 'isNewRule', false);
402432
const onTypeChange = (e: ChangeEvent<HTMLSelectElement>): void =>
403433
this.handleFormatRuleChange(i, 'columnType', e.target.value);
434+
404435
const ruleError = this.getRuleError(rule);
405436

406437
return (
@@ -511,6 +542,7 @@ export class ColumnSpecificSectionContent extends PureComponent<
511542
isInvalid: boolean
512543
): ReactElement {
513544
const { showTimeZone, showTSeparator, timeZone } = this.state;
545+
514546
const value = format.formatString ?? '';
515547
return (
516548
<select
@@ -544,6 +576,8 @@ export class ColumnSpecificSectionContent extends PureComponent<
544576
format: Partial<TableColumnFormat>,
545577
isInvalid: boolean
546578
): ReactElement {
579+
const { defaultIntegerFormatOptions } = this.props;
580+
const { defaultFormatString } = defaultIntegerFormatOptions;
547581
const value = format.formatString ?? '';
548582
return (
549583
<input
@@ -552,7 +586,9 @@ export class ColumnSpecificSectionContent extends PureComponent<
552586
})}
553587
data-lpignore
554588
id={formatId}
555-
placeholder={IntegerColumnFormatter.DEFAULT_FORMAT_STRING}
589+
placeholder={
590+
defaultFormatString ?? IntegerColumnFormatter.DEFAULT_FORMAT_STRING
591+
}
556592
type="text"
557593
value={value}
558594
onChange={e => {
@@ -574,6 +610,9 @@ export class ColumnSpecificSectionContent extends PureComponent<
574610
format: Partial<TableColumnFormat>,
575611
isInvalid: boolean
576612
): ReactElement {
613+
const { defaultDecimalFormatOptions } = this.props;
614+
const { defaultFormatString } = defaultDecimalFormatOptions;
615+
577616
const value = format.formatString ?? '';
578617
return (
579618
<input
@@ -582,7 +621,9 @@ export class ColumnSpecificSectionContent extends PureComponent<
582621
})}
583622
data-lpignore
584623
id={formatId}
585-
placeholder={DecimalColumnFormatter.DEFAULT_FORMAT_STRING}
624+
placeholder={
625+
defaultFormatString ?? DecimalColumnFormatter.DEFAULT_FORMAT_STRING
626+
}
586627
type="text"
587628
value={value}
588629
onChange={e => {

packages/code-studio/src/settings/FormattingSectionContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ export class FormattingSectionContent extends PureComponent<
617617
checked={truncateNumbersWithPound ?? null}
618618
onChange={this.handleTruncateNumbersWithPoundChange}
619619
>
620-
Truncate numbers with #
620+
Show truncated numbers as ###
621621
</Checkbox>
622622
</div>
623623
</div>

0 commit comments

Comments
 (0)