|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { cx } from './lib/cx'; |
| 4 | +import { ShowMoreButton } from './ShowMoreButton'; |
| 5 | + |
| 6 | +import type { CreateURL } from 'instantsearch.js'; |
| 7 | +import type { MenuItem } from 'instantsearch.js/es/connectors/menu/connectMenu'; |
| 8 | + |
| 9 | +export type MenuProps = React.HTMLAttributes<HTMLDivElement> & { |
| 10 | + items: MenuItem[]; |
| 11 | + classNames?: Partial<MenuCSSClasses>; |
| 12 | + showMore?: boolean; |
| 13 | + canToggleShowMore: boolean; |
| 14 | + onToggleShowMore: () => void; |
| 15 | + isShowingMore: boolean; |
| 16 | + createURL: CreateURL<MenuItem['value']>; |
| 17 | + onRefine: (item: MenuItem) => void; |
| 18 | +}; |
| 19 | + |
| 20 | +export type MenuCSSClasses = { |
| 21 | + /** |
| 22 | + * Class names to apply to the root element |
| 23 | + */ |
| 24 | + root: string; |
| 25 | + /** |
| 26 | + * Class names to apply to the list element |
| 27 | + */ |
| 28 | + list: string; |
| 29 | + /** |
| 30 | + * Class names to apply to each item element |
| 31 | + */ |
| 32 | + item: string; |
| 33 | + /** |
| 34 | + * Class names to apply to each selected item element |
| 35 | + */ |
| 36 | + itemSelected: string; |
| 37 | + /** |
| 38 | + * Class names to apply to each link element |
| 39 | + */ |
| 40 | + link: string; |
| 41 | + /** |
| 42 | + * Class names to apply to each label element |
| 43 | + */ |
| 44 | + label: string; |
| 45 | + /** |
| 46 | + * Class names to apply to each facet count element |
| 47 | + */ |
| 48 | + count: string; |
| 49 | + /** |
| 50 | + * Class names to apply to the "Show more" button |
| 51 | + */ |
| 52 | + showMore: string; |
| 53 | + /** |
| 54 | + * Class names to apply to the "Show more" button if it's disabled |
| 55 | + */ |
| 56 | + showMoreDisabled: string; |
| 57 | +}; |
| 58 | + |
| 59 | +export function Menu({ |
| 60 | + items, |
| 61 | + classNames = {}, |
| 62 | + showMore, |
| 63 | + canToggleShowMore, |
| 64 | + onToggleShowMore, |
| 65 | + isShowingMore, |
| 66 | + createURL, |
| 67 | + onRefine, |
| 68 | + ...props |
| 69 | +}: MenuProps) { |
| 70 | + return ( |
| 71 | + <div |
| 72 | + {...props} |
| 73 | + className={cx('ais-Menu', classNames.root, props.className)} |
| 74 | + > |
| 75 | + <ul className={cx('ais-Menu-list', classNames.list)}> |
| 76 | + {items.map((item) => ( |
| 77 | + <li |
| 78 | + key={item.label} |
| 79 | + className={cx( |
| 80 | + 'ais-Menu-item', |
| 81 | + classNames.item, |
| 82 | + item.isRefined && |
| 83 | + cx('ais-Menu-item--selected', classNames.itemSelected) |
| 84 | + )} |
| 85 | + > |
| 86 | + <a |
| 87 | + className={cx('ais-Menu-link', classNames.link)} |
| 88 | + href={createURL(item.value)} |
| 89 | + onClick={(event) => { |
| 90 | + event.preventDefault(); |
| 91 | + onRefine(item); |
| 92 | + }} |
| 93 | + > |
| 94 | + <span className={cx('ais-Menu-label', classNames.label)}> |
| 95 | + {item.label} |
| 96 | + </span> |
| 97 | + <span className={cx('ais-Menu-count', classNames.count)}> |
| 98 | + {item.count} |
| 99 | + </span> |
| 100 | + </a> |
| 101 | + </li> |
| 102 | + ))} |
| 103 | + </ul> |
| 104 | + {showMore && ( |
| 105 | + <ShowMoreButton |
| 106 | + className={cx( |
| 107 | + 'ais-Menu-showMore', |
| 108 | + classNames.showMore, |
| 109 | + !canToggleShowMore && |
| 110 | + cx('ais-Menu-showMore--disabled', classNames.showMoreDisabled) |
| 111 | + )} |
| 112 | + disabled={!canToggleShowMore} |
| 113 | + onClick={onToggleShowMore} |
| 114 | + isShowingMore={isShowingMore} |
| 115 | + /> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + ); |
| 119 | +} |
0 commit comments