Skip to content

Commit

Permalink
fix: Formatting Rule Doesn't use default set by user (#1547)
Browse files Browse the repository at this point in the history
* Default Formatting Rule should change depending on the defaults set by
the user
* Changing columnType should fill the input with the default string
  • Loading branch information
Zhou-Ziheng authored Oct 4, 2023
1 parent a273e64 commit ce51229
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ it('displays an error when the formatting rule is empty', async () => {
expect(screen.getByDisplayValue('test')).toBeInTheDocument();

await user.selectOptions(screen.getByLabelText('Column Type'), 'Decimal');
await user.type(screen.getByLabelText('Formatting Rule'), ' {backspace}');
await user.type(
screen.getByLabelText('Formatting Rule'),
' {Control>}A{/Control}{backspace}'
);
expect(screen.queryByText(/Empty formatting rule\./)).toBeInTheDocument();
});

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

expect(screen.getByDisplayValue(newFormat)).toBeInTheDocument();
Expand Down
71 changes: 56 additions & 15 deletions packages/code-studio/src/settings/ColumnSpecificSectionContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ interface ColumnSpecificSectionContentState {
showTimeZone: boolean;
showTSeparator: boolean;
timeZone: string;
defaultDateTimeFormat: string;
defaultDecimalFormatOptions: FormatOption;
defaultIntegerFormatOptions: FormatOption;
truncateNumbersWithPound: boolean;
timestampAtMenuOpen: Date;
}
Expand Down Expand Up @@ -116,9 +113,6 @@ export class ColumnSpecificSectionContent extends PureComponent<

const {
formatter,
defaultDateTimeFormat,
defaultDecimalFormatOptions,
defaultIntegerFormatOptions,
showTimeZone,
showTSeparator,
timeZone,
Expand All @@ -141,9 +135,6 @@ export class ColumnSpecificSectionContent extends PureComponent<
showTimeZone,
showTSeparator,
timeZone,
defaultDateTimeFormat,
defaultDecimalFormatOptions,
defaultIntegerFormatOptions,
truncateNumbersWithPound,
timestampAtMenuOpen: new Date(),
};
Expand Down Expand Up @@ -221,7 +212,7 @@ export class ColumnSpecificSectionContent extends PureComponent<
let resetKeys = {};
if (key === 'columnType') {
resetKeys = {
format: '',
format: this.makeDefaultFormatterItemByType(value as string),
};
}
const newEntry = {
Expand Down Expand Up @@ -284,14 +275,18 @@ export class ColumnSpecificSectionContent extends PureComponent<
commitChanges(): void {
const {
formatSettings,
defaultDateTimeFormat,
showTimeZone,
showTSeparator,
timeZone,
defaultDecimalFormatOptions,
defaultIntegerFormatOptions,
truncateNumbersWithPound,
} = this.state;

const {
defaultDateTimeFormat,
defaultDecimalFormatOptions,
defaultIntegerFormatOptions,
} = this.props;

const { dh } = this.props;

const formatter =
Expand Down Expand Up @@ -390,6 +385,41 @@ export class ColumnSpecificSectionContent extends PureComponent<
return error;
}

makeDefaultFormatterItemByType(
columnType: string
): TableColumnFormat | string {
switch (TableUtils.getNormalizedType(columnType)) {
case TableUtils.dataType.INT: {
const { defaultIntegerFormatOptions } = this.props;
const { defaultFormatString: defaultIntegerFormatString } =
defaultIntegerFormatOptions;
return IntegerColumnFormatter.makeFormat(
'',
defaultIntegerFormatString ??
IntegerColumnFormatter.DEFAULT_FORMAT_STRING,
IntegerColumnFormatter.TYPE_GLOBAL,
undefined
);
}

case TableUtils.dataType.DECIMAL: {
const { defaultDecimalFormatOptions } = this.props;
const { defaultFormatString: defaultDecimalFormatString } =
defaultDecimalFormatOptions;
return DecimalColumnFormatter.makeFormat(
'',
defaultDecimalFormatString ??
DecimalColumnFormatter.DEFAULT_FORMAT_STRING,
DecimalColumnFormatter.TYPE_GLOBAL,
undefined
);
}
default: {
return '';
}
}
}

renderFormatRule(i: number, rule: FormatterItem): ReactElement {
const columnNameId = `input-${i}-columnName`;
const columnTypeId = `input-${i}-columnType`;
Expand All @@ -401,6 +431,7 @@ export class ColumnSpecificSectionContent extends PureComponent<
this.handleFormatRuleChange(i, 'isNewRule', false);
const onTypeChange = (e: ChangeEvent<HTMLSelectElement>): void =>
this.handleFormatRuleChange(i, 'columnType', e.target.value);

const ruleError = this.getRuleError(rule);

return (
Expand Down Expand Up @@ -511,6 +542,7 @@ export class ColumnSpecificSectionContent extends PureComponent<
isInvalid: boolean
): ReactElement {
const { showTimeZone, showTSeparator, timeZone } = this.state;

const value = format.formatString ?? '';
return (
<select
Expand Down Expand Up @@ -544,6 +576,8 @@ export class ColumnSpecificSectionContent extends PureComponent<
format: Partial<TableColumnFormat>,
isInvalid: boolean
): ReactElement {
const { defaultIntegerFormatOptions } = this.props;
const { defaultFormatString } = defaultIntegerFormatOptions;
const value = format.formatString ?? '';
return (
<input
Expand All @@ -552,7 +586,9 @@ export class ColumnSpecificSectionContent extends PureComponent<
})}
data-lpignore
id={formatId}
placeholder={IntegerColumnFormatter.DEFAULT_FORMAT_STRING}
placeholder={
defaultFormatString ?? IntegerColumnFormatter.DEFAULT_FORMAT_STRING
}
type="text"
value={value}
onChange={e => {
Expand All @@ -574,6 +610,9 @@ export class ColumnSpecificSectionContent extends PureComponent<
format: Partial<TableColumnFormat>,
isInvalid: boolean
): ReactElement {
const { defaultDecimalFormatOptions } = this.props;
const { defaultFormatString } = defaultDecimalFormatOptions;

const value = format.formatString ?? '';
return (
<input
Expand All @@ -582,7 +621,9 @@ export class ColumnSpecificSectionContent extends PureComponent<
})}
data-lpignore
id={formatId}
placeholder={DecimalColumnFormatter.DEFAULT_FORMAT_STRING}
placeholder={
defaultFormatString ?? DecimalColumnFormatter.DEFAULT_FORMAT_STRING
}
type="text"
value={value}
onChange={e => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ export class FormattingSectionContent extends PureComponent<
checked={truncateNumbersWithPound ?? null}
onChange={this.handleTruncateNumbersWithPoundChange}
>
Truncate numbers with #
Show truncated numbers as ###
</Checkbox>
</div>
</div>
Expand Down

0 comments on commit ce51229

Please sign in to comment.