Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying results counts for facet options in Instant Results #2563

Merged
merged 3 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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