Skip to main content

Breaking: filters describe their popover with a view, and search is its own filter type

A filter now describes what its popover contains. The filter bar builds the popover. Title, reset button, spinner, scrolling and footer are the same for every filter. Free-text search is now a separate filter type. The filter bar shows it as a search field next to the Filters button.

This is a major release with no deprecation period:

  • @trackunit/filters-filter-bar 4.0.0
  • @trackunit/react-filter-components 4.0.0
  • @trackunit/filters-asset-filter-definitions 5.0.0

Codemods change most of your code for you. They list what they cannot change. Code that you did not change fails to compile. It does not render wrong.

For a full explanation of the API, see the Filter Bar docs in Storybook. For examples that you can run, see the defineFilter stories.

New: filters have a view​

A filter has a view instead of a component. A view is a hook. It returns the parts of the popover that are specific to this filter:

{ header?, body, footer?, loading?, resettable?, fitContent? }

Only body is required. The filter bar adds the title and the reset button.

-const StatusFilterView = (props: FilterViewProps<Array<string>>) => {
+const useStatusFilterView = (props: FilterViewProps<Array<string>, StringArrayFilterDefinition>): FilterViewSlots => {
const { options, loading } = useStatusOptions(props.filterState);
- return <DefaultCheckboxFilter {...props} loading={loading} options={options} />;
+ return useCheckboxFilterView({ ...props, loading, options });
};

const statusFilter = {
filterKey: "status",
type: "stringArray",
group: "STATUS",
title: "Status",
- component: props => <StatusFilterView {...props} />,
+ view: useStatusFilterView,
} satisfies StringArrayFilterDefinition;

Each old Default*Filter component has a matching view hook:

RemovedUse instead
DefaultCheckboxFilteruseCheckboxFilterView
DefaultRadioFilteruseRadioFilterView
DefaultMinMaxFilteruseMinMaxFilterView
DefaultDateRangeFilteruseDateRangeFilterView
HierarchicalCheckboxFilteruseHierarchicalCheckboxFilterView

There are also new views for common cases:

  • useStringRadioFilterView and useBooleanRadioFilterView: single select.
  • useFreeTextFilterView and useFreeTextListFilterView: free text, or a list of typed values such as IDs.

A filter with no UI of its own, such as an area that is set from the map, uses kind: "readOnly". The popover shows its title, value and a reset button.

New: search is a filter type​

useSearchFilter returns a filter with type: "search". The filter bar shows it next to the Filters button. It shares the bar's state, URL and storage, and you still read it from values.search.

const search = useSearchFilter({ localStorageKey: "assetList" });
const filterBar = useFilterBar({ name: "assetList", filterBarDefinition: { search, brand } });
return <FilterBar {...filterBar} />;

Search is not an applied filter. The menu, the filter pills, the filter count and the Reset button all ignore it. resetFiltersToInitialState() does clear it.

If you build your own bar, show the search field with FilterBarSearch. To remove the field from a FilterBar, use hiddenFilters={["search"]}.

New: no useMemo for definitions​

The filter bar keeps its state by filterKey. You can build a definition again on every render. Values do not reset. In effects and memos, depend on filterBar.filterBarConfig, not on the whole filterBar. Your existing useMemo calls still work.

Breaking changes​

  • Inline filters are removed. showDirectly, allowShowFiltersDirectly and useFiltersMenu().showDirectlyFilters no longer exist. Every filter except search opens as a popover. If you need a control next to the bar, render it yourself. Write to the bar with filterBar.filterBarConfig.setters. Replace allowShowFiltersDirectly={false} on a FilterBar with hiddenFilters={["search"]}.
  • component is removed. Filters use view. Default*Filter, HierarchicalCheckboxFilter, FilterHeader, DefaultFilterProps and HierarchicalFilterProps are removed. For extra sticky content, use the header slot.
  • body is required. It cannot be null, undefined or a boolean. Return content, such as an empty state.
  • hideHeader is removed from FilterViewProps. The title always shows.
  • Date range from and to are replaced by disabledDays: { before, after }.
  • Some exports have new names:
    • DynamicFilterList is now FilterOptionList.
    • Filter, FilterProps and FilterVisualStyle are now FilterPopover, FilterPopoverProps and FilterPopoverVisualStyle.
    • CheckBoxFilterItem is now CheckboxFilterItem.
  • Asset filter views are hooks. In @trackunit/filters-asset-filter-definitions, BrandFilterView is now useBrandFilterView. The same change applies to all other views. Use them as a view, or call them from your own view hook. SearchFilterInline is removed. Use useSearchFilter.
  • The period filter has one list of options. Every filter bar shows the last 24 hours, 7 days and 30 days. An empty value no longer falls back to LAST_24_HOURS. To use another list, pass periods to usePeriodFilter.
  • Tests that call definition.component(props) must change. Render the filter with FilterComponent or FiltersRenderer instead.

Changes in behavior​

  • A filter that loads shows a spinner in the body. When it loads again, a small spinner shows next to reset. The options stay on screen.
  • A filter with no options shows "No results".
  • Checkbox, radio and hierarchical views show a search field when they have 5 or more options.
  • Those views also have zeroCountOptions: "show" | "hide". A selected option stays in the list, so you can clear it.
  • "Select all" skips the "no value" option.
  • The date range picks days as a draft. The Apply button saves them.
  • Boolean filters keep their value after a reload and in shared links. This includes "Not set" for custom fields.
  • If one stored value is no longer valid, only that filter resets. Before, the whole bar reset.
  • Empty text is no longer saved as a recent search.

If you have not updated yet​

Older versions keep working. A range such as ^3.x does not install 4.0.0, so your app stays on the old API until you change the version.

When you are ready:

  1. Run the codemods below. The first step also updates the versions.
  2. Fix what the codemods list. Your build fails until you do.
  3. Check any filter that you built in a variable and spread later. A leftover showDirectly: true can pass type checking, and the filter then opens as a popover.

Codemods​

Six codemods are included. They run in this order:

CodemodWhat it does
v4-0-0-remove-show-directlyRemoves showDirectly and allowShowFiltersDirectly. Changes search typing and rendering.
v4-0-0-component-to-view-shapeChanges component to view hooks.
v4-0-0-dynamic-filter-list-rename-to-filter-option-listRenames DynamicFilterList.
v4-0-0-date-range-bounds-to-disabled-daysChanges from and to to disabledDays. Those days are now disabled in the calendar.
v4-0-0-filter-rename-to-filter-popoverRenames Filter, FilterProps and FilterVisualStyle.
v4-0-0-checkbox-filter-item-renameRenames CheckBoxFilterItem.

Run them from your project root:

  1. Update the @trackunit/* versions and collect the migrations:
    • nx g @trackunit/migrations:migrate
  2. Read trackunit-migrations.json, then run them:
    • nx g @trackunit/migrations:run-migrations

The codemods only change code that they can convert safely. For each file they skip, the output shows the file, the line, the filter key, a reason code and the fix. Use this list as your to-do list.

Skipped filters do not compile until you move them to view by hand. The codemods are then marked as done, so do not run them again. Fix the skipped files by hand.

For the design, see DS-180.