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

Product Block Editor: autofocus attributes combobox component #47378

Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update

Product Block Editor: autofocus attributes combobox component
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const AttributesComboboxControl: React.FC<
items = [],
instanceNumber = 0,
isLoading = false,
autoFocus = false,
onChange,
} ) => {
const [ createNewAttributeOption, updateCreateNewAttributeOption ] =
Expand Down Expand Up @@ -178,6 +179,23 @@ const AttributesComboboxControl: React.FC<
}
}, [ instanceNumber ] );

// Autofocus the input.
useEffect( () => {
if ( ! comboRef?.current ) {
return;
}

const inputElement = comboRef.current.querySelector(
'input.components-combobox-control__input'
) as HTMLInputElement;

if ( ! inputElement || ! autoFocus ) {
return;
}

inputElement.focus();
}, [ autoFocus ] );

if ( ! help ) {
help = ! attributeSelected ? (
<div className="woocommerce-attributes-combobox-help">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type AttributesComboboxControlComponent = {
label?: string;
help?: JSX.Element | string | null;
isLoading: boolean;
autoFocus?: boolean;
placeholder?: string;
disabled?: boolean;
instanceNumber?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export const AttributeTableRow: React.FC< AttributeTableRowProps > = ( {
current={ attribute }
items={ filteredAttributes }
isLoading={ isLoadingAttributes }
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={ true }
onChange={ ( nextAttribute ) => {
if ( nextAttribute.id === attributeId ) {
return;
Expand Down
Loading