2023-06-16 19:24:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore - Ignoring because @wordpress/element library does not have type definition for FunctionComponent
|
|
|
|
// eslint-disable-next-line
|
|
|
|
import { FunctionComponent } from '@wordpress/element';
|
|
|
|
|
2023-08-29 06:01:31 +00:00
|
|
|
export type SelectedOption = number | string | null | number[] | string[];
|
2023-06-16 19:24:12 +00:00
|
|
|
|
2023-08-29 06:01:31 +00:00
|
|
|
export interface WithMaybeSelectedOption {
|
2023-06-16 19:24:12 +00:00
|
|
|
selected?: SelectedOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HOC that transforms a single select to a multiple select.
|
|
|
|
*/
|
2023-08-29 06:01:31 +00:00
|
|
|
const withTransformSingleSelectToMultipleSelect = <
|
|
|
|
T extends Record< string, unknown >
|
|
|
|
>(
|
|
|
|
OriginalComponent: FunctionComponent< T & WithMaybeSelectedOption >
|
2023-06-16 19:24:12 +00:00
|
|
|
) => {
|
2023-08-29 06:01:31 +00:00
|
|
|
return ( props: T & WithMaybeSelectedOption ): JSX.Element => {
|
2023-06-16 19:24:12 +00:00
|
|
|
let { selected } = props;
|
|
|
|
selected = selected === undefined ? null : selected;
|
|
|
|
const isNil = selected === null;
|
|
|
|
|
|
|
|
return Array.isArray( selected ) ? (
|
|
|
|
<OriginalComponent { ...props } />
|
|
|
|
) : (
|
|
|
|
<OriginalComponent
|
|
|
|
{ ...props }
|
|
|
|
selected={ isNil ? [] : [ selected ] }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withTransformSingleSelectToMultipleSelect;
|