From 7b790e09973fc65c85d016f49fe879655b411ec2 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 27 Oct 2022 16:12:23 +1100 Subject: [PATCH 01/14] Rough pass at adding custom spin buttons to NumberControl --- .../components/src/number-control/index.tsx | 64 ++++++++++++++++++- .../styles/number-control-styles.js | 9 +++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/packages/components/src/number-control/index.tsx b/packages/components/src/number-control/index.tsx index 322f4116202c62..25472b962a0996 100644 --- a/packages/components/src/number-control/index.tsx +++ b/packages/components/src/number-control/index.tsx @@ -2,23 +2,28 @@ * External dependencies */ import classNames from 'classnames'; -import type { ForwardedRef } from 'react'; +import type { ForwardedRef, ChangeEvent } from 'react'; /** * WordPress dependencies */ import { forwardRef } from '@wordpress/element'; import { isRTL } from '@wordpress/i18n'; +import { plus as plusIcon, reset as resetIcon } from '@wordpress/icons'; /** * Internal dependencies */ -import { Input } from './styles/number-control-styles'; +import { Input, SpinButton } from './styles/number-control-styles'; import * as inputControlActionTypes from '../input-control/reducer/actions'; import { add, subtract, roundClamp } from '../utils/math'; import { ensureNumber, isValueEmpty } from '../utils/values'; import type { WordPressComponentProps } from '../ui/context/wordpress-component'; import type { NumberControlProps } from './types'; +import InputControlSuffixWrapper from '../input-control/input-suffix-wrapper'; +import { HStack } from '../h-stack'; + +const noop = () => {}; function UnforwardedNumberControl( { @@ -36,6 +41,8 @@ function UnforwardedNumberControl( step = 1, type: typeProp = 'number', value: valueProp, + size = 'default', + onChange = noop, ...props }: WordPressComponentProps< NumberControlProps, 'input', false >, ref: ForwardedRef< any > @@ -185,7 +192,7 @@ function UnforwardedNumberControl( { ...props } className={ classes } dragDirection={ dragDirection } - hideHTMLArrows={ hideHTMLArrows } + hideHTMLArrows={ size === '__unstable-large' || hideHTMLArrows } isDragEnabled={ isDragEnabled } label={ label } max={ max } @@ -200,6 +207,57 @@ function UnforwardedNumberControl( const baseState = numberControlStateReducer( state, action ); return stateReducerProp?.( baseState, action ) ?? baseState; } } + size={ size } + suffix={ + size === '__unstable-large' && + ! hideHTMLArrows && ( + + + + + ) + } + onChange={ onChange } /> ); } diff --git a/packages/components/src/number-control/styles/number-control-styles.js b/packages/components/src/number-control/styles/number-control-styles.js index a27784497b018d..0dd78826d0127e 100644 --- a/packages/components/src/number-control/styles/number-control-styles.js +++ b/packages/components/src/number-control/styles/number-control-styles.js @@ -4,10 +4,13 @@ */ import { css } from '@emotion/react'; import styled from '@emotion/styled'; + /** * Internal dependencies */ import InputControl from '../../input-control'; +import { COLORS } from '../../utils'; +import Button from '../../button'; const htmlArrowStyles = ( { hideHTMLArrows } ) => { if ( ! hideHTMLArrows ) return ``; @@ -28,3 +31,9 @@ const htmlArrowStyles = ( { hideHTMLArrows } ) => { export const Input = styled( InputControl )` ${ htmlArrowStyles }; `; + +export const SpinButton = styled( Button )` + &&& { + color: ${ COLORS.ui.theme }; + } +`; From 016f253db5756f388b29506d48cfdf5739a3f1a4 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 27 Oct 2022 16:42:47 +1100 Subject: [PATCH 02/14] Adjust spacing of step buttons to match design --- packages/components/src/number-control/index.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/components/src/number-control/index.tsx b/packages/components/src/number-control/index.tsx index 25472b962a0996..769ddbc8802141 100644 --- a/packages/components/src/number-control/index.tsx +++ b/packages/components/src/number-control/index.tsx @@ -20,8 +20,8 @@ import { add, subtract, roundClamp } from '../utils/math'; import { ensureNumber, isValueEmpty } from '../utils/values'; import type { WordPressComponentProps } from '../ui/context/wordpress-component'; import type { NumberControlProps } from './types'; -import InputControlSuffixWrapper from '../input-control/input-suffix-wrapper'; import { HStack } from '../h-stack'; +import { Spacer } from '../spacer'; const noop = () => {}; @@ -211,8 +211,8 @@ function UnforwardedNumberControl( suffix={ size === '__unstable-large' && ! hideHTMLArrows && ( - - + + - + ) } onChange={ onChange } From 49385466a54356152d36d382968b33022e56dc95 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 27 Oct 2022 16:47:56 +1100 Subject: [PATCH 03/14] Pass along suffix if provided --- .../components/src/number-control/index.tsx | 97 ++++++++++--------- 1 file changed, 51 insertions(+), 46 deletions(-) diff --git a/packages/components/src/number-control/index.tsx b/packages/components/src/number-control/index.tsx index 769ddbc8802141..5f59ec30253c4a 100644 --- a/packages/components/src/number-control/index.tsx +++ b/packages/components/src/number-control/index.tsx @@ -42,6 +42,7 @@ function UnforwardedNumberControl( type: typeProp = 'number', value: valueProp, size = 'default', + suffix, onChange = noop, ...props }: WordPressComponentProps< NumberControlProps, 'input', false >, @@ -209,52 +210,56 @@ function UnforwardedNumberControl( } } size={ size } suffix={ - size === '__unstable-large' && - ! hideHTMLArrows && ( - - - - + size === '__unstable-large' && ! hideHTMLArrows ? ( + <> + { suffix } + + + + + + ) : ( + suffix ) } onChange={ onChange } From 2cc595374b7af2a9c914e6ff9db6408ad3f92bbe Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 27 Oct 2022 16:53:58 +1100 Subject: [PATCH 04/14] DRY up onClick handlers --- .../components/src/number-control/index.tsx | 45 +++++++------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/packages/components/src/number-control/index.tsx b/packages/components/src/number-control/index.tsx index 5f59ec30253c4a..449526483cf6fc 100644 --- a/packages/components/src/number-control/index.tsx +++ b/packages/components/src/number-control/index.tsx @@ -186,6 +186,19 @@ function UnforwardedNumberControl( return nextState; }; + const buildSpinHandler = + ( direction: 'up' | 'down' ) => + ( event: ChangeEvent< HTMLInputElement > ) => { + let nextValue = isValueEmpty( valueProp ) ? baseValue : valueProp; + if ( direction === 'up' ) { + nextValue = add( nextValue, baseStep ); + } else if ( direction === 'down' ) { + nextValue = subtract( nextValue, baseStep ); + } + nextValue = constrainValue( nextValue ); + onChange( String( nextValue ), { event } ); + }; + return ( - ) => { - const currentValue = isValueEmpty( - valueProp - ) - ? baseValue - : valueProp; - const nextValue = constrainValue( - add( currentValue, baseStep ) - ); - onChange?.( String( nextValue ), { - event, - } ); - } } + onClick={ buildSpinHandler( 'up' ) } />