Skip to content

Commit

Permalink
CustomSelectControl: support generic props type (#63985)
Browse files Browse the repository at this point in the history
* CustomSelectControl: support generic props type

* chore: update changelog

* chore: tweak changelog

* chore: tweak ordering

* chore: remove export

Co-authored-by: meteorlxy <meteorlxy@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
  • Loading branch information
5 people authored Jul 29, 2024
1 parent b03d835 commit ef1ee6b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Internal

- `DropdownMenuV2`: break menu item help text on multiple lines for better truncation. ([#63916](https://github.com/WordPress/gutenberg/pull/63916)).
- `CustomSelectControl`: Support generic props type ([#63985](https://github.com/WordPress/gutenberg/pull/63985)).

## 28.4.0 (2024-07-24)

Expand Down
12 changes: 7 additions & 5 deletions packages/components/src/custom-select-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import { __, sprintf } from '@wordpress/i18n';
import _CustomSelect from '../custom-select-control-v2/custom-select';
import CustomSelectItem from '../custom-select-control-v2/item';
import * as Styled from '../custom-select-control-v2/styles';
import type { CustomSelectProps } from './types';
import type { CustomSelectProps, CustomSelectOption } from './types';
import { VisuallyHidden } from '../visually-hidden';

function useDeprecatedProps( {
function useDeprecatedProps< T extends CustomSelectOption >( {
__experimentalShowSelectedHint,
...otherProps
}: CustomSelectProps ) {
}: CustomSelectProps< T > ) {
return {
showSelectedHint: __experimentalShowSelectedHint,
...otherProps,
Expand All @@ -35,7 +35,7 @@ function useDeprecatedProps( {
function applyOptionDeprecations( {
__experimentalHint,
...rest
}: CustomSelectProps[ 'options' ][ number ] ) {
}: CustomSelectOption ) {
return {
hint: __experimentalHint,
...rest,
Expand All @@ -51,7 +51,9 @@ function getDescribedBy( currentValue: string, describedBy?: string ) {
return sprintf( __( 'Currently selected: %s' ), currentValue );
}

function CustomSelectControl( props: CustomSelectProps ) {
function CustomSelectControl< T extends CustomSelectOption >(
props: CustomSelectProps< T >
) {
const {
__next40pxDefaultSize = false,
describedBy,
Expand Down
14 changes: 7 additions & 7 deletions packages/components/src/custom-select-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { FocusEventHandler, MouseEventHandler } from 'react';
/**
* The object structure for the options array.
*/
type Option = {
export type CustomSelectOption = {
key: string;
name: string;
style?: React.CSSProperties;
Expand All @@ -24,15 +24,15 @@ type Option = {
/**
* The object returned from the onChange event.
*/
type ChangeObject = {
type CustomSelectChangeObject< T extends CustomSelectOption > = {
highlightedIndex?: number;
inputValue?: string;
isOpen?: boolean;
type?: string;
selectedItem: Option;
selectedItem: T;
};

export type CustomSelectProps = {
export type CustomSelectProps< T extends CustomSelectOption > = {
/**
* Optional classname for the component.
*/
Expand All @@ -55,7 +55,7 @@ export type CustomSelectProps = {
* Function called with the control's internal state changes. The `selectedItem`
* property contains the next selected item.
*/
onChange?: ( newValue: ChangeObject ) => void;
onChange?: ( newValue: CustomSelectChangeObject< T > ) => void;
/**
* A handler for `blur` events on the trigger button.
*
Expand Down Expand Up @@ -83,7 +83,7 @@ export type CustomSelectProps = {
/**
* The list of options that can be chosen from.
*/
options: Array< Option >;
options: Array< T >;
/**
* The size of the control.
*
Expand All @@ -93,7 +93,7 @@ export type CustomSelectProps = {
/**
* Can be used to externally control the value of the control.
*/
value?: Option;
value?: T;
/**
* Use the `showSelectedHint` property instead.
* @deprecated
Expand Down

0 comments on commit ef1ee6b

Please sign in to comment.