snowballtools-base/packages/frontend/src/components/shared/UserSelect/UserSelectItem/UserSelectItem.tsx
2024-02-29 13:58:45 +07:00

67 lines
1.9 KiB
TypeScript

import React, { forwardRef, ComponentPropsWithoutRef } from 'react';
import { Overwrite, UseComboboxGetItemPropsReturnValue } from 'downshift';
import {
userSelectItemTheme,
UserSelectItemTheme,
} from './UserSelectItem.theme';
import { CheckRadioIcon } from 'components/shared/CustomIcon';
import { UserSelectOption } from 'components/shared/UserSelect';
import { OmitCommon } from 'types/common';
/**
* Represents a type that merges ComponentPropsWithoutRef<'li'> with certain exclusions.
* @type {MergedComponentPropsWithoutRef}
*/
type MergedComponentPropsWithoutRef = OmitCommon<
ComponentPropsWithoutRef<'li'>,
Omit<
Overwrite<UseComboboxGetItemPropsReturnValue, UserSelectOption[]>,
'index' | 'item'
>
>;
export interface UserSelectItemProps
extends MergedComponentPropsWithoutRef,
UserSelectItemTheme {
selected: boolean;
option: UserSelectOption;
hovered?: boolean;
}
const UserSelectItem = forwardRef<HTMLLIElement, UserSelectItemProps>(
({ className, selected, hovered, option, ...props }, ref) => {
const theme = userSelectItemTheme();
const { value, label, imgSrc = './logo.svg' } = option;
return (
<li
{...props}
ref={ref}
className={theme.wrapper({ className, active: selected || hovered })}
>
<div className={theme.content()}>
<img src={imgSrc} alt={`${value}-logo`} className={theme.img()} />
<p className={theme.label()}>{label}</p>
</div>
{selected && <CheckRadioIcon className={theme.selectedIcon()} />}
</li>
);
},
);
export const EmptyUserSelectItem = () => {
const theme = userSelectItemTheme();
return (
<li className={theme.wrapper()}>
<div className={theme.content()}>
<p className={theme.label()}>No results found</p>
</div>
</li>
);
};
UserSelectItem.displayName = 'UserSelectItem';
export { UserSelectItem };