Skip to content

Commit

Permalink
fix(currentRefinements): implement noRefinementRoot modifier class (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab committed Sep 22, 2022
1 parent d48ed26 commit cb66830
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/components/CurrentRefinements/CurrentRefinements.tsx
@@ -1,6 +1,7 @@
/** @jsx h */

import { h } from 'preact';
import cx from 'classnames';
import { isSpecialClick, capitalize } from '../../lib/utils';
import type {
CurrentRefinementsConnectorParamsItem,
Expand All @@ -15,6 +16,7 @@ export type CurrentRefinementsComponentCSSClasses =
export type CurrentRefinementsProps = {
items: CurrentRefinementsConnectorParamsItem[];
cssClasses: CurrentRefinementsComponentCSSClasses;
canRefine: boolean;
};

const createItemKey = ({
Expand All @@ -37,8 +39,16 @@ const handleClick = (callback: () => void) => (event: any) => {
callback();
};

const CurrentRefinements = ({ items, cssClasses }: CurrentRefinementsProps) => (
<div className={cssClasses.root}>
const CurrentRefinements = ({
items,
cssClasses,
canRefine,
}: CurrentRefinementsProps) => (
<div
className={cx(cssClasses.root, {
[cssClasses.noRefinementRoot]: !canRefine,
})}
>
<ul className={cssClasses.list}>
{items.map((item, index) => (
<li
Expand Down
Expand Up @@ -10,6 +10,7 @@ import CurrentRefinements from '../CurrentRefinements';
describe('CurrentRefinements', () => {
const cssClasses = {
root: 'root',
noRefinementRoot: 'noRefinementRoot',
list: 'list',
item: 'item',
label: 'label',
Expand All @@ -21,6 +22,7 @@ describe('CurrentRefinements', () => {
it('renders', () => {
const props = {
cssClasses,
canRefine: true,
items: [
{
indexName: 'indexName',
Expand Down Expand Up @@ -122,10 +124,25 @@ describe('CurrentRefinements', () => {
expect(container).toMatchSnapshot();
});

it('renders without items', () => {
const props = {
cssClasses,
canRefine: false,
items: [],
};

const { container } = render(<CurrentRefinements {...props} />);
const root = container.querySelector('.root')!;

expect(root.classList).toContain('noRefinementRoot');
expect(container).toMatchSnapshot();
});

describe('options.refinements', () => {
it('can be used with a facet', () => {
const props = {
cssClasses,
canRefine: true,
items: [
{
indexName: 'indexName',
Expand All @@ -152,6 +169,7 @@ describe('CurrentRefinements', () => {
it('can be used with an exclude', () => {
const props = {
cssClasses,
canRefine: true,
items: [
{
indexName: 'indexName',
Expand Down Expand Up @@ -179,6 +197,7 @@ describe('CurrentRefinements', () => {
it('can be used with a disjunctive facet', () => {
const props = {
cssClasses,
canRefine: true,
items: [
{
indexName: 'indexName',
Expand All @@ -205,6 +224,7 @@ describe('CurrentRefinements', () => {
it('can be used with a hierarchical facet', () => {
const props = {
cssClasses,
canRefine: true,
items: [
{
indexName: 'indexName',
Expand All @@ -231,6 +251,7 @@ describe('CurrentRefinements', () => {
it('can be used with numeric filters', () => {
const props = {
cssClasses,
canRefine: true,
items: [
{
indexName: 'indexName',
Expand Down Expand Up @@ -288,6 +309,7 @@ describe('CurrentRefinements', () => {
it('can be used with a tag', () => {
const props = {
cssClasses,
canRefine: true,
items: [
{
indexName: 'indexName',
Expand All @@ -314,6 +336,7 @@ describe('CurrentRefinements', () => {
it('can be used with a query', () => {
const props = {
cssClasses,
canRefine: true,
items: [
{
indexName: 'indexName',
Expand Down
Expand Up @@ -479,3 +479,15 @@ exports[`CurrentRefinements renders 1`] = `
</div>
</div>
`;

exports[`CurrentRefinements renders without items 1`] = `
<div>
<div
class="root noRefinementRoot"
>
<ul
class="list"
/>
</div>
</div>
`;
Expand Up @@ -2,13 +2,15 @@

exports[`currentRefinements() render() DOM output renders correctly 1`] = `
{
"canRefine": true,
"cssClasses": {
"category": "ais-CurrentRefinements-category category",
"categoryLabel": "ais-CurrentRefinements-categoryLabel categoryLabel",
"delete": "ais-CurrentRefinements-delete delete",
"item": "ais-CurrentRefinements-item item",
"label": "ais-CurrentRefinements-label label",
"list": "ais-CurrentRefinements-list list",
"noRefinementRoot": "ais-CurrentRefinements--noRefinement",
"root": "ais-CurrentRefinements root",
},
"items": [
Expand Down Expand Up @@ -144,13 +146,15 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = `

exports[`currentRefinements() render() options.container should render with a HTMLElement container 1`] = `
{
"canRefine": false,
"cssClasses": {
"category": "ais-CurrentRefinements-category",
"categoryLabel": "ais-CurrentRefinements-categoryLabel",
"delete": "ais-CurrentRefinements-delete",
"item": "ais-CurrentRefinements-item",
"label": "ais-CurrentRefinements-label",
"list": "ais-CurrentRefinements-list",
"noRefinementRoot": "ais-CurrentRefinements--noRefinement",
"root": "ais-CurrentRefinements",
},
"items": [],
Expand All @@ -159,13 +163,15 @@ exports[`currentRefinements() render() options.container should render with a HT

exports[`currentRefinements() render() should render twice <CurrentRefinements ... /> 1`] = `
{
"canRefine": true,
"cssClasses": {
"category": "ais-CurrentRefinements-category",
"categoryLabel": "ais-CurrentRefinements-categoryLabel",
"delete": "ais-CurrentRefinements-delete",
"item": "ais-CurrentRefinements-item",
"label": "ais-CurrentRefinements-label",
"list": "ais-CurrentRefinements-list",
"noRefinementRoot": "ais-CurrentRefinements--noRefinement",
"root": "ais-CurrentRefinements",
},
"items": [
Expand All @@ -191,13 +197,15 @@ exports[`currentRefinements() render() should render twice <CurrentRefinements .

exports[`currentRefinements() render() should render twice <CurrentRefinements ... /> 2`] = `
{
"canRefine": true,
"cssClasses": {
"category": "ais-CurrentRefinements-category",
"categoryLabel": "ais-CurrentRefinements-categoryLabel",
"delete": "ais-CurrentRefinements-delete",
"item": "ais-CurrentRefinements-item",
"label": "ais-CurrentRefinements-label",
"list": "ais-CurrentRefinements-list",
"noRefinementRoot": "ais-CurrentRefinements--noRefinement",
"root": "ais-CurrentRefinements",
},
"items": [
Expand Down
19 changes: 16 additions & 3 deletions src/widgets/current-refinements/current-refinements.tsx
Expand Up @@ -22,6 +22,11 @@ export type CurrentRefinementsCSSClasses = Partial<{
*/
root: string | string[];

/**
* CSS class to add to the root element when no refinements.
*/
noRefinementRoot: string | string[];

/**
* CSS class to add to the list element.
*/
Expand Down Expand Up @@ -73,7 +78,7 @@ const suit = component('CurrentRefinements');
const renderer: Renderer<
CurrentRefinementsRenderState,
Partial<CurrentRefinementsWidgetParams>
> = ({ items, widgetParams }, isFirstRender) => {
> = ({ items, widgetParams, canRefine }, isFirstRender) => {
if (isFirstRender) {
return;
}
Expand All @@ -84,7 +89,11 @@ const renderer: Renderer<
};

render(
<CurrentRefinements cssClasses={cssClasses} items={items} />,
<CurrentRefinements
cssClasses={cssClasses}
items={items}
canRefine={canRefine}
/>,
container
);
};
Expand Down Expand Up @@ -112,8 +121,12 @@ const currentRefinements: CurrentRefinementsWidget =
}

const containerNode = getContainerNode(container);
const cssClasses = {
const cssClasses: CurrentRefinementsCSSClasses = {
root: cx(suit(), userCssClasses.root),
noRefinementRoot: cx(
suit({ modifierName: 'noRefinement' }),
userCssClasses.noRefinementRoot
),
list: cx(suit({ descendantName: 'list' }), userCssClasses.list),
item: cx(suit({ descendantName: 'item' }), userCssClasses.item),
label: cx(suit({ descendantName: 'label' }), userCssClasses.label),
Expand Down

0 comments on commit cb66830

Please sign in to comment.