Product Editor: Fix select on focus for currency and number fields (#41171)
This commit is contained in:
commit
bae3a34501
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: fix
|
||||
|
||||
Fix selection in currency and number fields to only select if field still has focus.
|
|
@ -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 );
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue