forked from finos/vuu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finos#1073 enable date/time pattern selection through ColumnSettings
- converges date and time column types to one unified date/time type, consistent with how some major programming languages handles datetime. - date/time pattern selection is only available for columns with "date/time" type. - changes DateTimePattern to be an object instead of strings to enable simultaneous selection of both date and time patterns. - also moves DateTimeColumnDescriptor to vuu-table-types package for consistency.
- Loading branch information
1 parent
0de5af5
commit 6f09305
Showing
19 changed files
with
355 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...i/packages/vuu-table-extras/src/column-formatting-settings/DateTimeFormattingSettings.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.vuuDateTimeFormattingSettings { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 4px; | ||
padding-top: 6px; | ||
} |
133 changes: 133 additions & 0 deletions
133
...i/packages/vuu-table-extras/src/column-formatting-settings/DateTimeFormattingSettings.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import React, { SyntheticEvent, useCallback, useMemo, useState } from "react"; | ||
import { Dropdown, SingleSelectionHandler } from "@finos/vuu-ui-controls"; | ||
import { | ||
DateTimePattern, | ||
defaultPatternsByType, | ||
fallbackDateTimePattern, | ||
getTypeFormattingFromColumn, | ||
isNotNullOrUndefined, | ||
supportedDateTimePatterns, | ||
} from "@finos/vuu-utils"; | ||
import { | ||
FormField, | ||
FormFieldLabel, | ||
ToggleButton, | ||
ToggleButtonGroup, | ||
} from "@salt-ds/core"; | ||
import { DateTimeColumnDescriptor } from "@finos/vuu-table-types"; | ||
import { FormattingSettingsProps } from "./types"; | ||
|
||
import "./DateTimeFormattingSettings.css"; | ||
|
||
const classBase = "vuuDateTimeFormattingSettings"; | ||
|
||
export const DateTimeFormattingSettings: React.FC< | ||
FormattingSettingsProps<DateTimeColumnDescriptor> | ||
> = (props) => { | ||
const { column, onChange } = props; | ||
const formatting = getTypeFormattingFromColumn(column); | ||
const { pattern = fallbackDateTimePattern } = formatting; | ||
const toggleValue = useMemo(() => getToggleValue(pattern), [pattern]); | ||
|
||
const [fallbackState, setFallbackState] = useState<Required<DateTimePattern>>( | ||
{ | ||
time: pattern.time ?? defaultPatternsByType.time, | ||
date: pattern.date ?? defaultPatternsByType.date, | ||
} | ||
); | ||
|
||
const onPatternChange = useCallback( | ||
(pattern: DateTimePattern) => onChange({ ...formatting, pattern }), | ||
[onChange, formatting] | ||
); | ||
|
||
const onDropdownChange = useCallback< | ||
<T extends keyof DateTimePattern>( | ||
key: T | ||
) => SingleSelectionHandler<Required<DateTimePattern>[T]> | ||
>( | ||
(key) => (_, p) => { | ||
const updatedPattern = { ...(pattern ?? {}), [key]: p }; | ||
setFallbackState((s) => ({ | ||
time: updatedPattern.time ?? s.time, | ||
date: updatedPattern.date ?? s.date, | ||
})); | ||
onPatternChange(updatedPattern); | ||
}, | ||
[onPatternChange, pattern] | ||
); | ||
|
||
const onToggleChange = useCallback( | ||
(evnt: SyntheticEvent<HTMLButtonElement, Event>) => { | ||
const value = evnt.currentTarget.value as ToggleValue; | ||
switch (value) { | ||
case "time": | ||
return onPatternChange({ | ||
[value]: pattern[value] ?? fallbackState[value], | ||
}); | ||
case "date": | ||
return onPatternChange({ | ||
[value]: pattern[value] ?? fallbackState[value], | ||
}); | ||
case "both": | ||
return onPatternChange({ | ||
time: pattern.time ?? fallbackState.time, | ||
date: pattern.date ?? fallbackState.date, | ||
}); | ||
} | ||
}, | ||
[onPatternChange] | ||
); | ||
|
||
return ( | ||
<div className={classBase}> | ||
<FormField> | ||
<FormFieldLabel>{"Date/Time pattern"}</FormFieldLabel> | ||
<ToggleButtonGroup | ||
className="vuuToggleButtonGroup" | ||
onChange={onToggleChange} | ||
value={toggleValue} | ||
> | ||
{toggleValues.map((v) => ( | ||
<ToggleButton key={v} value={v}> | ||
{v.toUpperCase()} | ||
</ToggleButton> | ||
))} | ||
</ToggleButtonGroup> | ||
</FormField> | ||
|
||
{(["date", "time"] as const) | ||
.filter((v) => !!pattern[v]) | ||
.map((v) => ( | ||
<FormField labelPlacement="left" key={v}> | ||
<FormFieldLabel>{`${labelByType[v]}`}</FormFieldLabel> | ||
<Dropdown<Required<DateTimePattern>[typeof v]> | ||
onSelectionChange={onDropdownChange(v)} | ||
selected={pattern[v]} | ||
source={supportedDateTimePatterns[v]} | ||
width="100%" | ||
/> | ||
</FormField> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
const labelByType: Record<keyof DateTimePattern, string> = { | ||
date: "Date", | ||
time: "Time", | ||
}; | ||
|
||
const toggleValues = ["date", "time", "both"] as const; | ||
|
||
type ToggleValue = (typeof toggleValues)[number]; | ||
|
||
function getToggleValue(pattern: DateTimePattern): ToggleValue { | ||
const { date, time } = pattern; | ||
|
||
return !isNotNullOrUndefined(time) | ||
? "date" | ||
: !isNotNullOrUndefined(date) | ||
? "time" | ||
: "both"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
vuu-ui/packages/vuu-table-extras/src/column-formatting-settings/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./ColumnFormattingPanel"; | ||
export * from "./NumericFormattingSettings"; | ||
export * from "./DateTimeFormattingSettings"; |
6 changes: 6 additions & 0 deletions
6
vuu-ui/packages/vuu-table-extras/src/column-formatting-settings/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ColumnDescriptor, ColumnTypeFormatting } from "@finos/vuu-table-types"; | ||
|
||
export interface FormattingSettingsProps<T extends ColumnDescriptor = ColumnDescriptor> { | ||
column: T; | ||
onChange: (formatting: ColumnTypeFormatting) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.