Skip to content

Commit

Permalink
Merge pull request #2563 from 10up/feature/2526
Browse files Browse the repository at this point in the history
Displaying results counts for facet options in Instant Results
  • Loading branch information
felipeelia authored Feb 24, 2022
2 parents cbc63bc + 091adae commit beeaca6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions assets/css/instant-results.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import "instant-results/utilities.css";
@import "instant-results/checkbox.css";
@import "instant-results/input.css";
@import "instant-results/modal.css";
@import "instant-results/options-list.css";
Expand Down
7 changes: 7 additions & 0 deletions assets/css/instant-results/checkbox.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.ep-search-checkbox__count::before {
content: "(";
}

.ep-search-checkbox__count::after {
content: ")";
}
3 changes: 2 additions & 1 deletion assets/js/instant-results/components/common/checkbox-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default ({ disabled, label, options, onChange, selected, sortBy }) => {
* @param {Option} option Option.
* @return {WPElement} Render function.
*/
const displayOption = ({ id, label, value }) => {
const displayOption = ({ count, id, label, value }) => {
const children = childOptions[value];

if (!showAll && optionsShown >= optionsLimit) {
Expand All @@ -133,6 +133,7 @@ export default ({ disabled, label, options, onChange, selected, sortBy }) => {
<li className="ep-search-options-list__item" key={value}>
<Checkbox
checked={selected.includes(value)}
count={count}
disabled={disabled}
id={id}
label={label}
Expand Down
7 changes: 4 additions & 3 deletions assets/js/instant-results/components/common/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import { WPElement } from '@wordpress/element';
* Checkbox component.
*
* @param {Option} props Component props.
* @param {string} props.count Checkbox count.
* @param {string} props.id Checkbox ID.
* @param {string} props.label Checkbox label.
*
* @return {WPElement} Component element.
*/
export default ({ id, label, ...props }) => {
export default ({ count, id, label, ...props }) => {
return (
<div className="ep-search-checkbox">
<input className="ep-search-checkbox__input" id={id} type="checkbox" {...props} />
<input className="ep-search-checkbox__input" id={id} type="checkbox" {...props} />{' '}
<label className="ep-search-checkbox__label" htmlFor={id}>
{label}
{label} {count && <span className="ep-search-checkbox__count">{count}</span>}
</label>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ export default ({ defaultIsOpen, label }) => {
* @return {Array} Array of options.
*/
const reduceOptions = useCallback(
(options, { key }, index) => {
(options, { doc_count, key }, index) => {
if (!Object.prototype.hasOwnProperty.call(postTypeLabels, key)) {
return options;
}

options.push({
checked: selectedPostTypes.includes(key),
count: doc_count,
id: `ep-search-post-type-${key}`,
label: postTypeLabels[key].singular,
order: index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ export default ({ defaultIsOpen, label, postTypes, taxonomy }) => {
* @return {Array} Array of options.
*/
const reduceOptions = useCallback(
(options, { key }) => {
(options, { doc_count, key }) => {
const { name, parent, term_id, term_order } = JSON.parse(key);

options.push({
checked: selectedTerms.includes(term_id),
count: doc_count,
id: `ep-search-${taxonomy}-${term_id}`,
label: name,
parent: parent.toString(),
Expand Down

0 comments on commit beeaca6

Please sign in to comment.