-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UnitControl: Improve interactions + add unit parsing (#22329)
* Improve UnitControl component This update adds rich features and interactions to the UnitControl component. Originally, this work was in support of adding padding controls to the Cover block. This update abstracts just the UnitControls from that effort. UnitControl and NumberControl are now powered by a new lower level primitive: InputControl. InputControl handles the majority of the interaction and event logic. Components using it, like NumberControl, are able to tap into the state flow and add features like "drag to update". * Update docs manifest * Fix inputControl action type for ~SUBMIT~ COMMIT * Remove stateReducer memoization. Add isPressEnterToChange knob for Input stories * Refactors parseUnit utility * Fixed unit parsing in parseUnit utility function * Remove typos in README description * Fix color values and offsets for InputControl label * Remove arg flattening and rename combineStateReducers to composeStateReducers * Update packages/components/src/unit-control/utils.js Co-authored-by: Zebulan Stanphill <zebulanstanphill@protonmail.com> * Add default value for isFocused in InputControl Backdrop * Improve label rendering for long values with floating label Also improve Safari placeholder vertical alignment * Remove unused composeStateReducers from useInputControlStateReducer * Replace combinedStateReducers in args + comments with composedStateReducers * Update return state of composeStateReducers * Optimize parseUnit with keyBy rather than Array.find * Refactor classNames for InputControl and UnitControl * Revert "Optimize parseUnit with keyBy rather than Array.find" This reverts commit 8fdc6e8. Co-authored-by: Zebulan Stanphill <zebulanstanphill@protonmail.com>
- Loading branch information
1 parent
d26842b
commit 6efa61c
Showing
29 changed files
with
2,312 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# InputControl | ||
|
||
InputControl components let users enter and edit text. This is an experimental component intended to (in time) merge with or replace [TextControl](../text-control). | ||
|
||
## Usage | ||
|
||
```js | ||
import { __experimentalInputControl as InputControl } from '@wordpress/components'; | ||
import { useState } from '@wordpress/compose'; | ||
|
||
const Example = () => { | ||
const [ value, setValue ] = useState( '' ); | ||
|
||
return ( | ||
<InputControl | ||
value={ value } | ||
onChange={ ( nextValue ) => setValue( nextValue ) } | ||
/> | ||
); | ||
}; | ||
``` | ||
|
||
## Props | ||
|
||
### disabled | ||
|
||
If true, the `input` will be disabled. | ||
|
||
- Type: `Boolean` | ||
- Required: No | ||
- Default: `false` | ||
|
||
### isPressEnterToChange | ||
|
||
If true, the `ENTER` key press is required in order to trigger an `onChange`. If enabled, a change is also triggered when tabbing away (`onBlur`). | ||
|
||
- Type: `Boolean` | ||
- Required: No | ||
- Default: `false` | ||
|
||
### isFloatingLabel | ||
|
||
If true, the `label` will render with a floating interaction. | ||
|
||
- Type: `Boolean` | ||
- Required: No | ||
|
||
### hideLabelFromVision | ||
|
||
If true, the label will only be visible to screen readers. | ||
|
||
- Type: `Boolean` | ||
- Required: No | ||
|
||
### label | ||
|
||
If this property is added, a label will be generated using label property as the content. | ||
|
||
- Type: `String` | ||
- Required: No | ||
|
||
### onChange | ||
|
||
A function that receives the value of the input. | ||
|
||
- Type: `Function` | ||
- Required: Yes | ||
|
||
### size | ||
|
||
Adjusts the size of the input. | ||
Sizes include: `default`, `small` | ||
|
||
- Type: `String` | ||
- Required: No | ||
- Default: `default` | ||
|
||
### suffix | ||
|
||
Renders an element on the right side of the input. | ||
|
||
- Type: `React.ReactNode` | ||
- Required: No | ||
|
||
### type | ||
|
||
Type of the input element to render. Defaults to "text". | ||
|
||
- Type: `String` | ||
- Required: No | ||
- Default: "text" | ||
|
||
### value | ||
|
||
The current value of the input. | ||
|
||
- Type: `String | Number` | ||
- Required: Yes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { memo } from '@wordpress/element'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Fieldset, Legend, LegendText } from './styles/input-control-styles'; | ||
|
||
function Backdrop( { | ||
disabled = false, | ||
isFloating = false, | ||
isFloatingLabel = false, | ||
isFocused = false, | ||
label, | ||
size = 'default', | ||
} ) { | ||
return ( | ||
<Fieldset | ||
aria-hidden="true" | ||
className="components-input-control__backdrop" | ||
disabled={ disabled } | ||
isFloatingLabel={ isFloatingLabel } | ||
isFocused={ isFocused } | ||
> | ||
{ isFloatingLabel && ( | ||
<Legend | ||
aria-hidden="true" | ||
className="components-input-control__backdrop-label" | ||
isFloating={ isFloating } | ||
size={ size } | ||
> | ||
<LegendText className="components-input-control__backdrop-text"> | ||
{ label } | ||
</LegendText> | ||
</Legend> | ||
) } | ||
</Fieldset> | ||
); | ||
} | ||
|
||
const MemoizedBackdrop = memo( Backdrop ); | ||
|
||
export default MemoizedBackdrop; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { noop } from 'lodash'; | ||
import classNames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { useState, forwardRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Backdrop from './backdrop'; | ||
import InputField from './input-field'; | ||
import Label from './label'; | ||
import { Container, Root, Suffix } from './styles/input-control-styles'; | ||
import { isValueEmpty } from './utils'; | ||
|
||
function useUniqueId( idProp ) { | ||
const instanceId = useInstanceId( InputControl ); | ||
const id = `inspector-input-control-${ instanceId }`; | ||
|
||
return idProp || id; | ||
} | ||
|
||
export function InputControl( | ||
{ | ||
__unstableStateReducer: stateReducer = ( state ) => state, | ||
children, | ||
className, | ||
disabled = false, | ||
hideLabelFromVision = false, | ||
id: idProp, | ||
isPressEnterToChange = false, | ||
isFloatingLabel = false, | ||
label, | ||
onBlur = noop, | ||
onChange = noop, | ||
onFocus = noop, | ||
onValidate = noop, | ||
onKeyDown = noop, | ||
size = 'default', | ||
suffix, | ||
value, | ||
...props | ||
}, | ||
ref | ||
) { | ||
const [ isFocused, setIsFocused ] = useState( false ); | ||
const [ isFilled, setIsFilled ] = useState( ! isValueEmpty( value ) ); | ||
|
||
const id = useUniqueId( idProp ); | ||
const classes = classNames( 'components-input-control', className ); | ||
|
||
const handleOnBlur = ( event ) => { | ||
onBlur( event ); | ||
setIsFocused( false ); | ||
}; | ||
|
||
const handleOnFocus = ( event ) => { | ||
onFocus( event ); | ||
setIsFocused( true ); | ||
}; | ||
|
||
const isInputFilled = isFilled || ! isValueEmpty( value ); | ||
|
||
const isFloating = isFloatingLabel ? isInputFilled || isFocused : false; | ||
const isFloatingLabelSet = | ||
! hideLabelFromVision && isFloatingLabel && label; | ||
|
||
return ( | ||
<Root | ||
className={ classes } | ||
isFloatingLabel={ isFloatingLabelSet } | ||
isFocused={ isFocused } | ||
> | ||
<Label | ||
className="components-input-control__label" | ||
hideLabelFromVision={ hideLabelFromVision } | ||
htmlFor={ id } | ||
isFilled={ isFilled } | ||
isFloating={ isFloating } | ||
isFloatingLabel={ isFloatingLabel } | ||
size={ size } | ||
> | ||
{ label } | ||
</Label> | ||
<Container | ||
className="components-input-control__container" | ||
disabled={ disabled } | ||
isFocused={ isFocused } | ||
> | ||
<InputField | ||
{ ...props } | ||
className="components-input-control__input" | ||
disabled={ disabled } | ||
id={ id } | ||
isFloating={ isFloating } | ||
isFloatingLabelSet={ isFloatingLabelSet } | ||
isPressEnterToChange={ isPressEnterToChange } | ||
onBlur={ handleOnBlur } | ||
onChange={ onChange } | ||
onFocus={ handleOnFocus } | ||
onKeyDown={ onKeyDown } | ||
onUpdateValue={ setIsFilled } | ||
onValidate={ onValidate } | ||
ref={ ref } | ||
setIsFocused={ setIsFocused } | ||
size={ size } | ||
stateReducer={ stateReducer } | ||
value={ value } | ||
/> | ||
{ suffix && ( | ||
<Suffix className="components-input-control__suffix"> | ||
{ suffix } | ||
</Suffix> | ||
) } | ||
<Backdrop | ||
aria-hidden="true" | ||
disabled={ disabled } | ||
isFloating={ isFloating } | ||
isFloatingLabel={ isFloatingLabelSet } | ||
isFocused={ isFocused } | ||
label={ label } | ||
size={ size } | ||
/> | ||
{ children } | ||
</Container> | ||
</Root> | ||
); | ||
} | ||
|
||
export default forwardRef( InputControl ); |
Oops, something went wrong.