Skip to content

Commit

Permalink
apply CommitHandler type changes across existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
heswell committed Aug 16, 2024
1 parent 8c6866e commit ade89ae
Show file tree
Hide file tree
Showing 26 changed files with 201 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { ColumnDescriptor } from "@finos/vuu-table-types";
import {
type CommitHandler,
VuuInput,
VuuTypeaheadInput,
} from "@finos/vuu-ui-controls";
import { VuuInput, VuuTypeaheadInput } from "@finos/vuu-ui-controls";
import { SuggestionProvider, TableSchemaTable } from "@finos/vuu-data-types";
import { CommitHandler } from "@finos/vuu-utils";

export interface DataItemEditControlProps {
column: ColumnDescriptor;
onCommit: CommitHandler;
onCommit: CommitHandler<HTMLInputElement, string | undefined>;
suggestionProvider?: SuggestionProvider;
table?: TableSchemaTable;
}
Expand All @@ -23,6 +20,7 @@ export const getDataItemEditControl = ({
return (
<VuuTypeaheadInput
column={column.name}
onCommit={onCommit}
suggestionProvider={suggestionProvider}
table={table}
/>
Expand Down
4 changes: 3 additions & 1 deletion vuu-ui/packages/vuu-filters/src/filter-bar/FilterBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,16 @@ export const FilterBar = ({
suggestionProvider={suggestionProvider}
tableSchema={tableSchema}
/>
) : (
) : QuickFilterProps ? (
<QuickFilters
{...QuickFilterProps}
availableColumns={columnDescriptors}
onApplyFilter={onApplyFilter}
suggestionProvider={suggestionProvider}
tableSchema={tableSchema}
/>
) : (
"Missing QuickFilterProps"
)}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Ref,
SyntheticEvent,
forwardRef,
useCallback,
useMemo,
useState,
} from "react";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FilterClause, FilterClauseOp } from "@finos/vuu-filter-types";
import { hasOpenOptionList, isDateInput } from "@finos/vuu-utils";
import { hasOpenOptionList } from "@finos/vuu-utils";
import {
KeyboardEvent,
RefCallback,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React, { ChangeEvent, useCallback, useState } from "react";
import React, {
useCallback,
useState,
} from "react";
import cx from "clsx";
import { getLocalTimeZone, DateValue } from "@internationalized/date";
import { toCalendarDate } from "@finos/vuu-utils";
import { CommitHandler, toCalendarDate } from "@finos/vuu-utils";
import { NumericFilterClauseOp } from "@finos/vuu-filter-types";
import { FilterClauseValueEditor } from "../filterClauseTypes";
import { VuuDatePicker } from "@finos/vuu-ui-controls";
Expand All @@ -23,8 +26,8 @@ export const FilterClauseValueEditorDate = (
getInitialState(value),
);

const onSelectedDateChange = useCallback(
(e: ChangeEvent<HTMLInputElement>, selectedDateInputValue?: string) => {
const handleCommit = useCallback<CommitHandler<HTMLElement, number>>(
(e, selectedDateInputValue) => {
console.log("change date");
if (selectedDateInputValue) {
const dateValue = toCalendarDate(new Date(selectedDateInputValue));
Expand All @@ -46,7 +49,7 @@ export const FilterClauseValueEditorDate = (
inputProps={inputProps}
className={cx("vuuFilterClause-DatePicker", className)}
onBlur={onBlur}
onCommit={onSelectedDateChange}
onCommit={handleCommit}
selectedDate={date}
/>
);
Expand Down
11 changes: 6 additions & 5 deletions vuu-ui/packages/vuu-filters/src/inline-filter/InlineFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getDataItemEditControl } from "@finos/vuu-data-react";
import { VirtualColSpan, useHeaderProps } from "@finos/vuu-table";
import { CommitHandler } from "@finos/vuu-ui-controls";
import { queryClosest } from "@finos/vuu-utils";
import { CommitHandler, queryClosest } from "@finos/vuu-utils";
import { useComponentCssInjection } from "@salt-ds/styles";
import { useWindow } from "@salt-ds/window";
import { HTMLAttributes, useCallback } from "react";
Expand Down Expand Up @@ -33,14 +32,16 @@ export const InlineFilter = ({

const { columns, virtualColSpan = 0 } = useHeaderProps();

const onCommit = useCallback<CommitHandler>(
const onCommit = useCallback<
CommitHandler<HTMLInputElement, string | number | undefined>
>(
(evt, value) => {
const field = queryClosest(evt.target, "[data-field]");
if (field) {
if (field && value !== undefined) {
const columnName = field.dataset.field;
const column = columns.find((c) => c.name === columnName);
if (column) {
onChange(column, value);
onChange(column, value.toString());
}
}
},
Expand Down
40 changes: 28 additions & 12 deletions vuu-ui/packages/vuu-filters/src/quick-filters/QuickFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export interface QuickFilterProps
FilterBarProps,
"onApplyFilter" | "suggestionProvider" | "tableSchema"
> {
allowFind?: boolean;
availableColumns: ColumnDescriptor[];
onChangeQuickFilterColumns?: (columns: string[]) => void;
quickFilterColumns?: string[];
quickFilterColumns: string[];
/**
* Render a general 'search' control.
* if true, all columns of type 'string' will be included in the search. Otherwise
Expand All @@ -30,6 +31,7 @@ export interface QuickFilterProps
}

export const QuickFilters = ({
allowFind = true,
availableColumns,
onApplyFilter,
onChangeQuickFilterColumns,
Expand All @@ -52,7 +54,6 @@ export const QuickFilters = ({
onColumnsSelectionChange,
onCommit,
rootRef,
quickFilters,
} = useQuickFilters({
availableColumns,
onApplyFilter,
Expand All @@ -61,13 +62,14 @@ export const QuickFilters = ({
});

const filterColumns = availableColumns.filter(({ name }) =>
quickFilters?.includes(name),
quickFilterColumns?.includes(name),
);

return (
<div className={classBase} ref={rootRef}>
<div className={`${classBase}-filter-container`}>
<FormField data-embedded data-field="find">
const getFilterControls = () => {
const controls = [];
if (allowFind) {
controls.push(
<FormField data-embedded data-field="find" key="find">
<FormFieldLabel>Find</FormFieldLabel>
<VuuInput
inputProps={{
Expand All @@ -77,8 +79,12 @@ export const QuickFilters = ({
startAdornment={searchIcon}
variant="secondary"
/>
</FormField>
{filterColumns?.map((column) => (
</FormField>,
);
}
{
filterColumns?.forEach((column) =>
controls.push(
<FormField key={column.label ?? column.name} data-field={column.name}>
<FormFieldLabel>{column.label ?? column.name}</FormFieldLabel>
{getDataItemEditControl({
Expand All @@ -87,15 +93,25 @@ export const QuickFilters = ({
suggestionProvider,
table: tableSchema?.table,
})}
</FormField>
))}
</FormField>,
),
);
}

return controls;
};

return (
<div className={classBase} ref={rootRef}>
<div className={`${classBase}-filter-container`}>
{getFilterControls()}
</div>
<ColumnPicker
columns={availableColumnNames}
icon="more-horiz"
iconSize={16}
onSelectionChange={onColumnsSelectionChange}
selected={quickFilters}
selected={quickFilterColumns}
/>
</div>
);
Expand Down
32 changes: 18 additions & 14 deletions vuu-ui/packages/vuu-filters/src/quick-filters/useQuickFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import type { DataSourceFilter } from "@finos/vuu-data-types";
import type { Filter } from "@finos/vuu-filter-types";
import type { VuuRowDataItemType } from "@finos/vuu-protocol-types";
import type { ColumnDescriptor } from "@finos/vuu-table-types";
import { CommitHandler, MultiSelectionHandler } from "@finos/vuu-ui-controls";
import { filterAsQuery, queryClosest } from "@finos/vuu-utils";
import { MultiSelectionHandler } from "@finos/vuu-ui-controls";
import { CommitHandler, filterAsQuery, queryClosest } from "@finos/vuu-utils";
import {
ChangeEventHandler,
RefCallback,
useCallback,
useMemo,
useRef,
useState,
} from "react";
import { QuickFilterProps } from "./QuickFilters";

Expand Down Expand Up @@ -90,10 +89,7 @@ export const useQuickFilters = ({
availableColumns,
onApplyFilter,
onChangeQuickFilterColumns,
quickFilterColumns = [],
}: QuickFilterProps) => {
//TODO make controlled
const [quickFilters, setQuickFilters] = useState(quickFilterColumns);
const filters = useRef<QuickFilterValues>({});
const rootRef = useCallback<RefCallback<HTMLDivElement>>((el) => {
if (el) {
Expand All @@ -109,15 +105,25 @@ export const useQuickFilters = ({
[],
);

const handleCommit = useCallback<CommitHandler>(
const handleCommit = useCallback<
CommitHandler<HTMLInputElement, string | number | undefined>
>(
(e, value) => {
if (value.trim() !== "") {
const field = queryClosest(e.target, "[data-field]");
const column = field?.dataset.field;
if (column) {
const field = queryClosest(e.target, "[data-field]");
const column = field?.dataset.field;
if (column) {
if (
value === undefined ||
(typeof value === "string" && value.trim() === "")
) {
if (filters.current[column] === undefined) {
return;
}
delete filters.current[column];
} else if (typeof value === "string" && value.trim() !== "") {
filters.current[column] = value;
onApplyFilter?.(buildFilter(filters.current, availableColumns));
}
onApplyFilter?.(buildFilter(filters.current, availableColumns));
}
},
[availableColumns, onApplyFilter],
Expand All @@ -126,7 +132,6 @@ export const useQuickFilters = ({
const handleColumnsSelectionChange = useCallback<MultiSelectionHandler>(
(evt, newSelected) => {
onChangeQuickFilterColumns?.(newSelected);
setQuickFilters(newSelected);
},
[onChangeQuickFilterColumns],
);
Expand All @@ -141,7 +146,6 @@ export const useQuickFilters = ({
onChange: handleChange,
onColumnsSelectionChange: handleColumnsSelectionChange,
onCommit: handleCommit,
quickFilters,
rootRef,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
moveItem,
subscribedOnly,
useLayoutEffectSkipFirst,
CommitHandler,
} from "@finos/vuu-utils";
import { CommitHandler } from "@finos/vuu-ui-controls/src";
import {
MouseEvent,
SyntheticEvent,
Expand Down Expand Up @@ -203,16 +203,22 @@ export const useTableSettings = ({
[],
);

const handleCommitColumnWidth = useCallback<CommitHandler>((_, value) => {
const columnDefaultWidth = parseInt(value);
if (!isNaN(columnDefaultWidth)) {
setColumnState((state) => ({
...state,
tableConfig: {
...state.tableConfig,
columnDefaultWidth,
},
}));
const handleCommitColumnWidth = useCallback<
CommitHandler<HTMLInputElement, string | undefined>
>((_, value) => {
if (value === undefined) {
console.log(`column width is undefined`);
} else {
const columnDefaultWidth = parseInt(value);
if (!isNaN(columnDefaultWidth)) {
setColumnState((state) => ({
...state,
tableConfig: {
...state.tableConfig,
columnDefaultWidth,
},
}));
}
}
console.log({ value });
}, []);
Expand Down
9 changes: 5 additions & 4 deletions vuu-ui/packages/vuu-table/src/bulk-edit/BulkEditRow.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getDataItemEditControl } from "@finos/vuu-data-react";
import { ColumnDescriptor } from "@finos/vuu-table-types";
import { CommitHandler } from "@finos/vuu-ui-controls";
import { queryClosest } from "@finos/vuu-utils";
import { CommitHandler, queryClosest } from "@finos/vuu-utils";
import { useComponentCssInjection } from "@salt-ds/styles";
import { useWindow } from "@salt-ds/window";
import { HTMLAttributes, useCallback } from "react";
Expand Down Expand Up @@ -31,9 +30,11 @@ export const BulkEditRow = ({ onChange, ...htmlAttributes }: BulkEditProps) => {

const { columns, virtualColSpan = 0 } = useHeaderProps();

const onCommit = useCallback<CommitHandler>(
const onCommit = useCallback<
CommitHandler<HTMLInputElement, string | undefined>
>(
(evt, value) => {
if (String(value).trim() !== "") {
if (value !== undefined && String(value).trim() !== "") {
const field = queryClosest(evt.target, "[data-field]");
if (field) {
const columnName = field.dataset.field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ export const CalendarNavigation = forwardRef<
years,
canNavigateNext,
canNavigatePrevious,
visibleMonth,
selectedMonth,
selectedYear,
isOutsideAllowedMonths,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function useFocusManagement({ date }: { date: DateValue }) {
newDate = startOfWeek(date, getCurrentLocale());
break;
case "End":
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore TODO bug in @internationalized/date
newDate = endOfWeek(date, getCurrentLocale());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ export const VuuDatePicker = ({
};

const handleSelectionChange = useCallback(
(e: SyntheticEvent<HTMLElement>, date: DateValue) => {
(e: SyntheticEvent<Element>, date: DateValue) => {
if (date && !isSameDate(date, selectedDate)) {
onSelectionChange?.(e, date);
setOpen(false);
console.log(`about to commit date ${date.toString()}`);
onCommit?.(e, toEpochMillis(date, localTimeZone));
onCommit?.(
e as SyntheticEvent<HTMLElement>,
toEpochMillis(date, localTimeZone),
);
}
},
[onCommit, onSelectionChange, selectedDate],
Expand Down
Loading

0 comments on commit ade89ae

Please sign in to comment.