Skip to content

Commit

Permalink
Merge pull request #127 from eea/develop
Browse files Browse the repository at this point in the history
feat: added SingleTermFacet.jsx
  • Loading branch information
tiberiuichim committed Mar 18, 2024
2 parents 9bbb2ac + d499f0e commit d4e5399
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 2 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

### [0.8.0](https://github.com/eea/volto-searchlib/compare/0.7.3...0.8.0) - 12 March 2024
### [0.9.0](https://github.com/eea/volto-searchlib/compare/0.8.0...0.9.0) - 18 March 2024

#### :rocket: New Features

- feat: added SingleTermFacet.jsx [laszlocseh - [`ba61fc9`](https://github.com/eea/volto-searchlib/commit/ba61fc99603fa39b521938ca18572b23027a9811)]

#### :hammer_and_wrench: Others

- Bump version in package.json [Laszlo Cseh - [`db384c6`](https://github.com/eea/volto-searchlib/commit/db384c6523673cb7e45ffb2d43f29dd170cba759)]
### [0.8.0](https://github.com/eea/volto-searchlib/compare/0.7.3...0.8.0) - 13 March 2024

#### :boom: Breaking Change

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eeacms/volto-searchlib",
"version": "0.8.0",
"version": "0.9.0",
"description": "@eeacms/volto-searchlib: Volto add-on",
"main": "src/index.js",
"author": "European Environment Agency: IDM2 A-Team",
Expand Down
159 changes: 159 additions & 0 deletions searchlib/components/Facets/Unconnected/SingleTermFacet.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import React from 'react';
import { Icon } from 'semantic-ui-react';
import cx from 'classnames';
import { Resizable, ToggleSort, Term } from '@eeacms/search/components';
import { useSort } from '@eeacms/search/lib/hocs';
import { useAppConfig } from '@eeacms/search/lib/hocs';

function getFilterValueDisplay(filterValue) {
if (filterValue === undefined || filterValue === null) return '';
if (filterValue.hasOwnProperty('name')) return filterValue.name;
return String(filterValue);
}
const FacetOptions = (props) => {
const { sortedOptions, label, onSelect, onRemove, field } = props;
return (
<div className="sui-multi-checkbox-facet">
{sortedOptions.map((option) => {
const checked = option.selected;
return (
<label
key={`${getFilterValueDisplay(option.value)}`}
htmlFor={`multiterm_facet_${label}${getFilterValueDisplay(
option.value,
)}`}
className="sui-multi-checkbox-facet__option-label"
>
<div className="sui-multi-checkbox-facet__option-input-wrapper">
<input
id={`multiterm_facet_${label}${getFilterValueDisplay(
option.value,
)}`}
type="checkbox"
className="sui-multi-checkbox-facet__checkbox"
checked={checked}
onChange={() => {
sortedOptions.forEach((opt) => {
if (opt.value === option.value) {
onSelect(opt.value);
} else {
onRemove(opt.value);
}
});
}}
/>
<span className="radio-checkmark" />
<span className="sui-multi-checkbox-facet__input-text">
<Term term={option.value} field={field} />
</span>
</div>
<span className="sui-multi-checkbox-facet__option-count">
({option.count.toLocaleString('en')})
</span>
</label>
);
})}
</div>
);
};

const SingleTermFacetViewComponent = (props) => {
const {
className,
label,
onRemove,
onSelect,
options, // options is the actual value
showSearch,
onSearch,
field,
} = props;

const { appConfig } = useAppConfig();
const facetConfig = appConfig.facets.find((f) => f.field === field);
const configSortOn = facetConfig.sortOn || 'count';
const configSortOrder = facetConfig.sortOrder || 'descending';
let defaultSortOrder = {
// each criteria has its own default sort order
count: 'descending',
value: 'ascending',
custom: 'ascending',
};

defaultSortOrder[configSortOn] = configSortOrder;
const { sortedValues: sortedOptions, toggleSort, sorting } = useSort(
options,
['value', 'count'],
{
defaultSortOn: configSortOn,
defaultSortOrder: defaultSortOrder,
},
field,
);

return (
<fieldset className={cx('sui-facet searchlib-multiterm-facet', className)}>
{/*<legend className="sui-facet__title">{label}</legend>*/}

{showSearch && (
<div className="sui-facet-search">
{/* <Icon name="search" size="small" color="blue" /> */}
<input
className="sui-facet-search__text-input"
type="search"
placeholder={'Quick search'}
onChange={(e) => {
onSearch(e.target.value);
}}
/>
</div>
)}

{options.length < 1 && <div>No matching options</div>}

<div className="sui-multi-checkbox-facet facet-term-controls">
<div className="sui-multi-checkbox-facet__option-label">
<span className="sui-multi-checkbox-facet__option-count">
<ToggleSort
label="Alphabetical order"
onToggle={() => toggleSort('value')}
// on={sorting.sortOn === 'value'}
icon={
sorting.sortOrder === 'ascending' &&
sorting.sortOn === 'value' ? (
<Icon name="sort alphabet ascending" />
) : (
<Icon name="sort alphabet descending" />
)
}
/>
<ToggleSort
label="Count"
onToggle={() => toggleSort('count')}
// on={sorting.sortOn === 'count'}
icon={
sorting.sortOrder === 'ascending' &&
sorting.sortOn === 'count' ? (
<Icon name="sort numeric ascending" />
) : (
<Icon name="sort numeric descending" />
)
}
/>
</span>
</div>
</div>
<Resizable>
<FacetOptions
sortedOptions={sortedOptions}
label={label}
onSelect={onSelect}
onRemove={onRemove}
field={field}
/>
</Resizable>
</fieldset>
);
};

export default SingleTermFacetViewComponent;
19 changes: 19 additions & 0 deletions searchlib/lib/facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ export const suiRangeFacet = ({
};
};

export const singleTermFacet = ({
field,
label,
filterType = 'any',
isFilterable = false,
...params
}) => {
return {
...defaults,
field,
factory: 'SingleTermFacet',
label: label || field,
showInFacetsList: true,
filterType,
isFilterable,
...params,
};
};

export const multiTermFacet = ({
field,
label,
Expand Down
7 changes: 7 additions & 0 deletions searchlib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import React from 'react';
import { SearchBox } from '@elastic/react-search-ui';
import SingleTermFacet from '@eeacms/search/components/Facets/Unconnected/SingleTermFacet';
import MultiTermFacet from '@eeacms/search/components/Facets/Unconnected/MultiTermFacet';
import MultiTermListFacet from '@eeacms/search/components/Facets/Unconnected/MultiTermListFacet';
import HistogramFacet from '@eeacms/search/components/Facets/Unconnected/HistogramFacet';
Expand Down Expand Up @@ -112,6 +113,12 @@ const config = {
buildFilter: getBooleanFilter,
getValue: getBooleanFacet,
},
SingleTermFacet: {
component: SingleTermFacet,
buildRequest: buildTermFacetAggregationRequest,
buildFilter: getTermFilter,
getValue: getValueFacet,
},
MultiTermFacet: {
component: MultiTermFacet,
buildRequest: buildTermFacetAggregationRequest,
Expand Down

0 comments on commit d4e5399

Please sign in to comment.