Skip to content

Commit 2f503ba

Browse files
authored
fix: Conditional date formatting (#1104)
Conditional formatting for date columns now properly prevents conditions with empty values from being applied.
1 parent 144605a commit 2f503ba

3 files changed

Lines changed: 79 additions & 4 deletions

File tree

packages/iris-grid/src/sidebar/conditional-formatting/ConditionEditor.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
BooleanCondition,
1616
CharCondition,
1717
getLabelForCharCondition,
18+
isDateConditionValid,
19+
getDefaultValueForType,
1820
} from './ConditionalFormattingUtils';
1921

2022
const log = Log.module('ConditionEditor');
@@ -262,7 +264,7 @@ function ConditionEditor(props: ConditionEditorProps): JSX.Element {
262264
if (selectedColumnType !== prevColumnType) {
263265
// Column type changed, reset condition and value fields
264266
setCondition(getDefaultConditionForType(selectedColumnType));
265-
setValue(undefined);
267+
setValue(getDefaultValueForType(selectedColumnType));
266268
setStartValue(undefined);
267269
setEndValue(undefined);
268270
setPrevColumnType(selectedColumnType);
@@ -322,9 +324,7 @@ function ConditionEditor(props: ConditionEditorProps): JSX.Element {
322324
'Unable to create formatting rule. Condition is not selected.'
323325
);
324326
isValid = false;
325-
}
326-
327-
if (
327+
} else if (
328328
TableUtils.isNumberType(column.type) &&
329329
!isNumberConditionValid(
330330
selectedCondition as NumberCondition,
@@ -338,7 +338,20 @@ function ConditionEditor(props: ConditionEditorProps): JSX.Element {
338338
conditionValue
339339
);
340340
isValid = false;
341+
} else if (
342+
TableUtils.isDateType(column.type) &&
343+
!isDateConditionValid(
344+
selectedCondition as DateCondition,
345+
conditionValue
346+
)
347+
) {
348+
log.debug(
349+
'Unable to create formatting rule. Invalid date condition',
350+
conditionValue
351+
);
352+
isValid = false;
341353
}
354+
342355
onChange(
343356
{
344357
condition: selectedCondition,

packages/iris-grid/src/sidebar/conditional-formatting/ConditionalFormattingUtils.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Column } from '@deephaven/jsapi-shim';
22
import IrisGridTestUtils from '../../IrisGridTestUtils';
33
import {
4+
DateCondition,
45
FormatStyleType,
56
FormatterType,
67
FormattingRule,
78
getFormatColumns,
9+
isDateConditionValid,
810
StringCondition,
911
} from './ConditionalFormattingUtils';
1012

@@ -215,3 +217,46 @@ describe('getFormatColumns', () => {
215217
]);
216218
});
217219
});
220+
221+
describe('isDateConditionValid', () => {
222+
const values = {
223+
valid: '2023-02-23T11:46:31.000000000 NY',
224+
invalid: 'blah',
225+
empty: '',
226+
undefined,
227+
};
228+
229+
it.each([DateCondition.IS_NULL, DateCondition.IS_NOT_NULL])(
230+
'should return true for null check conditions: %s',
231+
condition => {
232+
const testValues = [
233+
values.valid,
234+
values.invalid,
235+
values.empty,
236+
values.undefined,
237+
];
238+
239+
testValues.forEach(value => {
240+
expect(isDateConditionValid(condition, value)).toBeTruthy();
241+
});
242+
}
243+
);
244+
245+
it.each([
246+
DateCondition.IS_AFTER,
247+
DateCondition.IS_AFTER_OR_EQUAL,
248+
DateCondition.IS_BEFORE_OR_EQUAL,
249+
DateCondition.IS_BEFORE,
250+
DateCondition.IS_EXACTLY,
251+
DateCondition.IS_NOT_EXACTLY,
252+
])(
253+
'should return false for empty value when condition requires it: %s',
254+
condition => {
255+
const testValues = [values.empty, values.undefined];
256+
257+
testValues.forEach(value => {
258+
expect(isDateConditionValid(condition, value)).toBeFalsy();
259+
});
260+
}
261+
);
262+
});

packages/iris-grid/src/sidebar/conditional-formatting/ConditionalFormattingUtils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,23 @@ export function getFormatColumns(
706706
return result;
707707
}
708708

709+
/**
710+
* Validate that a given date condition + value pair is valid.
711+
* @param condition
712+
* @param value
713+
*/
714+
export function isDateConditionValid(condition: DateCondition, value?: string) {
715+
switch (condition) {
716+
case DateCondition.IS_NULL:
717+
case DateCondition.IS_NOT_NULL:
718+
return true;
719+
720+
default:
721+
// Proper date validation will be addressed by Issue #1108
722+
return value != null && value !== '';
723+
}
724+
}
725+
709726
export function isSupportedColumn({ type }: ModelColumn): boolean {
710727
return (
711728
TableUtils.isNumberType(type) ||

0 commit comments

Comments
 (0)