Skip to content

Commit

Permalink
fixup! feat(ts): convert hierarchical-menu to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Apr 1, 2021
1 parent 42d24a6 commit 1621d9f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
32 changes: 28 additions & 4 deletions src/components/RefinementList/__tests__/RefinementList-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,20 @@ describe('RefinementList', () => {
label: 'foo',
count: 1,
data: [
{ value: 'bar', label: 'bar', count: 2, isRefined: false },
{ value: 'baz', label: 'baz', count: 3, isRefined: false },
{
value: 'bar',
label: 'bar',
count: 2,
isRefined: false,
data: null,
},
{
value: 'baz',
label: 'baz',
count: 3,
isRefined: false,
data: null,
},
],
isRefined: false,
},
Expand Down Expand Up @@ -382,8 +394,20 @@ describe('RefinementList', () => {
label: 'foo',
count: 1,
data: [
{ value: 'bar', label: 'bar', count: 2, isRefined: false },
{ value: 'baz', label: 'baz', count: 3, isRefined: false },
{
value: 'bar',
label: 'bar',
count: 2,
isRefined: false,
data: null,
},
{
value: 'baz',
label: 'baz',
count: 3,
isRefined: false,
data: null,
},
],
isRefined: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica
createInitOptions({ helper })
).hierarchicalMenu
).toEqual({
anotherCategory: expect.any(Object),
anotherCategory: renderState.hierarchicalMenu.anotherCategory,
category: {
items: [],
refine: expect.any(Function),
Expand Down
14 changes: 8 additions & 6 deletions src/connectors/hierarchical-menu/connectHierarchicalMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
TransformItems,
RenderOptions,
Widget,
SortBy,
} from '../../types';

const withUsage = createDocumentationMessageGenerator({
Expand Down Expand Up @@ -41,7 +42,7 @@ export type HierarchicalMenuItem = {
/**
* n+1 level of items, same structure HierarchicalMenuItem
*/
data?: HierarchicalMenuItem[] | null;
data: HierarchicalMenuItem[] | null;
};

export type HierarchicalMenuConnectorParams = {
Expand Down Expand Up @@ -78,9 +79,7 @@ export type HierarchicalMenuConnectorParams = {
* How to sort refinements. Possible values: `count|isRefined|name:asc|name:desc`.
* You can also use a sort function that behaves like the standard Javascript [compareFunction](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Syntax).
*/
sortBy?:
| Array<'count' | 'isRefined' | 'name:asc' | 'name:desc'>
| (() => void);
sortBy?: SortBy<HierarchicalMenuItem>;
/**
* Function to transform the items passed to the templates.
*/
Expand All @@ -100,6 +99,10 @@ export type HierarchicalMenuRendererOptions = {
* Sets the path of the hierarchical filter and triggers a new search.
*/
refine: (value: string) => void;
/**
* Indicates if search state can be refined.
*/
canRefine: boolean;
/**
* True if the menu is displaying all the menu items.
*/
Expand Down Expand Up @@ -209,11 +212,10 @@ const connectHierarchicalMenu: ConnectHierarchicalMenu = function connectHierarc
...subValue,
label,
value,
data: null,
};
if (Array.isArray(data)) {
item.data = _prepareFacetValues(data);
} else if (data !== undefined) {
item.data = data;
}
return item;
});
Expand Down
42 changes: 22 additions & 20 deletions src/widgets/hierarchical-menu/hierarchical-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
Template,
WidgetFactory,
RendererOptions,
SortBy,
} from '../../types';
import { component } from '../../lib/suit';

Expand All @@ -32,11 +33,16 @@ type HierarchicalMenuTemplates = {
/**
* Item template, provided with `name`, `count`, `isRefined`, `url` data properties.
*/
item: Template;
item: Template<{
name: string;
count: number;
isRefined: boolean;
url: string;
}>;
/**
* Template used for the show more text, provided with `isShowingMore` data property.
*/
showMoreText: Template;
showMoreText: Template<{ isShowingMore: boolean }>;
};

export type HierarchicalMenuCSSClasses = {
Expand Down Expand Up @@ -115,21 +121,6 @@ export type HierarchicalMenuWidgetParams = {
rootPath?: string;
/**
* Show the siblings of the selected parent level of the current refined value.
*/
showParentLevel?: boolean;
/**
* Max number of values to display.
*/
limit?: number;
/**
* Whether to display the "show more" button.
*/
showMore?: boolean;
/**
* Max number of values to display when showing more.
* does not impact the root level.
*
* The hierarchical menu is able to show or hide the siblings with `showParentLevel`.
*
* With `showParentLevel` set to `true` (default):
* - Parent lvl0
Expand All @@ -149,14 +140,25 @@ export type HierarchicalMenuWidgetParams = {
* - Parent lvl0
* - Parent lvl0
*/
showParentLevel?: boolean;
/**
* Max number of values to display.
*/
limit?: number;
/**
* Whether to display the "show more" button.
*/
showMore?: boolean;
/**
* Max number of values to display when showing more.
* does not impact the root level.
*/
showMoreLimit?: number;
/**
* How to sort refinements. Possible values: `count|isRefined|name:asc|name:desc`.
* You can also use a sort function that behaves like the standard Javascript [compareFunction](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Syntax).
*/
sortBy?:
| Array<'count' | 'isRefined' | 'name:asc' | 'name:desc'>
| (() => void);
sortBy?: SortBy<HierarchicalMenuItem>;
/**
* Function to transform the items passed to the templates.
*/
Expand Down

0 comments on commit 1621d9f

Please sign in to comment.