🐛 fix: forward ref the component to fix console error

This commit is contained in:
Wahyu Kurniawan 2024-02-24 13:48:20 +07:00
parent d181e6ba98
commit 410def0750
No known key found for this signature in database
GPG Key ID: 040A1549143A8E33
2 changed files with 81 additions and 69 deletions

View File

@ -1,4 +1,4 @@
import React, { ComponentPropsWithoutRef, useMemo } from 'react';
import React, { forwardRef, ComponentPropsWithoutRef, useMemo } from 'react';
import { Overwrite, UseComboboxGetItemPropsReturnValue } from 'downshift';
import { SelectOption, SelectOrientation } from 'components/shared/Select';
import { selectItemTheme, SelectItemTheme } from './SelectItem.theme';
@ -29,58 +29,70 @@ export interface SelectItemProps
empty?: boolean;
}
export const SelectItem = ({
className,
selected,
option,
orientation,
hovered,
empty,
variant,
...props
}: SelectItemProps) => {
const theme = selectItemTheme({ active: hovered, orientation, variant });
const SelectItem = forwardRef<HTMLLIElement, SelectItemProps>(
(
{
className,
selected,
option,
orientation,
hovered,
empty,
variant,
...props
},
ref,
) => {
const theme = selectItemTheme({ active: hovered, orientation, variant });
const { label, description, leftIcon, rightIcon, disabled } = option;
const { label, description, leftIcon, rightIcon, disabled } = option;
const renderRightIcon = useMemo(() => {
if (rightIcon) {
return cloneIcon(rightIcon, { className: theme.icon() });
} else if (selected) {
const renderRightIcon = useMemo(() => {
if (rightIcon) {
return cloneIcon(rightIcon, { className: theme.icon() });
} else if (selected) {
return (
<CheckRadioIcon
className={cn(theme.icon(), 'text-controls-primary')}
/>
);
}
return null;
}, [rightIcon]);
if (empty) {
return (
<CheckRadioIcon className={cn(theme.icon(), 'text-controls-primary')} />
<li {...props} className={theme.wrapper()}>
No results found
</li>
);
}
return null;
}, [rightIcon]);
if (empty) {
return (
<li {...props} className={theme.wrapper()}>
No results found
<li
{...props}
ref={ref}
className={theme.wrapper({ className })}
data-disabled={disabled}
>
{leftIcon && cloneIcon(leftIcon, { className: theme.icon() })}
<div className={theme.content()}>
<p className={theme.label()} data-disabled={disabled}>
{label}
</p>
{orientation === 'horizontal' && <span className={theme.dot()} />}
{description && (
<p className={theme.description()} data-disabled={disabled}>
{description}
</p>
)}
</div>
{renderRightIcon}
</li>
);
}
},
);
return (
<li
{...props}
className={theme.wrapper({ className })}
data-disabled={disabled}
>
{leftIcon && cloneIcon(leftIcon, { className: theme.icon() })}
<div className={theme.content()}>
<p className={theme.label()} data-disabled={disabled}>
{label}
</p>
{orientation === 'horizontal' && <span className={theme.dot()} />}
{description && (
<p className={theme.description()} data-disabled={disabled}>
{description}
</p>
)}
</div>
{renderRightIcon}
</li>
);
};
SelectItem.displayName = 'SelectItem';
export { SelectItem };

View File

@ -1,4 +1,4 @@
import React, { ComponentPropsWithoutRef } from 'react';
import React, { forwardRef, ComponentPropsWithoutRef } from 'react';
import {
Overwrite,
UseMultipleSelectionGetSelectedItemReturnValue,
@ -35,26 +35,26 @@ export interface SelectValueProps
/**
* The SelectValue component is used to display the selected value of a select component
*/
export const SelectValue = ({
className,
size,
option,
onDelete,
...props
}: SelectValueProps) => {
const theme = selectValueTheme();
const SelectValue = forwardRef<HTMLSpanElement, SelectValueProps>(
({ className, size, option, onDelete, ...props }, ref) => {
const theme = selectValueTheme();
return (
<span {...props} className={theme.wrapper({ className, size })}>
{option.label}
<Button
onClick={() => onDelete?.(option)}
iconOnly
variant="unstyled"
size="xs"
>
<CrossIcon className={theme.icon()} />
</Button>
</span>
);
};
return (
<span {...props} ref={ref} className={theme.wrapper({ className, size })}>
{option.label}
<Button
onClick={() => onDelete?.(option)}
iconOnly
variant="unstyled"
size="xs"
>
<CrossIcon className={theme.icon()} />
</Button>
</span>
);
},
);
SelectValue.displayName = 'SelectValue';
export { SelectValue };