Product Editor: Fix select on focus for currency and number fields (#41171)

This commit is contained in:
Matt Sherman 2023-11-02 11:11:19 -04:00 committed by GitHub
commit bae3a34501
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 25 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix selection in currency and number fields to only select if field still has focus.

View File

@ -8,7 +8,7 @@ import { useContext } from '@wordpress/element';
* Internal dependencies
*/
import { useProductHelper } from './use-product-helper';
import { formatCurrencyDisplayValue } from '../utils';
import { deferSelectInFocus, formatCurrencyDisplayValue } from '../utils';
export type CurrencyInputProps = {
prefix: string;
@ -51,18 +51,7 @@ export const useCurrencyInputProps = ( {
return sanitizePrice( String( val ) );
},
onFocus( event: React.FocusEvent< HTMLInputElement > ) {
// In some browsers like safari .select() function inside
// the onFocus event doesn't work as expected because it
// conflicts with onClick the first time user click the
// input. Using setTimeout defers the text selection and
// avoid the unexpected behaviour.
setTimeout(
function deferSelection( element: HTMLInputElement ) {
element.select();
},
0,
event.currentTarget
);
deferSelectInFocus( event.currentTarget );
if ( onFocus ) {
onFocus( event );
}

View File

@ -2,6 +2,7 @@
* Internal dependencies
*/
import { useProductHelper } from './use-product-helper';
import { deferSelectInFocus } from '../utils';
export type NumberInputProps = {
value: string;
@ -28,18 +29,7 @@ export const useNumberInputProps = ( {
const numberInputProps: NumberInputProps = {
value: formatNumber( value ),
onFocus( event: React.FocusEvent< HTMLInputElement > ) {
// In some browsers like safari .select() function inside
// the onFocus event doesn't work as expected because it
// conflicts with onClick the first time user click the
// input. Using setTimeout defers the text selection and
// avoid the unexpected behaviour.
setTimeout(
function deferSelection( element: HTMLInputElement ) {
element.select();
},
0,
event.currentTarget
);
deferSelectInFocus( event.currentTarget );
if ( onFocus ) {
onFocus( event );
}

View File

@ -0,0 +1,17 @@
export function deferSelectInFocus( element: HTMLInputElement ) {
// In some browsers like safari .select() function inside
// the onFocus event doesn't work as expected because it
// conflicts with onClick the first time user click the
// input. Using setTimeout defers the text selection and
// avoid the unexpected behaviour.
setTimeout(
function deferSelection( originalElement: HTMLInputElement ) {
if ( element.ownerDocument.activeElement === originalElement ) {
// We still have focus, so select the content.
originalElement.select();
}
},
0,
element
);
}

View File

@ -2,6 +2,7 @@
* Internal dependencies
*/
import { AUTO_DRAFT_NAME } from './constants';
import { deferSelectInFocus } from './defer-select-in-focus';
import { formatCurrencyDisplayValue } from './format-currency-display-value';
import { getCheckboxTracks } from './get-checkbox-tracks';
import { getCurrencySymbolProps } from './get-currency-symbol-props';
@ -30,6 +31,7 @@ export * from './sift';
export {
AUTO_DRAFT_NAME,
deferSelectInFocus,
formatCurrencyDisplayValue,
getCheckboxTracks,
getCurrencySymbolProps,