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

fix(refinement-list): re-apply focus on facet checkbox after render #6392

Merged
merged 6 commits into from
Oct 21, 2024
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ class RefinementList<TTemplates extends Templates> extends Component<
> {
public static defaultProps = defaultProps;

private listRef = createRef<HTMLUListElement>();
private searchBox = createRef<SearchBox>();

private lastRefinedValue: string | undefined = undefined;

public shouldComponentUpdate(
nextProps: RefinementListPropsWithDefaultProps<TTemplates>
) {
Expand All @@ -122,6 +125,7 @@ class RefinementList<TTemplates extends Templates> extends Component<
}

private refine(facetValueToRefine: string) {
this.lastRefinedValue = facetValueToRefine;
this.props.toggleRefinement(facetValueToRefine);
}

Expand Down Expand Up @@ -270,6 +274,20 @@ class RefinementList<TTemplates extends Templates> extends Component<
}
}

/**
* This sets focus on the last refined input element after a render
* because Preact does not perform it automatically.
* @see https://github.com/preactjs/preact/issues/3242
*/
public componentDidUpdate() {
dhayab marked this conversation as resolved.
Show resolved Hide resolved
this.listRef.current
?.querySelector<HTMLInputElement>(
`input[value="${this.lastRefinedValue}"]`
)
?.focus();
this.lastRefinedValue = undefined;
}

private refineFirstValue() {
const firstValue = this.props.facetValues && this.props.facetValues[0];
if (firstValue) {
Expand Down Expand Up @@ -330,7 +348,7 @@ class RefinementList<TTemplates extends Templates> extends Component<

const facetValues = this.props.facetValues &&
this.props.facetValues.length > 0 && (
<ul className={this.props.cssClasses.list}>
<ul ref={this.listRef} className={this.props.cssClasses.list}>
{this.props.facetValues.map(this._generateFacetItem, this)}
</ul>
);
Expand Down