Use native step buttons

This commit is contained in:
Nathan Schneider 2024-01-05 14:39:04 -03:00
parent d86ee233d2
commit 8425d17399
7 changed files with 5 additions and 87 deletions

View File

@ -1,4 +1,4 @@
Significance: minor Significance: minor
Type: add Type: add
Add showStepButtons attribute for Number block and component Add native step buttons for Number block and component

View File

@ -79,14 +79,6 @@ The maximum numeric value that can be entered in the field.
The amount that is incremented or decremented when using the up/down arrows. The amount that is incremented or decremented when using the up/down arrows.
### showStepButtons
- **Type:** `Boolean`
- **Required:** `No`
- **Default:** `false`
Shows + and - buttons to increment/decrement the numeric value.
## Usage ## Usage
Here's a snippet that adds a field similar to the previous screenshot: Here's a snippet that adds a field similar to the previous screenshot:

View File

@ -40,10 +40,6 @@
"step": { "step": {
"type": "number", "type": "number",
"default": 1 "default": 1
},
"showStepButtons": {
"type": "boolean",
"default": false
} }
}, },
"supports": { "supports": {

View File

@ -32,7 +32,6 @@ export function Edit( {
tooltip, tooltip,
disabled, disabled,
step, step,
showStepButtons,
} = attributes; } = attributes;
const [ value, setValue ] = useProductEntityProp( property, { const [ value, setValue ] = useProductEntityProp( property, {
postType, postType,
@ -96,7 +95,6 @@ export function Edit( {
tooltip={ tooltip } tooltip={ tooltip }
disabled={ disabled } disabled={ disabled }
step={ step } step={ step }
showStepButtons={ showStepButtons }
/> />
</div> </div>
); );

View File

@ -14,5 +14,4 @@ export interface NumberBlockAttributes extends BlockAttributes {
required?: boolean; required?: boolean;
tooltip?: string; tooltip?: string;
step?: number; step?: number;
showStepButtons: boolean;
} }

View File

@ -1,14 +1,11 @@
/** /**
* External dependencies * External dependencies
*/ */
import { createElement, Fragment } from '@wordpress/element'; import { createElement } from '@wordpress/element';
import { useInstanceId } from '@wordpress/compose'; import { useInstanceId } from '@wordpress/compose';
import classNames from 'classnames'; import classNames from 'classnames';
import { plus, reset } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import { import {
BaseControl, BaseControl,
Button,
// @ts-expect-error `__experimentalInputControl` does exist. // @ts-expect-error `__experimentalInputControl` does exist.
__experimentalInputControl as InputControl, __experimentalInputControl as InputControl,
} from '@wordpress/components'; } from '@wordpress/components';
@ -32,7 +29,6 @@ export type NumberProps = {
tooltip?: string; tooltip?: string;
disabled?: boolean; disabled?: boolean;
step?: number; step?: number;
showStepButtons?: boolean;
}; };
export const NumberControl: React.FC< NumberProps > = ( { export const NumberControl: React.FC< NumberProps > = ( {
@ -48,7 +44,6 @@ export const NumberControl: React.FC< NumberProps > = ( {
placeholder, placeholder,
disabled, disabled,
step = 1, step = 1,
showStepButtons = false,
}: NumberProps ) => { }: NumberProps ) => {
const inputProps = useNumberInputProps( { const inputProps = useNumberInputProps( {
value: value || '', value: value || '',
@ -77,53 +72,10 @@ export const NumberControl: React.FC< NumberProps > = ( {
step={ step } step={ step }
disabled={ disabled } disabled={ disabled }
id={ id } id={ id }
suffix={ suffix={ suffix }
<>
{ suffix }
{ showStepButtons && (
<>
<Button
icon={ plus }
onClick={ () =>
onChange(
String(
parseFloat( value || '0' ) +
step
)
)
}
isSmall
aria-hidden="true"
aria-label={ __(
'Increment',
'woocommerce'
) }
tabIndex={ -1 }
/>
<Button
icon={ reset }
onClick={ () =>
onChange(
String(
parseFloat( value || '0' ) -
step
)
)
}
isSmall
aria-hidden="true"
aria-label={ __(
'Decrement',
'woocommerce'
) }
tabIndex={ -1 }
/>
</>
) }
</>
}
placeholder={ placeholder } placeholder={ placeholder }
onBlur={ onBlur } onBlur={ onBlur }
type="number"
/> />
</BaseControl> </BaseControl>
); );

View File

@ -8,7 +8,6 @@ export type NumberInputProps = {
value: string; value: string;
onChange: ( value: string ) => void; onChange: ( value: string ) => void;
onFocus: ( event: React.FocusEvent< HTMLInputElement > ) => void; onFocus: ( event: React.FocusEvent< HTMLInputElement > ) => void;
onKeyUp: ( event: React.KeyboardEvent< HTMLInputElement > ) => void;
}; };
type Props = { type Props = {
@ -18,12 +17,7 @@ type Props = {
onKeyUp?: ( event: React.KeyboardEvent< HTMLInputElement > ) => void; onKeyUp?: ( event: React.KeyboardEvent< HTMLInputElement > ) => void;
}; };
export const useNumberInputProps = ( { export const useNumberInputProps = ( { value, onChange, onFocus }: Props ) => {
value,
onChange,
onFocus,
onKeyUp,
}: Props ) => {
const { formatNumber, parseNumber } = useProductHelper(); const { formatNumber, parseNumber } = useProductHelper();
const numberInputProps: NumberInputProps = { const numberInputProps: NumberInputProps = {
@ -34,19 +28,6 @@ export const useNumberInputProps = ( {
onFocus( event ); onFocus( event );
} }
}, },
onKeyUp( event: React.KeyboardEvent< HTMLInputElement > ) {
const amount = Number.parseFloat( value || '0' );
const step = Number( event.currentTarget.step || '1' );
if ( event.code === 'ArrowUp' ) {
onChange( String( amount + step ) );
}
if ( event.code === 'ArrowDown' ) {
onChange( String( amount - step ) );
}
if ( onKeyUp ) {
onKeyUp( event );
}
},
onChange( newValue: string ) { onChange( newValue: string ) {
const sanitizeValue = parseNumber( newValue ); const sanitizeValue = parseNumber( newValue );
onChange( sanitizeValue ); onChange( sanitizeValue );