Skip to content

Commit

Permalink
UnitControl: Improve interactions + add unit parsing (#22329)
Browse files Browse the repository at this point in the history
* 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
Jon Quach and ZebulanStanphill authored May 20, 2020
1 parent d26842b commit 6efa61c
Show file tree
Hide file tree
Showing 29 changed files with 2,312 additions and 225 deletions.
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,12 @@
"markdown_source": "../packages/components/src/icon/README.md",
"parent": "components"
},
{
"title": "InputControl",
"slug": "input-control",
"markdown_source": "../packages/components/src/input-control/README.md",
"parent": "components"
},
{
"title": "IsolatedEventContainer",
"slug": "isolated-event-container",
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"re-resizable": "^6.0.0",
"react-dates": "^17.1.1",
"react-spring": "^8.0.20",
"react-use-gesture": "^7.0.15",
"reakit": "^1.0.2",
"rememo": "^3.0.0",
"tinycolor2": "^1.4.1",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export { default as Guide } from './guide';
export { default as GuidePage } from './guide/page';
export { default as Icon } from './icon';
export { default as IconButton } from './button/deprecated';
export { default as __experimentalInputControl } from './input-control';
export { default as KeyboardShortcuts } from './keyboard-shortcuts';
export { default as MenuGroup } from './menu-group';
export { default as MenuItem } from './menu-item';
Expand Down
98 changes: 98 additions & 0 deletions packages/components/src/input-control/README.md
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
44 changes: 44 additions & 0 deletions packages/components/src/input-control/backdrop.js
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;
136 changes: 136 additions & 0 deletions packages/components/src/input-control/index.js
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 );
Loading

0 comments on commit 6efa61c

Please sign in to comment.