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
Show file tree
Hide file tree
Changes from 4 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ describe('RefinementList', () => {
<label>
<input type="radio" checked="${item.isRefined}" />
${item.value}
</span>
</label>
`,
showMoreText: '',
};
Expand Down
34 changes: 34 additions & 0 deletions tests/common/widgets/refinement-list/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,40 @@ export function createOptionsTests(
]);
});

test('keeps focus on toggled input between re-renders', async () => {
const searchClient = createMockedSearchClient();

await setup({
instantSearchOptions: {
indexName: 'indexName',
searchClient,
},
widgetParams: { attribute: 'brand' },
});

await act(async () => {
await wait(0);
});

expect(document.activeElement).toEqual(document.body);

const initialTargetItem = document.querySelector(
'.ais-RefinementList-checkbox[value="Samsung"]'
)!;

await act(async () => {
userEvent.click(initialTargetItem);
expect(document.activeElement).toEqual(initialTargetItem);
await wait(0);
});

const updatedTargetItem = document.querySelector(
'.ais-RefinementList-checkbox[value="Samsung"]'
)!;

expect(document.activeElement).toEqual(updatedTargetItem);
});

describe('sorting', () => {
test('sorts the items by ascending name', async () => {
const searchClient = createMockedSearchClient();
Expand Down