Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeControl: Convert component to TypeScript #40535

Merged
merged 28 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
da9f81f
Convert RangeControl to TypeScript
aaronrobertshaw May 30, 2022
71b528f
Add named export for docgen
aaronrobertshaw May 30, 2022
b22fc98
Add changelog
aaronrobertshaw May 31, 2022
24320dc
Fix naming of component
aaronrobertshaw May 31, 2022
8bb4c17
Rename and make icon props optional
aaronrobertshaw Jun 5, 2022
60cca10
Add additional descriptions for icon types
aaronrobertshaw Jun 5, 2022
c3adac8
Improve color type descriptions
aaronrobertshaw Jun 6, 2022
57f23cc
Omit as prop from WordPressComponentProps
aaronrobertshaw Jun 6, 2022
d76dd77
Remove unnecessary backticks from type default
aaronrobertshaw Jun 6, 2022
057b4a8
Expand type definitions in RangeControlProps for better storybook docs
aaronrobertshaw Jun 6, 2022
54719d3
Fix tooltip regression when step is any
aaronrobertshaw Jun 6, 2022
9b18165
Fix removal of polymorphism for RangeControl
aaronrobertshaw Jun 7, 2022
af0b3ad
Fix post-rebase typing errors
aaronrobertshaw Jun 20, 2022
d2ae390
Minimise runtime changes via ts-expect-error
aaronrobertshaw Jul 3, 2022
8c2c5e2
Clarify icon prop descriptions
aaronrobertshaw Jul 5, 2022
da940c5
Revert resetFallbackValue type to number
aaronrobertshaw Jul 5, 2022
775fafe
Remove unrequired className from WrapperProps
aaronrobertshaw Jul 5, 2022
b0914c9
Fix WordPressComponentProps and ForwardedRef types
aaronrobertshaw Jul 5, 2022
0b7ea66
Leverage useMergeRefs to fix for modern refs
aaronrobertshaw Jul 5, 2022
357eedf
Fix rebase error
aaronrobertshaw Jul 5, 2022
3f5e9d0
Remove unused SetControlledRangeValue type
aaronrobertshaw Jul 5, 2022
70f331e
Fix onShowTooltip and onHideTooltip types
aaronrobertshaw Jul 5, 2022
822d757
Remove types from JSDocs
aaronrobertshaw Jul 5, 2022
2887c5b
Improve useControlledRangeValue hook types
aaronrobertshaw Jul 5, 2022
d5086e2
Fix copy and pasted typo
aaronrobertshaw Jul 11, 2022
cace914
Apply suggested feedback to remove runtime changes
aaronrobertshaw Jul 11, 2022
c2c7d1e
Tweak JSDoc comments and typing
aaronrobertshaw Jul 11, 2022
f19616e
Fix case for custom hook types
aaronrobertshaw Jul 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- `Autocomplete`: Refactor away from `_.deburr()` ([#42266](https://github.com/WordPress/gutenberg/pull/42266/)).
- `MenuItem`: Refactor away from `_.isString()` ([#42268](https://github.com/WordPress/gutenberg/pull/42268/)).
- `Shortcut`: Refactor away from `_.isString()` ([#42268](https://github.com/WordPress/gutenberg/pull/42268/)).
- `RangeControl`: Convert to TypeScript ([#40535](https://github.com/WordPress/gutenberg/pull/40535)).

## 19.14.0 (2022-06-29)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function useBorderControl(
);

const onSliderChange = useCallback(
( value: string ) => {
( value?: number ) => {
onWidthChange( `${ value }${ widthUnit }` );
},
[ onWidthChange, widthUnit ]
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/color-picker/input-with-slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const InputWithSlider = ( {
min={ min }
max={ max }
value={ value }
// @ts-expect-error
// See: https://github.com/WordPress/gutenberg/pull/40535#issuecomment-1172418185
onChange={ onChange }
withInputField={ false }
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/icon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { SVG } from '@wordpress/primitives';
import Dashicon from '../dashicon';
import type { IconKey as DashiconIconKey } from '../dashicon/types';

type IconType< P > = DashiconIconKey | ComponentType< P > | JSX.Element;
export type IconType< P > = DashiconIconKey | ComponentType< P > | JSX.Element;

interface BaseProps< P > {
/**
Expand Down
186 changes: 107 additions & 79 deletions packages/components/src/range-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,75 +109,100 @@ const MyRangeControl = () => {
};
```

### Props
## Props

The set of props accepted by the component will be specified below.
Props not included in this set will be applied to the input elements.

#### label
### `afterIcon`: `string|Function|WPComponent|null`

If this property is added, a label will be generated using label property as the content.
If this property is added, an [Icon component](/packages/components/src/icon/README.md) will be rendered after the slider with the icon equal to `afterIcon`.

For more information on `IconType` see the [Icon component](/packages/components/src/icon/index.tsx#L23).

- Required: No
- Platform: Web

### `allowReset`: `boolean`

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

- Type: `String`
- Required: No
- Default: `false`
- Platform: Web | Mobile

#### help
### `beforeIcon`: `string|Function|WPComponent|null`

If this property is added, a help text will be generated using help property as the content.
If this property is added, an [Icon component](/packages/components/src/icon/README.md) will be rendered before the slider with the icon equal to `beforeIcon`.

For more information on `IconType` see the [Icon component](/packages/components/src/icon/index.tsx#L23).

- Type: `String|WPElement`
- Required: No
- Platform: Web

#### beforeIcon
### `color`: `CSSProperties['color']`

If this property is added, a DashIcon component will be rendered before the slider with the icon equal to beforeIcon
CSS color string for the `RangeControl` wrapper.

- Type: `String`
- Required: No
- Platform: Web

#### afterIcon
### `currentInput`: `number`

If this property is added, a DashIcon component will be rendered after the slider with the icon equal to afterIcon
The current input to use as a fallback if `value` is currently `undefined`.

- Type: `String`
- Required: No
- Platform: Web

#### allowReset
### `disabled`: `boolean`

If this property is true, a button to reset the slider is rendered.
Disables the `input`, preventing new values from being applied.

- Type: `Boolean`
- Required: No
- Platform: Web | Mobile
- Platform: Web

#### disabled

Disables the `input`, preventing new values from being applied.
### `help`: `string|WPElement`

If this property is added, a help text will be generated using help property as the content.

- Type: `Boolean`
- Required: No
- Platform: Web

#### initialPosition
### `hideLabelFromVision`: `boolean`

Provides control over whether the label will only be visible to screen readers.

- Required: No

### `icon`: `string`

An icon to be shown above the slider next to its container title.

- Required: No
- Platform: Mobile

### `initialPosition`: `number`

If no value exists this prop contains the slider starting position.

- Type: `Number`
- Required: No
- Platform: Web | Mobile

#### isShiftStepEnabled
### `isShiftStepEnabled`: `boolean`

Passed as a prop to the `NumberControl` component and is only applicable if `withInputField` is true. If true, while the number input has focus, pressing `UP` or `DOWN` along with the `SHIFT` key will change the value by the `shiftStep` value.

- Type: `Boolean`
- Required: No

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

If this property is added, a label will be generated using label property as the content.

- Required: No
- Platform: Web | Mobile

### `marks`: `Array|boolean`

Renders a visual representation of `step` ticks. Custom mark indicators can be provided by an `Array`.

Expand All @@ -204,49 +229,72 @@ const marks = [
];

const MyRangeControl() {
return (<RangeControl marks={ marks } min={ 0 } max={ 10 } step={ 1 } />)
return ( <RangeControl marks={ marks } min={ 0 } max={ 10 } step={ 1 } /> )
}
```

- Type: `Array|Boolean`
- Required: No
- Platform: Web

#### onChange
### `onBlur`: `FocusEventHandler< HTMLInputElement >`

Callback for when `RangeControl` input loses focus.

- Required: No
- Platform: Web

### `onChange`: `( value?: number ) => void`

A function that receives the new value. The value will be less than `max` and more than `min` unless a reset (enabled by `allowReset`) has occurred. In which case the value will be either that of `resetFallbackValue` if it has been specified or otherwise `undefined`.

- Type: `function`
- Required: Yes
- Required: No
- Platform: Web | Mobile

#### min
### `onFocus`: `FocusEventHandler< HTMLInputElement >`

Callback for when `RangeControl` input gains focus.

- Required: No
- Platform: Web

### `onMouseLeave`: `MouseEventHandler< HTMLInputElement >`

Callback for when mouse exits the `RangeControl`.

- Required: No
- Platform: Web

### `onMouseMove`: `MouseEventHandler< HTMLInputElement >`

Callback for when mouse moves within the `RangeControl`.

- Required: No
- Platform: Web

### `min`: `number`

The minimum `value` allowed.

- Type: `Number`
- Required: No
- Default: 0
- Platform: Web | Mobile

#### max
### `max`: `number`

The maximum `value` allowed.

- Type: `Number`
- Required: No
- Default: 100
- Platform: Web | Mobile

#### railColor
### `railColor`: `CSSProperties[ 'color' ]`

Customizes the (background) color of the rail element.
CSS color string to customize the rail element's background.

- Type: `String`
- Required: No
- Platform: Web

#### renderTooltipContent
### `renderTooltipContent`: ( value ) => value

A way to customize the rendered UI of the value. Example:

Expand All @@ -258,89 +306,69 @@ const MyRangeControl() {
}
```

- Type: `Function`
- Required: No
- Platform: Web

#### resetFallbackValue
### `resetFallbackValue`: `number`

The value to revert to if the Reset button is clicked (enabled by `allowReset`)

- Type: `Number`
- Required: No
- Platform: Web

#### showTooltip

Forcing the Tooltip UI to show or hide. This is overriden to `false` when `step` is set to the special string value `any`.

- Type: `Boolean`
- Required: No
- Platform: Web

#### step
### `separatorType`: `'none' | 'fullWidth' | 'topFullWidth'`

The minimum amount by which `value` changes. It is also a factor in validation as `value` must be a multiple of `step` (offset by `min`) to be valid. Accepts the special string value `any` that voids the validation constraint and overrides both `withInputField` and `showTooltip` props to `false`.
Define if separator line under/above control row should be disabled or full width. By default it is placed below excluding underline the control icon.

- Type: `Number | "any"`
- Required: No
- Platform: Web
- Platform: Mobile

#### shiftStep
### `shiftStep`: `number`

Passed as a prop to the `NumberControl` component and is only applicable if `withInputField` and `isShiftStepEnabled` are both true and while the number input has focus. Acts as a multiplier of `step`.

- Type: `Number`
- Required: No

#### trackColor
### `showTooltip`: `boolean`

Customizes the (background) color of the track element.
Forcing the Tooltip UI to show or hide. This is overridden to `false` when `step` is set to the special string value `any`.

- Type: `String`
- Required: No
- Platform: Web

#### value
### `step`: `number | 'any'`

The current value of the range slider.

- Type: `Number`
- Required: Yes
- Platform: Web | Mobile
The minimum amount by which `value` changes. It is also a factor in validation as `value` must be a multiple of `step` (offset by `min`) to be valid. Accepts the special string value `any` that voids the validation constraint and overrides both `withInputField` and `showTooltip` props to `false`.

#### withInputField
- Required: No
- Platform: Web
### `trackColor`: `CSSProperties[ 'color' ]`

Determines if the `input` number field will render next to the RangeControl. This is overriden to `false` when `step` is set to the special string value `any`.
CSS color string to customize the track element's background.

- Type: `Boolean`
- Required: No
- Platform: Web

#### icon
### `type`: `string`

An icon to be shown above the slider next to it's container title.
Define if the value selection should present a stepper control or a slider control in the bottom sheet on mobile. To use the stepper set the type value as `stepper`. Defaults to slider if no option is provided.

- Type: `String`
- Required: No
- Platform: Mobile

#### separatorType
### `value`: `number`

Define if separator line under/above control row should be disabled or full width. By default it is placed below excluding underline the control icon.
The current value of the range slider.

- Type: `String Enum`
- Values: `none` | `fullWidth` | `topFullWidth`
- Required: No
- Platform: Mobile
- Platform: Web | Mobile

#### type
### `withInputField`: `boolean`

Define if the value selection should present a stepper control or a slider control in the bottom sheet on mobile. To use the stepper set the type value as `stepper`. Defaults to slider if no option is provided.
Determines if the `input` number field will render next to the RangeControl. This is overridden to `false` when `step` is set to the special string value `any`.

- Type: `String`
- Required: No
- Platform: Mobile
- Platform: Web

## Related components

Expand Down
Loading