/** * WordPress dependencies */ import { BaseControl } from '@safe-wordpress/components'; import { useInstanceId } from '@safe-wordpress/compose'; import * as icons from '@safe-wordpress/icons'; const { Icon: _, // Deprecated aliases warning: __, ...availableIcons } = icons; /** * External dependencies */ import clsx from 'clsx'; import { OptionProps, SingleValueProps } from 'react-select'; import { find } from 'lodash'; /** * Internal dependencies */ import { StylizedSelectControl } from '../stylized-select-control'; import './style.scss'; type IconSelectorProps = { readonly className?: string; readonly label?: string; readonly help?: string; readonly icon?: string; readonly onChange: ( icon?: string ) => void; }; export const IconSelector = ( { className, label, help, icon, onChange, }: IconSelectorProps ): JSX.Element | null => { const instanceId = useInstanceId( IconSelector ); const iconValue = !! icon ? find( ICON_OPTIONS, { value: icon } ) : undefined; return ( options={ ICON_OPTIONS } value={ iconValue } onChange={ ( option ) => onChange( option?.value ) } styles={ { menuList: ( provided ) => ( { ...provided, maxHeight: 150, } ), } } components={ { Option: IconOption, SingleValue: IconSingleValue, } } /> ); }; type IconData = { icon: JSX.Element; value: string; label: string; }; const ICON_OPTIONS: ReadonlyArray< IconData > = Object.entries( availableIcons ).map( ( [ name, icon ] ) => ( { icon, value: name, label: name, } ) ); const IconOption = ( { data: { label, icon }, isFocused, isSelected, innerRef, innerProps, }: OptionProps< IconData > ): JSX.Element => (
{ icon }
{ label }
); const IconSingleValue = ( { data: { label, icon }, innerProps, }: SingleValueProps< IconData > ): JSX.Element => (
{ icon }
{ label }
);