Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add show_search and show_quick_filters to ui.table #461

Merged
merged 10 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions plugins/ui/src/deephaven/ui/components/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from ..elements import UITable
from ..types import (
CellPressCallback,
ColumnName,
ColumnPressCallback,
QuickFilterExpression,
RowPressCallback,
)

Expand All @@ -18,6 +20,9 @@ def table(
on_cell_double_press: CellPressCallback | None = None,
on_column_press: ColumnPressCallback | None = None,
on_column_double_press: ColumnPressCallback | None = None,
quick_filters: dict[ColumnName, QuickFilterExpression] | None = None,
show_search: bool | None = None,
show_quick_filters: bool | None = None,
) -> UITable:
"""
Customization to how a table is displayed, how it behaves, and listen to UI events.
Expand All @@ -40,6 +45,9 @@ def table(
The first parameter is the column name.
on_column_double_press: The callback function to run when a column is double clicked.
The first parameter is the column name.
show_search: Whether to show the search bar by default.
quick_filters: The quick filters to apply to the table. Dictionary of column name to filter value.
show_quick_filters: Whether to show the quick filter bar by default.
"""
props = locals()
del props["table"]
Expand Down
18 changes: 3 additions & 15 deletions plugins/ui/src/deephaven/ui/elements/UITable.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ def _with_dict_prop(self, key: str, value: dict[str, Any]) -> "UITable":
A new UITable with the passed in prop added to the existing props
"""
logger.debug("_with_dict_prop(%s, %s)", key, value)
existing = self._props.get(key, {})
existing = (
self._props.get(key) or {}
) # Turn missing or explicit None into empty dict
mofojed marked this conversation as resolved.
Show resolved Hide resolved
return UITable(**{**self._props, key: {**existing, **value}}) # type: ignore

def render(self, context: RenderContext) -> dict[str, Any]:
Expand Down Expand Up @@ -477,20 +479,6 @@ def on_row_double_press(self, callback: RowPressCallback) -> "UITable":
)
return self._with_prop("on_row_double_press", callback)

def quick_filter(
self, filter: dict[ColumnName, QuickFilterExpression]
) -> "UITable":
"""
Add a quick filter for the UI to apply to the table.

Args:
filter: The quick filter to apply to the table.

Returns:
A new UITable
"""
return self._with_dict_prop("filters", filter)

def selection_mode(self, mode: SelectionMode) -> "UITable":
"""
Set the selection mode for the table.
Expand Down
23 changes: 15 additions & 8 deletions plugins/ui/src/js/src/elements/UITable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ function UITable({
onColumnDoublePress,
onRowPress,
onRowDoublePress,
canSearch,
filters,
quickFilters,
mofojed marked this conversation as resolved.
Show resolved Hide resolved
sorts,
alwaysFetchColumns,
table: exportedTable,
showSearch: showSearchBar,
showQuickFilters,
}: UITableProps): JSX.Element | null {
const dh = useApi();
const [model, setModel] = useState<IrisGridModel>();
Expand All @@ -47,12 +48,16 @@ function UITable({
}, [columns, utils, sorts]);

const hydratedQuickFilters = useMemo(() => {
if (filters !== undefined && model !== undefined && columns !== undefined) {
log.debug('Hydrating filters', filters);
if (
quickFilters !== undefined &&
model !== undefined &&
columns !== undefined
) {
log.debug('Hydrating filters', quickFilters);

const dehydratedQuickFilters: DehydratedQuickFilter[] = [];

Object.entries(filters).forEach(([columnName, filter]) => {
Object.entries(quickFilters).forEach(([columnName, filter]) => {
const columnIndex = model.getColumnIndexByName(columnName);
if (columnIndex !== undefined) {
dehydratedQuickFilters.push([columnIndex, { text: filter }]);
Expand All @@ -62,7 +67,7 @@ function UITable({
return utils.hydrateQuickFilters(columns, dehydratedQuickFilters);
}
return undefined;
}, [filters, model, columns, utils]);
}, [quickFilters, model, columns, utils]);

// Just load the object on mount
useEffect(() => {
Expand Down Expand Up @@ -114,15 +119,17 @@ function UITable({
() => ({
mouseHandlers,
alwaysFetchColumns,
showSearchBar: canSearch,
showSearchBar,
sorts: hydratedSorts,
quickFilters: hydratedQuickFilters,
isFilterBarShown: showQuickFilters,
settings,
}),
[
mouseHandlers,
alwaysFetchColumns,
canSearch,
showSearchBar,
showQuickFilters,
hydratedSorts,
hydratedQuickFilters,
settings,
Expand Down
5 changes: 3 additions & 2 deletions plugins/ui/src/js/src/elements/UITableUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export interface UITableProps {
onColumnPress?: (columnName: ColumnName) => void;
onColumnDoublePress?: (columnName: ColumnName) => void;
alwaysFetchColumns?: string[];
canSearch?: boolean;
filters?: Record<string, string>;
quickFilters?: Record<string, string>;
sorts?: DehydratedSort[];
showSearch?: boolean;
showQuickFilters?: boolean;
[key: string]: unknown;
}

Expand Down
35 changes: 0 additions & 35 deletions plugins/ui/test/deephaven/ui/test_ui_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,41 +131,6 @@ def test_can_search(self):
},
)

def test_quick_filter(self):
mattrunyon marked this conversation as resolved.
Show resolved Hide resolved
import deephaven.ui as ui

ui_table = ui.table(self.source)

t = ui_table.quick_filter({"X": "X > 1"})

self.expect_render(
t,
{
"table": self.source,
"filters": {"X": "X > 1"},
},
)

t = ui_table.quick_filter({"X": "X > 1"}).quick_filter({"X": "X > 2"})

self.expect_render(
t,
{
"table": self.source,
"filters": {"X": "X > 2"},
},
)

t = ui_table.quick_filter({"X": "X > 1", "Y": "Y < 2"})

self.expect_render(
t,
{
"table": self.source,
"filters": {"X": "X > 1", "Y": "Y < 2"},
},
)

def test_sort(self):
import deephaven.ui as ui
from deephaven import SortDirection
Expand Down
Loading