Skip to content

Commit

Permalink
Merge branch 'trunk' into ts-treegrid
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka authored Feb 2, 2023
2 parents d30ea0b + b272396 commit 2497ee6
Show file tree
Hide file tree
Showing 37 changed files with 581 additions and 274 deletions.
2 changes: 2 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

- `Toolbar`: unify Storybook examples under one file, migrate from knobs to controls ([47117](https://github.com/WordPress/gutenberg/pull/47117)).
- `DropdownMenu`: migrate Storybook to controls ([47149](https://github.com/WordPress/gutenberg/pull/47149)).
- `Panel`, `PanelHeader`, `PanelRow`: Convert to TypeScript ([#47259](https://github.com/WordPress/gutenberg/pull/47259)).
- Removed deprecated `@storybook/addon-knobs` dependency from the package ([47152](https://github.com/WordPress/gutenberg/pull/47152)).
- `ColorListPicker`: Convert to TypeScript ([#46358](https://github.com/WordPress/gutenberg/pull/46358)).
- `KeyboardShortcuts`: Convert to TypeScript ([#47429](https://github.com/WordPress/gutenberg/pull/47429)).
- `ColorPalette`, `BorderControl`, `GradientPicker`: refine types and logic around single vs multiple palettes
([#47384](https://github.com/WordPress/gutenberg/pull/47384)).
- `Button`: Convert to TypeScript ([#46997](https://github.com/WordPress/gutenberg/pull/46997)).
- `BoxControl`: Convert to TypeScript ([#47622](https://github.com/WordPress/gutenberg/pull/47622)).
- `QueryControls`: Convert to TypeScript ([#46721](https://github.com/WordPress/gutenberg/pull/46721)).
- `TreeGrid`: Convert to TypeScript ([#47516](https://github.com/WordPress/gutenberg/pull/47516)).
- `Notice`: refactor to TypeScript ([47118](https://github.com/WordPress/gutenberg/pull/47118)).
Expand Down
43 changes: 17 additions & 26 deletions packages/components/src/box-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,82 +30,73 @@ const Example = () => {
```

## Props
### allowReset
### `allowReset`: `boolean`

If this property is true, a button to reset the box control is rendered.

- Type: `Boolean`
- Required: No
- Default: `true`

### splitOnAxis
### `splitOnAxis`: `boolean`

If this property is true, when the box control is unlinked, vertical and horizontal controls can be used instead of updating individual sides.

- Type: `Boolean`
- Required: No
- Default: `false`

### inputProps
### `inputProps`: `object`

Props for the internal [InputControl](../input-control) components.
Props for the internal [UnitControl](../unit-control) components.

- Type: `Object`
- Required: No
- Default: `{ min: 0 }`

### label
### `label`: `string`

Heading label for BoxControl.
Heading label for the control.

- Type: `String`
- Required: No
- Default: `Box Control`
- Default: `__( 'Box Control' )`

### onChange
### `onChange`: `(next: BoxControlValue) => void`

A callback function when an input value changes.

- Type: `Function`
- Required: Yes

### resetValues
### `resetValues`: `object`

The `top`, `right`, `bottom`, and `left` box dimension values to use when the control is reset.

- Type: `Object`
- Required: No
- Default: `{ top: undefined, right: undefined, bottom: undefined, left: undefined }`

### sides
### `sides`: `string[]`

Collection of sides to allow control of. If omitted or empty, all sides will be available.
Collection of sides to allow control of. If omitted or empty, all sides will be available. Allowed values are "top", "right", "bottom", "left", "vertical", and "horizontal".

- Type: `Array<Object>`
- Required: No

### units
### `units`: `WPUnitControlUnit[]`

Collection of available units which are compatible with [UnitControl](../unit-control).

- Type: `Array<Object>`
- Required: No

### values
### `values`: `object`

The `top`, `right`, `bottom`, and `left` box dimension values.

- Type: `Object`
- Required: No

### onMouseOver
### `onMouseOver`: `function`

A handler for onMouseOver events.

- Type: `Function`
- Required: No

### onMouseOut
### `onMouseOut`: `function`

A handler for onMouseOut events.

- Type: `Function`
- Required: No
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Internal dependencies
*/
import type { UnitControlProps } from '../unit-control/types';
import type { BoxControlInputControlProps } from './types';
import UnitControl from './unit-control';
import {
LABELS,
Expand All @@ -22,18 +24,20 @@ export default function AllInputControl( {
selectedUnits,
setSelectedUnits,
...props
} ) {
}: BoxControlInputControlProps ) {
const allValue = getAllValue( values, selectedUnits, sides );
const hasValues = isValuesDefined( values );
const isMixed = hasValues && isValuesMixed( values, selectedUnits, sides );
const allPlaceholder = isMixed ? LABELS.mixed : null;
const allPlaceholder = isMixed ? LABELS.mixed : undefined;

const handleOnFocus = ( event ) => {
const handleOnFocus: React.FocusEventHandler< HTMLInputElement > = (
event
) => {
onFocus( event, { side: 'all' } );
};

const handleOnChange = ( next ) => {
const isNumeric = ! isNaN( parseFloat( next ) );
const handleOnChange: UnitControlProps[ 'onChange' ] = ( next ) => {
const isNumeric = next !== undefined && ! isNaN( parseFloat( next ) );
const nextValue = isNumeric ? next : undefined;
const nextValues = applyValueToSides( values, nextValue, sides );

Expand All @@ -42,7 +46,7 @@ export default function AllInputControl( {

// Set selected unit so it can be used as fallback by unlinked controls
// when individual sides do not have a value containing a unit.
const handleOnUnitChange = ( unit ) => {
const handleOnUnitChange: UnitControlProps[ 'onUnitChange' ] = ( unit ) => {
const newUnits = applyValueToSides( selectedUnits, unit, sides );
setSelectedUnits( newUnits );
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils';
import UnitControl from './unit-control';
import { LABELS } from './utils';
import { Layout } from './styles/box-control-styles';
import type { BoxControlInputControlProps } from './types';

const groupedSides = [ 'vertical', 'horizontal' ];
const groupedSides = [ 'vertical', 'horizontal' ] as const;
type GroupedSide = typeof groupedSides[ number ];

export default function AxialInputControls( {
onChange,
Expand All @@ -18,15 +20,17 @@ export default function AxialInputControls( {
setSelectedUnits,
sides,
...props
} ) {
const createHandleOnFocus = ( side ) => ( event ) => {
if ( ! onFocus ) {
return;
}
onFocus( event, { side } );
};
}: BoxControlInputControlProps ) {
const createHandleOnFocus =
( side: GroupedSide ) =>
( event: React.FocusEvent< HTMLInputElement > ) => {
if ( ! onFocus ) {
return;
}
onFocus( event, { side } );
};

const createHandleOnHoverOn = ( side ) => () => {
const createHandleOnHoverOn = ( side: GroupedSide ) => () => {
if ( ! onHoverOn ) {
return;
}
Expand All @@ -44,7 +48,7 @@ export default function AxialInputControls( {
}
};

const createHandleOnHoverOff = ( side ) => () => {
const createHandleOnHoverOff = ( side: GroupedSide ) => () => {
if ( ! onHoverOff ) {
return;
}
Expand All @@ -62,12 +66,12 @@ export default function AxialInputControls( {
}
};

const createHandleOnChange = ( side ) => ( next ) => {
const createHandleOnChange = ( side: GroupedSide ) => ( next?: string ) => {
if ( ! onChange ) {
return;
}
const nextValues = { ...values };
const isNumeric = ! isNaN( parseFloat( next ) );
const isNumeric = next !== undefined && ! isNaN( parseFloat( next ) );
const nextValue = isNumeric ? next : undefined;

if ( side === 'vertical' ) {
Expand All @@ -83,21 +87,22 @@ export default function AxialInputControls( {
onChange( nextValues );
};

const createHandleOnUnitChange = ( side ) => ( next ) => {
const newUnits = { ...selectedUnits };
const createHandleOnUnitChange =
( side: GroupedSide ) => ( next?: string ) => {
const newUnits = { ...selectedUnits };

if ( side === 'vertical' ) {
newUnits.top = next;
newUnits.bottom = next;
}
if ( side === 'vertical' ) {
newUnits.top = next;
newUnits.bottom = next;
}

if ( side === 'horizontal' ) {
newUnits.left = next;
newUnits.right = next;
}
if ( side === 'horizontal' ) {
newUnits.left = next;
newUnits.right = next;
}

setSelectedUnits( newUnits );
};
setSelectedUnits( newUnits );
};

// Filter sides if custom configuration provided, maintaining default order.
const filteredSides = sides?.length
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Internal dependencies
*/
import type { WordPressComponentProps } from '../ui/context';
import {
Root,
Viewbox,
Expand All @@ -9,6 +10,7 @@ import {
BottomStroke,
LeftStroke,
} from './styles/box-control-icon-styles';
import type { BoxControlIconProps, BoxControlProps } from './types';

const BASE_ICON_SIZE = 24;

Expand All @@ -17,11 +19,14 @@ export default function BoxControlIcon( {
side = 'all',
sides,
...props
} ) {
const isSideDisabled = ( value ) =>
sides?.length && ! sides.includes( value );
}: WordPressComponentProps< BoxControlIconProps, 'span' > ) {
const isSideDisabled = (
value: NonNullable< BoxControlProps[ 'sides' ] >[ number ]
) => sides?.length && ! sides.includes( value );

const hasSide = ( value ) => {
const hasSide = (
value: NonNullable< BoxControlProps[ 'sides' ] >[ number ]
) => {
if ( isSideDisabled( value ) ) {
return false;
}
Expand Down
Loading

0 comments on commit 2497ee6

Please sign in to comment.