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

Add clear link to Facet by Meta Range block. #3364

Merged
merged 1 commit into from
Mar 3, 2023
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
Add clear link to Facet by Range block.
  • Loading branch information
JakePT committed Mar 3, 2023
commit 360aac1aefd774f35f7acde97f6b1e28031ef2f3
4 changes: 3 additions & 1 deletion assets/js/blocks/facets/meta-range/components/range-facet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import { __ } from '@wordpress/i18n';
* Range facet component.
*
* @param {object} props Props.
* @param {string} props.clearUrl Clear filter URL.
* @param {number} props.min Minimum value.
* @param {number} props.max Maximum value.
* @param {string} props.prefix Value prefix.
* @param {string} props.suffix Value suffix.
* @param {number[]} props.value Currnet value.
* @returns {WPElement} Component element.
*/
export default ({ min, max, prefix, suffix, value, ...props }) => {
export default ({ clearUrl, min, max, prefix, suffix, value, ...props }) => {
return (
<div className="ep-range-facet">
<div className="ep-range-facet__slider">
Expand All @@ -46,6 +47,7 @@ export default ({ min, max, prefix, suffix, value, ...props }) => {
{suffix}
</div>
<div className="ep-range-facet__action">
{clearUrl ? <a href={clearUrl}>{__('Clear', 'elasticpress')}</a> : null}{' '}
<button type="submit">{__('Filter', 'elasticpress')}</button>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions assets/js/blocks/facets/meta-range/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const App = ({ max, min }) => {
const prefix = useMemo(() => min.dataset.prefix, [min]);
const suffix = useMemo(() => min.dataset.suffix, [min]);

/**
* Clear URL.
*/
const clearUrl = useMemo(() => (min.value !== '' ? min.form.action : null), [min]);

/**
* Handle change.
*
Expand All @@ -72,6 +77,7 @@ const App = ({ max, min }) => {
*/
return (
<RangeFacet
clearUrl={clearUrl}
max={maxAgg}
min={minAgg}
prefix={prefix}
Expand Down
10 changes: 7 additions & 3 deletions includes/classes/Feature/Facets/Types/MetaRange/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function render( $args, $instance ) {

$all_selected_filters = (array) $feature->get_selected();
$selected_filters = $all_selected_filters[ $facet_type->get_filter_type() ] ?? [];

foreach ( $selected_filters as $filter => $values ) {
if ( $this->meta_field !== $filter ) {
continue;
Expand All @@ -76,10 +77,13 @@ public function render( $args, $instance ) {
$selected_max_value = $values['_max'] ?? null;
unset( $all_selected_filters[ $facet_type->get_filter_type() ][ $filter ] );
}
$form_action = wp_parse_url( $feature->build_query_url( $all_selected_filters ) );
wp_parse_str( $form_action['query'] ?? '', $filter_fields );

$form_action = $feature->build_query_url( $all_selected_filters );
$action_url = wp_parse_url( $form_action );

wp_parse_str( $action_url['query'] ?? '', $filter_fields );
?>
<form class="ep-facet-meta-range">
<form action="<?php echo esc_url( $form_action ); ?>" class="ep-facet-meta-range">
<input type="hidden" data-prefix="<?php echo esc_attr( $instance['prefix'] ); ?>" data-suffix="<?php echo esc_attr( $instance['suffix'] ); ?>" name="<?php echo esc_attr( $min_field_name ); ?>" min="<?php echo absint( $min ); ?>" max="<?php echo absint( $max ); ?>" value="<?php echo esc_attr( $selected_min_value ); ?>">
<input type="hidden" name="<?php echo esc_attr( $max_field_name ); ?>" min="<?php echo absint( $min ); ?>" max="<?php echo absint( $max ); ?>" value="<?php echo esc_attr( $selected_max_value ); ?>">

Expand Down