Skip to content

Commit

Permalink
fix(date-range): refactor date-range widget field (#5223)
Browse files Browse the repository at this point in the history
Signed-off-by: samuel.park <[email protected]>
  • Loading branch information
piggggggggy authored Dec 16, 2024
1 parent 6bdedc0 commit 478ebd5
Show file tree
Hide file tree
Showing 9 changed files with 702 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ import type { PivotOptions } from '@/common/modules/widgets/types/widget-model';
const props = defineProps<TransformDataTableProps<PivotOptions>>();
const emit = defineEmits<{(e: 'update:operator-options', value: PivotOptions): void;
(e: 'update:form-data', value: Omit<PivotOptions, 'dataTableId'>): void;
(e: 'update:data-table-info', value: TransformDataTableInfo): void;
}>();
const emit = defineEmits<{(e: 'update:operator-options', value: PivotOptions): void; }>();
const widgetGenerateStore = useWidgetGenerateStore();
const widgetGenerateState = widgetGenerateStore.state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ export const widgetFieldDefaultValueMap: DefaultValueRegistry = {
dateFormat: {
format: Object.keys(DATE_FORMAT)[0],
},
dateRange: {},
dateRange: {
inherit: true,
options: {
value: 'auto',
},
},
displayAnnotation: undefined,
displaySeriesLabel: undefined,
granularity: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import type { ColorSchemaValue } from '@/common/modules/widgets/_widget-fields/c
import type { ComparisonValue } from '@/common/modules/widgets/_widget-fields/comparison/type';
import type { _CustomTableColumnWidthValue } from '@/common/modules/widgets/_widget-fields/custom-table-column-width/type';
import type { DataFieldOptions, DataFieldValue } from '@/common/modules/widgets/_widget-fields/data-field/type';
import { DAILY_ENABLED_VALUES, MONTHLY_ENABLED_VALUES, YEARLY_ENABLED_VALUES } from '@/common/modules/widgets/_widget-fields/date-range/constant';
import { checkInvalidCustomValue } from '@/common/modules/widgets/_widget-fields/date-range/helper';
import type { DateRangeValue } from '@/common/modules/widgets/_widget-fields/date-range/type';
import type { DisplayAnnotationValue } from '@/common/modules/widgets/_widget-fields/display-annotation/type';
import type { DisplaySeriesLabelValue } from '@/common/modules/widgets/_widget-fields/display-series-label/type';
import type { GranularityValue } from '@/common/modules/widgets/_widget-fields/granularity/type';
Expand All @@ -26,6 +29,17 @@ export interface WidgetValidatorRegistry {
}

export const widgetValidatorRegistry: WidgetValidatorRegistry = {
dateRange: (fieldValue: DateRangeValue, widgetConfig, allValueMap) => {
const _dateRangeType = fieldValue.options.value;
const _granularity = allValueMap.granularity?.value?.granularity;
if (!_dateRangeType || !_granularity) return false;
if (checkInvalidCustomValue(fieldValue, _granularity).invalid) return false;
if (_granularity === 'MONTHLY' && !MONTHLY_ENABLED_VALUES.includes(_dateRangeType)) return false;
if (_granularity === 'DAILY' && !DAILY_ENABLED_VALUES.includes(_dateRangeType)) return false;
if (_granularity === 'YEARLY' && !YEARLY_ENABLED_VALUES.includes(_dateRangeType)) return false;

return true;
},
dataField: (fieldValue: DataFieldValue, widgetConfig) => {
const _fieldsSchema = integrateFieldsSchema(widgetConfig.requiredFieldsSchema, widgetConfig.optionalFieldsSchema);
const dataFieldOptions = _fieldsSchema.dataField?.options as DataFieldOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class WidgetFieldValueManager {

const validator = widgetValidatorRegistry[key];
if (validator) {
const isValid = validator(this.modifiedData[key], this.widgetConfig);
const isValid = validator(this.modifiedData[key], this.widgetConfig, this.modifiedData);
if (!isValid) {
this.validationErrors[key as string] = `Invalid value for field "${key}"`;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type { _XAxisValue } from '@/common/modules/widgets/_widget-fields/x-axis
import type { _YAxisValue } from '@/common/modules/widgets/_widget-fields/y-axis/type';
import type { WidgetConfig } from '@/common/modules/widgets/types/widget-config-type';

export type FieldValueValidator<T extends WidgetFieldValue> = (fieldValue: T, widgetConfig: WidgetConfig) => boolean;
export type FieldValueValidator<T extends WidgetFieldValue> = (fieldValue: T, widgetConfig: WidgetConfig, allValueMap: WidgetFieldValueMap) => boolean;
export type FieldDefaultValueConvertor<T extends keyof WidgetFieldTypeMap> = (widgetConfig: WidgetConfig, dataTable: PublicDataTableModel|PrivateDataTableModel) => WidgetFieldTypeMap[T]['value'];

export interface WidgetFieldValueMap {
Expand Down
Loading

0 comments on commit 478ebd5

Please sign in to comment.