Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7e79847
Start refactor of ToolsPanel component to typescript
Sep 15, 2021
b1e2fc0
Add typing for dropdown menu
Aug 12, 2021
65fa4f6
Start typing the ToolsPanelHeader component
Sep 15, 2021
74ddbd8
Add types for ToolsPanelHeader
Aug 12, 2021
15c0ae3
Add remaining types
Aug 12, 2021
336efba
More conflict resolution
Sep 15, 2021
a1fe287
Update PolymorphicComponent* references to WordPressComponentProps*
Sep 2, 2021
8d9d8d7
Fix type issues following rebase
Sep 15, 2021
ee69bf8
fix remaining menu item type issues
Sep 15, 2021
1174878
Improve menu item types
Sep 16, 2021
19dec18
Move remaining eligible ToolsPanel files to TS
aaronrobertshaw Sep 17, 2021
bb89386
Revert "Improve menu item types"
aaronrobertshaw Sep 17, 2021
8369760
Revert "Add typing for dropdown menu"
aaronrobertshaw Sep 17, 2021
d853e9a
Remove menu group and item type doc block annotations
aaronrobertshaw Sep 17, 2021
19bdce7
Add ts-nocheck to ToolsPanelHeader
aaronrobertshaw Sep 17, 2021
bce9e12
Move ts-nocheck to dependencies and fix type errors
aaronrobertshaw Sep 20, 2021
87127db
Improve typing of ToolsPanel
aaronrobertshaw Sep 21, 2021
6c4180e
Add missing dependencies to tools panel item hook
aaronrobertshaw Sep 21, 2021
19cd494
Use optional chaining for onSelect and onDeselect calls
aaronrobertshaw Sep 22, 2021
f061db7
Add TypeScript types for function props in READMEs
aaronrobertshaw Sep 22, 2021
d4674f1
Enforce boolean for hasMenuItems in ToolsPanelContext
aaronrobertshaw Sep 22, 2021
1f833ab
Make all properties except panelId mandatory in ToolsPanelContext
aaronrobertshaw Sep 22, 2021
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
Prev Previous commit
Next Next commit
Move ts-nocheck to dependencies and fix type errors
  • Loading branch information
aaronrobertshaw committed Sep 23, 2021
commit bce9e125e661df09bdad1acd66bb536fd87826ef
29 changes: 16 additions & 13 deletions packages/components/src/dropdown-menu/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
Expand Down Expand Up @@ -33,19 +34,21 @@ function mergeProps( defaultProps = {}, props = {} ) {
return mergedProps;
}

