Only select on focus in currency and number fields if still has focus

This commit is contained in:
Matt Sherman 2023-11-01 15:54:03 -04:00
parent ac76d8c4b8
commit 94c7112c00
4 changed files with 23 additions and 25 deletions

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,