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

Refactor Button component to TypeScript #46997

Merged
merged 46 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c05948f
Rename button/index.js to .jsx
kienstra Jan 3, 2023
5b57ef6
Possible approach to the prop types
kienstra Jan 5, 2023
718959c
Simplify the tag container
kienstra Jan 9, 2023
d8cae0c
Allow passing childrent to TagElement
kienstra Jan 9, 2023
d007837
Fix the type of onKeyDown
kienstra Jan 10, 2023
6ab8806
Rename test/index.js to .tsx, fix some errors
kienstra Jan 10, 2023
878c77d
Fix an eslint error from children being defined in the outer scope
kienstra Jan 10, 2023
78e0aaf
Rename stories/index.js to .tsx, add type annotations
kienstra Jan 10, 2023
00c05da
Merge branch 'trunk' into update/button-ts
kienstra Jan 10, 2023
9e6cc7d
Alphabetize types.ts types
kienstra Jan 10, 2023
9298bd7
Refactor TagElement to element
kienstra Jan 10, 2023
7314d0a
Alphabetize an import
kienstra Jan 10, 2023
0b616c7
Revert changes to element, this will fail type checking
kienstra Jan 10, 2023
3998f8b
Merge branch 'trunk' into update/button-ts
kienstra Jan 15, 2023
67efafe
Use the [] array type syntax
kienstra Jan 15, 2023
bbc7cd4
Improve the typing of hasChildren
kienstra Jan 15, 2023
0f7a242
Fix the typing of chilren.length
kienstra Jan 15, 2023
ca6ded1
Revert "Revert changes to element, this will fail type checking"
kienstra Jan 15, 2023
dadbb4b
Pass the generic of WordPressComponentProps a second argument
kienstra Jan 15, 2023
a447deb
Don't destructure out children, simply let them be in props
kienstra Jan 15, 2023
e702dd4
Use React.MousEvent
kienstra Jan 15, 2023
0badec8
Update README.md with types from types.ts
kienstra Jan 15, 2023
b9a3b50
Fix the type of shortcut
kienstra Jan 15, 2023
57866b7
Remove unnecessary Type annotations
kienstra Jan 15, 2023
d8dcaee
In README.md, alphabetize the props
kienstra Jan 15, 2023
5e270a1
Add a Required annotation to the variant prop
kienstra Jan 15, 2023
8ce4937
Add a JS DocBlock to Button
kienstra Jan 15, 2023
fd4144a
Add a CHANGELOG entry
kienstra Jan 15, 2023
916f9ac
Commit Lena's suggestion: Update packages/components/src/button/types.ts
kienstra Jan 21, 2023
3632a42
Commit Lena's suggestion: Update packages/components/src/button/depre…
kienstra Jan 21, 2023
309db5c
Commit Lena's suggestion: Update packages/components/src/button/index…
kienstra Jan 21, 2023
31cbdab
Apply Lena's suggestion to change 'div' to 'button'
kienstra Jan 22, 2023
2ca61e5
Remvoe needless type DisabledEvent
kienstra Jan 22, 2023
be1359d
Remove needless argTypes that Storybook will infer from types
kienstra Jan 22, 2023
35f8efb
Restore a deleted test, thanks to Lena's idea
kienstra Jan 22, 2023
b84337d
Don't expect the console to error
kienstra Jan 22, 2023
14e1137
Update tooltipPosition in README.md
kienstra Jan 22, 2023
12fa70f
Merge branch 'trunk' into update/button-ts
kienstra Jan 22, 2023
a38c275
Move the CHANGELOG entry to Unreleased
kienstra Jan 22, 2023
cea1f6b
Cherry-pick @mirka 's commit to make Button types specific to a or b…
kienstra Jan 23, 2023
ccd87bc
Commit Lena's static tests verbatim
kienstra Jan 23, 2023
561d6aa
Fix a failed test run that I caused
kienstra Jan 23, 2023
3892543
Rename CommonButtonProps to BaseButtonProps
kienstra Jan 23, 2023
01f3c9c
Merge in trunk, resolve conflict
kienstra Jan 23, 2023
07b7002
Commit Lena's suggestion: Update packages/components/src/button/stori…
kienstra Jan 23, 2023
dfef117
Commit Lena's diff for static typing test
kienstra Jan 24, 2023
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 @@ -13,6 +13,7 @@
- `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)).
- Removed deprecated `@storybook/addon-knobs` dependency from the package ([47152](https://github.com/WordPress/gutenberg/pull/47152)).
- `Button`: Convert to TypeScript ([#46997](https://github.com/WordPress/gutenberg/pull/46997)).

### Bug Fix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useBorderBoxControlLinkedButton } from './hook';
import type { LinkedButtonProps } from '../types';

const BorderBoxControlLinkedButton = (
props: WordPressComponentProps< LinkedButtonProps, 'div' >,
props: WordPressComponentProps< LinkedButtonProps, 'button' >,
forwardedRef: React.ForwardedRef< any >
) => {
const { className, isLinked, ...buttonProps } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useCx } from '../../utils/hooks/use-cx';
import type { LinkedButtonProps } from '../types';

export function useBorderBoxControlLinkedButton(
props: WordPressComponentProps< LinkedButtonProps, 'div' >
props: WordPressComponentProps< LinkedButtonProps, 'button' >
) {
const {
className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const BorderControlDropdown = (
onClick={ onToggle }
variant="tertiary"
aria-label={ toggleAriaLabel }
position={ dropdownPosition }
tooltipPosition={ dropdownPosition }
Copy link
Contributor Author

@kienstra kienstra Jan 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<Button> has no position prop, so maybe this should be tooltipPosition.

This also has a showTooltip prop below.

label={ __( 'Border color and style picker' ) }
showTooltip={ true }
>
Expand Down
104 changes: 49 additions & 55 deletions packages/components/src/button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,131 +121,125 @@ The presence of a `href` prop determines whether an `anchor` element is rendered

Props not included in this set will be applied to the `a` or `button` element.

#### disabled
#### `children`: `ReactNode`

Whether the button is disabled. If `true`, this will force a `button` element to be rendered.
The button's children.

- Type: `Boolean`
- Required: No

#### href
#### `className`: `string`

If provided, renders `a` instead of `button`.
An optional additional class name to apply to the rendered button.

- Type: `String`
- Required: No

#### variant
#### `describedBy`: `string`

Specifies the button's style. The accepted values are `'primary'` (the primary button styles), `'secondary'` (the default button styles), `'tertiary'` (the text-based button styles), and `'link'` (the link button styles).
An accessible description for the button.

- Type: `String`
- Required: No

#### isDestructive
#### `disabled`: `boolean`

Renders a red text-based button style to indicate destructive behavior.
Whether the button is disabled. If `true`, this will force a `button` element to be rendered.

- Type: `Boolean`
- Required: No

#### isSmall
#### `focus`: `boolean`

Decreases the size of the button.
Whether the button is focused.

- Type: `Boolean`
- Required: No

#### isPressed
#### `href`: `string`

Renders a pressed button style.
If provided, renders `a` instead of `button`.

- Type: `Boolean`
- Required: No

#### isBusy
#### `icon`: `IconProps< unknown >[ 'icon' ]`

Indicates activity while a action is being performed.
If provided, renders an [Icon](/packages/components/src/icon/README.md) component inside the button.

- Type: `Boolean`
- Required: No

#### focus
#### `iconPosition`: `'left' | 'right'`

Whether the button is focused.
If provided with `icon`, sets the position of icon relative to the `text`. Available options are `left|right`.

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

#### target
#### `iconSize`: `IconProps< unknown >[ 'size' ]`

If provided with `href`, sets the `target` attribute to the `a`.
If provided with `icon`, sets the icon size. Please refer to the [Icon](/packages/components/src/icon/README.md) component for more details regarding the default value of its `size` prop.

- Type: `String`
- Required: No

#### className
#### `isBusy`: `boolean`

An optional additional class name to apply to the rendered button.
Indicates activity while a action is being performed.

- Type: `String`
- Required: No

#### icon
#### `isDestructive`: `boolean`

If provided, renders an [Icon](/packages/components/src/icon/README.md) component inside the button.
Renders a red text-based button style to indicate destructive behavior.

- Type: `String|Function|WPComponent|null`
- Required: No

#### iconSize
#### `isPressed`: `boolean`

If provided with `icon`, sets the icon size. Please refer to the [Icon](/packages/components/src/icon/README.md) component for more details regarding the default value of its `size` prop.
Renders a pressed button style.

- Type: `Number`
- Required: No

#### iconPosition
#### `isSmall`: `boolean`

If provided with `icon`, sets the position of icon relative to the `text`. Available options are `left|right`.
Decreases the size of the button.

- Type: `string`
- Required: No
- Default: `left`

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

If provided, displays the given text inside the button. If the button contains children elements, the text is displayed before them.
Sets the `aria-label` of the component, if none is provided. Sets the Tooltip content if `showTooltip` is provided.

- Type: `String`
- Required: No

#### showTooltip
#### `shortcut`: `string | { display: string; ariaLabel: string; }`

If provided with `showTooltip`, appends the Shortcut label to the tooltip content. If an object is provided, it should contain `display` and `ariaLabel` keys.

- Required: No

#### `showTooltip`: `boolean`

If provided, renders a [Tooltip](/packages/components/src/tooltip/README.md) component for the button.

- Type: `Boolean`
- Required: No

#### tooltipPosition
#### `target`: `string`

If provided with`showTooltip`, sets the position of the tooltip. Please refer to the [Tooltip](/packages/components/src/tooltip/README.md) component for more details regarding the defaults.
If provided with `href`, sets the `target` attribute to the `a`.

- Type: `String`
- Require: No
- Required: No

#### shortcut
#### `text`: `string`

If provided with `showTooltip`, appends the Shortcut label to the tooltip content. If an `Object` is provided, it should contain `display` and `ariaLabel` keys.
If provided, displays the given text inside the button. If the button contains children elements, the text is displayed before them.

- Type: `String|Object`
- Required: No

#### label
#### `tooltipPosition`: `PopoverProps[ 'position' ]`

Sets the `aria-label` of the component, if none is provided. Sets the Tooltip content if `showTooltip` is provided.
If provided with`showTooltip`, sets the position of the tooltip. Please refer to the [Tooltip](/packages/components/src/tooltip/README.md) component for more details regarding the defaults.

- Required: No

#### `variant`: `'primary' | 'secondary' | 'tertiary' | 'link'`

Specifies the button's style. The accepted values are `'primary'` (the primary button styles), `'secondary'` (the default button styles), `'tertiary'` (the text-based button styles), and `'link'` (the link button styles).

- Type: `String`
- Required: No

## Related components
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// @ts-nocheck
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';

/**
* WordPress dependencies
*/
Expand All @@ -8,9 +12,20 @@ import { forwardRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import Button from '../button';
import Button from '.';
import type { DeprecatedIconButtonProps } from './types';

function IconButton( { labelPosition, size, tooltip, label, ...props }, ref ) {
function UnforwardedIconButton(
{
label,
labelPosition,
size,
tooltip,
...props
}: React.ComponentPropsWithoutRef< typeof Button > &
DeprecatedIconButtonProps,
ref: ForwardedRef< any >
) {
deprecated( 'wp.components.IconButton', {
since: '5.4',
alternative: 'wp.components.Button',
Expand All @@ -29,4 +44,4 @@ function IconButton( { labelPosition, size, tooltip, label, ...props }, ref ) {
);
}

export default forwardRef( IconButton );
export default forwardRef( UnforwardedIconButton );
Loading