function DropdownMenu( {
children,
className,
controls,
icon = menu,
label,
popoverProps,
toggleProps,
menuProps,
disableOpenOnArrowDown = false,
text,
noIcons,
} ) {
function DropdownMenu( dropdownMenuProps ) {
const {
children,
className,
controls,
icon = menu,
label,
popoverProps,
toggleProps,
menuProps,
disableOpenOnArrowDown = false,
text,
noIcons,
} = dropdownMenuProps;

if ( isEmpty( controls ) && ! isFunction( children ) ) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/dropdown/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
Expand Down
9 changes: 3 additions & 6 deletions packages/components/src/menu-group/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
Expand All @@ -9,12 +10,8 @@ import classnames from 'classnames';
import { Children } from '@wordpress/element';
import { useInstanceId } from '@wordpress/compose';

export function MenuGroup( {
children,
className = '',
label,
hideSeparator,
} ) {
export function MenuGroup( props ) {
const { children, className = '', label, hideSeparator } = props;
const instanceId = useInstanceId( MenuGroup );

if ( ! Children.count( children ) ) {
Expand Down
29 changes: 7 additions & 22 deletions packages/components/src/menu-item/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
Expand All @@ -16,34 +17,18 @@ import Shortcut from '../shortcut';
import Button from '../button';
import Icon from '../icon';

/**
* Renders a generic menu item for use inside the more menu.
*
* @param {Object} props Component props.
* @param {WPElement} props.children Element to render as child of button.
* @param {string} props.info Text to use as description for button text.
* @param {string} props.className Class to set on the container.
* @param {WPIcon} props.icon Button's `icon` prop.
* @param {string|Object} props.shortcut Shortcut's `shortcut` prop.
* @param {boolean} props.isSelected Whether or not the menu item is currently selected.
* @param {string} [props.role="menuitem"] ARIA role of the menu item.
* @param {Object} ref React Element ref.
*
* @return {WPComponent} The component to be rendered.
*/
export function MenuItem(
{
export function MenuItem( props, ref ) {
let {
children,
info,
className,
icon,
shortcut,
isSelected,
role = 'menuitem',
...props
},
ref
) {
...buttonProps
} = props;

className = classnames( 'components-menu-item__button', className );

if ( info ) {
Expand Down Expand Up @@ -72,7 +57,7 @@ export function MenuItem(
}
role={ role }
className={ className }
{ ...props }
{ ...buttonProps }
>
<span className="components-menu-item__item">{ children }</span>
<Shortcut
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/navigable-container/container.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/navigable-container/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* Internal Dependencies
*/
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/navigable-container/menu.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/navigable-container/tabbable.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* WordPress dependencies
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/tools-panel/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import { createContext, useContext } from '@wordpress/element';
import type { TPContext } from './types';

export const ToolsPanelContext = createContext< TPContext >( {} );
export const useToolsPanelContext = () => useContext( ToolsPanelContext );
export const useToolsPanelContext = () =>
useContext< TPContext >( ToolsPanelContext );
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// This can be removed once typing has been completed for DropdownMenu,
// MenuGroup & MenuItem.
// @ts-nocheck

/**
* External dependencies
*/
Expand All @@ -22,9 +18,14 @@ import MenuGroup from '../../menu-group';
import MenuItem from '../../menu-item';
import { useToolsPanelHeader } from './hook';
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import type { ToolsPanelHeaderProps } from '../types';
import type {
ToolsPanelControlsGroupProps,
ToolsPanelHeaderProps,
} from '../types';

const DefaultControlsGroup = ( props: ToolsPanelControlsGroupProps ) => {
const { items, onClose, toggleItem } = props;

const DefaultControlsGroup = ( { items, onClose, toggleItem } ) => {
if ( ! items.length ) {
return null;
}
Expand Down Expand Up @@ -62,7 +63,9 @@ const DefaultControlsGroup = ( { items, onClose, toggleItem } ) => {
);
};

const OptionalControlsGroup = ( { items, onClose, toggleItem } ) => {
const OptionalControlsGroup = ( props: ToolsPanelControlsGroupProps ) => {
const { items, onClose, toggleItem } = props;

if ( ! items.length ) {
return null;
}
Expand Down Expand Up @@ -132,7 +135,7 @@ const ToolsPanelHeader = (
label={ labelText }
menuProps={ { className: dropdownMenuClassName } }
>
{ ( { onClose } ) => (
{ ( { onClose = () => undefined } ) => (
<>
<DefaultControlsGroup
items={ defaultItems }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export function useToolsPanelItem(
const {
panelId: currentPanelId,
menuItems,
registerPanelItem,
deregisterPanelItem,
flagItemCustomization,
registerPanelItem = () => undefined,
deregisterPanelItem = () => undefined,
flagItemCustomization = () => undefined,
isResetting,
} = useToolsPanelContext();

Expand Down
59 changes: 42 additions & 17 deletions packages/components/src/tools-panel/tools-panel/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import { useEffect, useMemo, useRef, useState } from '@wordpress/element';
import * as styles from '../styles';
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import { useCx } from '../../utils/hooks/use-cx';
import type { ToolsPanelProps, ToolsPanelItem } from '../types';

const generateMenuItems = ( { panelItems, reset } ) => {
const menuItems = { default: {}, optional: {} };

panelItems.forEach( ( { hasValue, isShownByDefault, label } ) => {
import type {
ToolsPanelItem,
ToolsPanelMenuItemKey,
ToolsPanelMenuItems,
ToolsPanelMenuItemsConfig,
ToolsPanelProps,
} from '../types';

const generateMenuItems = ( config: ToolsPanelMenuItemsConfig ) => {
const { panelItems, reset } = config;
const menuItems: ToolsPanelMenuItems = { default: {}, optional: {} };

panelItems.forEach( ( panelItem: ToolsPanelItem ) => {
const { hasValue, isShownByDefault, label } = panelItem;
const group = isShownByDefault ? 'default' : 'optional';
menuItems[ group ][ label ] = reset ? false : hasValue();
} );
Expand Down Expand Up @@ -49,7 +57,7 @@ export function useToolsPanel(
}, [ wasResetting ] );

// Allow panel items to register themselves.
const [ panelItems, setPanelItems ] = useState( [] );
const [ panelItems, setPanelItems ] = useState( <ToolsPanelItem[]>[] );

const registerPanelItem = ( item: ToolsPanelItem ) => {
setPanelItems( ( items ) => [ ...items, item ] );
Expand All @@ -62,7 +70,9 @@ export function useToolsPanel(
// controls, e.g. both panels have a "padding" control, the
// deregistration of the first panel doesn't occur until after the
// registration of the next.
const index = panelItems.findIndex( ( item ) => item.label === label );
const index = panelItems.findIndex(
( item: ToolsPanelItem ) => item.label === label
);

if ( index !== -1 ) {
setPanelItems( ( items ) => items.splice( index, 1 ) );
Expand All @@ -73,7 +83,10 @@ export function useToolsPanel(
// This is intended for use with default panel items. They are displayed
// separately to optional items and have different display states,
//.we need to update that when their value is customized.
const flagItemCustomization = ( label: string, group = 'default' ) => {
const flagItemCustomization = (
label: string,
group: ToolsPanelMenuItemKey = 'default'
) => {
setMenuItems( {
...menuItems,
[ group ]: {
Expand All @@ -84,12 +97,15 @@ export function useToolsPanel(
};

// Manage and share display state of menu items representing child controls.
const [ menuItems, setMenuItems ] = useState( {} );
const [ menuItems, setMenuItems ] = useState< ToolsPanelMenuItems >( {
default: {},
optional: {},
} );

const getResetAllFilters = () => {
const filters = [];
const filters: Array< Function > = [];

panelItems.forEach( ( item ) => {
panelItems.forEach( ( item: ToolsPanelItem ) => {
if ( item.resetAllFilter ) {
filters.push( item.resetAllFilter );
}
Expand All @@ -99,28 +115,37 @@ export function useToolsPanel(

// Setup menuItems state as panel items register themselves.
useEffect( () => {
const items = generateMenuItems( { panelItems, reset: false } );
const items: ToolsPanelMenuItems = generateMenuItems( {
panelItems,
reset: false,
} );
setMenuItems( items );
}, [ panelItems ] );

// Toggle the checked state of a menu item which is then used to determine
// display of the item within the panel.
const toggleItem = ( label: string ) => {
const currentItem = panelItems.find( ( item ) => item.label === label );
const currentItem: ToolsPanelItem | undefined = panelItems.find(
( item: ToolsPanelItem ) => item.label === label
);

if ( ! currentItem ) {
return;
}

const menuGroup = currentItem.isShownByDefault ? 'default' : 'optional';
const menuGroup: ToolsPanelMenuItemKey = currentItem.isShownByDefault
? 'default'
: 'optional';

setMenuItems( {
const newMenuItems: ToolsPanelMenuItems = {
...menuItems,
[ menuGroup ]: {
...menuItems[ menuGroup ],
[ label ]: ! menuItems[ menuGroup ][ label ],
},
} );
};

setMenuItems( newMenuItems );
};

// Resets display of children and executes resetAll callback if available.
Expand Down
19 changes: 18 additions & 1 deletion packages/components/src/tools-panel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,29 @@ export interface ToolsPanelItemProps extends ToolsPanelItem {
resetAllFilter: ResetAllFilter;
}

export type ToolsPanelMenuItemKey = 'default' | 'optional';

export type ToolsPanelMenuItems = {
[ menuItemKey in ToolsPanelMenuItemKey ]: { [ key: string ]: boolean };
};

export interface TPContext {
panelId?: PanelId;
menuItems?: { [ key: string ]: boolean };
menuItems?: ToolsPanelMenuItems;
hasMenuItems?: number;
registerPanelItem?: ( item: ToolsPanelItem ) => void;
deregisterPanelItem?: ( label: Label ) => void;
flagItemCustomization?: ( label: Label ) => void;
isResetting?: boolean;
}

export interface ToolsPanelControlsGroupProps {
items: [ string, boolean ][];
onClose: () => void;
toggleItem: ( label: Label ) => void;
}

export interface ToolsPanelMenuItemsConfig {
panelItems: ToolsPanelItem[];
reset: boolean;
}
6 changes: 6 additions & 0 deletions packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"src/disabled/**/*",
"src/divider/**/*",
"src/draggable/**/*",
"src/dropdown/**/*",
"src/dropdown-menu/**/*",
"src/elevation/**/*",
"src/flex/**/*",
"src/flyout/**/*",
Expand All @@ -41,6 +43,9 @@
"src/item-group/**/*",
"src/input-control/**/*",
"src/icon/**/*",
"src/menu-item/**/*",
"src/menu-group/**/*",
"src/navigable-container/**/*",
"src/number-control/**/*",
"src/popover/**/*",
"src/range-control/**/*",
Expand All @@ -55,6 +60,7 @@
"src/text/**/*",
"src/tip/**/*",
"src/toggle-group-control/**/*",
"src/tools-panel/**/*",
"src/tooltip/**/*",
"src/truncate/**/*",
"src/ui/**/*",
Expand Down