Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Normalize on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Dec 21, 2021
1 parent 23a8669 commit 1fae9a9
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions assets/js/base/components/quantity-selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { __, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import classNames from 'classnames';
import { useCallback } from '@wordpress/element';
import { useCallback, useLayoutEffect } from '@wordpress/element';
import { DOWN, UP } from '@wordpress/keycodes';
import { useDebouncedCallback } from 'use-debounce';

Expand Down Expand Up @@ -74,13 +74,8 @@ const QuantitySelector = ( {
/**
* The goal of this function is to normalize what was inserted,
* but after the customer has stopped typing.
*
* It's important to wait before normalizing or we end up with
* a frustrating experience, for example, if the minimum is 2 and
* the customer is trying to type "10", premature normalizing would
* always kick in at "1" and turn that into 2.
*/
const normalizeQuantity = useDebouncedCallback(
const normalizeQuantity = useCallback(
( initialValue: number ) => {
// We copy the starting value.
let value = initialValue;
Expand All @@ -105,10 +100,28 @@ const QuantitySelector = ( {
onChange( value );
}
},
[ hasMaximum, maximum, minimum, onChange, step ]
);

/*
* It's important to wait before normalizing or we end up with
* a frustrating experience, for example, if the minimum is 2 and
* the customer is trying to type "10", premature normalizing would
* always kick in at "1" and turn that into 2.
*/
const debouncedNormalizeQuantity = useDebouncedCallback(
normalizeQuantity,
// This value is deliberately smaller than what's in useStoreCartItemQuantity so we don't end up with two requests.
300
);

/**
* Normalize qty on mount before render.
*/
useLayoutEffect( () => {
normalizeQuantity( quantity );
}, [ quantity, normalizeQuantity ] );

/**
* Handles keyboard up and down keys to change quantity value.
*
Expand Down Expand Up @@ -160,7 +173,7 @@ const QuantitySelector = ( {
// we commit this value immediately.
onChange( value );
// but once the customer has stopped typing, we make sure his value is respecting the bounds (maximum value, minimum value, step value), and commit the normalized value.
normalizeQuantity( value );
debouncedNormalizeQuantity( value );
}
} }
aria-label={ sprintf(
Expand Down

0 comments on commit 1fae9a9

Please sign in to comment.