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-bar4.0.0@trackunit/react-filter-components4.0.0@trackunit/filters-asset-filter-definitions5.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:
| Removed | Use instead |
|---|---|
DefaultCheckboxFilter | useCheckboxFilterView |
DefaultRadioFilter | useRadioFilterView |
DefaultMinMaxFilter | useMinMaxFilterView |
DefaultDateRangeFilter | useDateRangeFilterView |
HierarchicalCheckboxFilter | useHierarchicalCheckboxFilterView |
There are also new views for common cases:
useStringRadioFilterViewanduseBooleanRadioFilterView: single select.useFreeTextFilterViewanduseFreeTextListFilterView: 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,allowShowFiltersDirectlyanduseFiltersMenu().showDirectlyFiltersno 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 withfilterBar.filterBarConfig.setters. ReplaceallowShowFiltersDirectly={false}on aFilterBarwithhiddenFilters={["search"]}. componentis removed. Filters useview.Default*Filter,HierarchicalCheckboxFilter,FilterHeader,DefaultFilterPropsandHierarchicalFilterPropsare removed. For extra sticky content, use theheaderslot.bodyis required. It cannot benull,undefinedor a boolean. Return content, such as an empty state.hideHeaderis removed fromFilterViewProps. The title always shows.- Date range
fromandtoare replaced bydisabledDays: { before, after }. - Some exports have new names:
DynamicFilterListis nowFilterOptionList.Filter,FilterPropsandFilterVisualStyleare nowFilterPopover,FilterPopoverPropsandFilterPopoverVisualStyle.CheckBoxFilterItemis nowCheckboxFilterItem.
- Asset filter views are hooks. In
@trackunit/filters-asset-filter-definitions,BrandFilterViewis nowuseBrandFilterView. The same change applies to all other views. Use them as aview, or call them from your own view hook.SearchFilterInlineis removed. UseuseSearchFilter. - 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, passperiodstousePeriodFilter. - Tests that call
definition.component(props)must change. Render the filter withFilterComponentorFiltersRendererinstead.
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:
- Run the codemods below. The first step also updates the versions.
- Fix what the codemods list. Your build fails until you do.
- Check any filter that you built in a variable and spread later. A leftover
showDirectly: truecan pass type checking, and the filter then opens as a popover.
Codemods
Six codemods are included. They run in this order:
| Codemod | What it does |
|---|---|
v4-0-0-remove-show-directly | Removes showDirectly and allowShowFiltersDirectly. Changes search typing and rendering. |
v4-0-0-component-to-view-shape | Changes component to view hooks. |
v4-0-0-dynamic-filter-list-rename-to-filter-option-list | Renames DynamicFilterList. |
v4-0-0-date-range-bounds-to-disabled-days | Changes from and to to disabledDays. Those days are now disabled in the calendar. |
v4-0-0-filter-rename-to-filter-popover | Renames Filter, FilterProps and FilterVisualStyle. |
v4-0-0-checkbox-filter-item-rename | Renames CheckBoxFilterItem. |
Run them from your project root:
- Update the
@trackunit/*versions and collect the migrations:nx g @trackunit/migrations:migrate
- 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.