feat: Add support for icons in Dropdown

This commit is contained in:
Aman Harwara
2021-10-01 13:06:00 +05:30
parent 1a92fffcd4
commit 47b49aa7a9
2 changed files with 31 additions and 4 deletions

View File

@@ -27,6 +27,7 @@ type DropdownProps = {
}; };
type ListboxButtonProps = { type ListboxButtonProps = {
icon?: IconType;
value: string | null; value: string | null;
label: string; label: string;
isExpanded: boolean; isExpanded: boolean;
@@ -35,12 +36,20 @@ type ListboxButtonProps = {
const customDropdownButton: FunctionComponent<ListboxButtonProps> = ({ const customDropdownButton: FunctionComponent<ListboxButtonProps> = ({
label, label,
isExpanded, isExpanded,
icon,
}) => ( }) => (
<> <>
<div className="dropdown-selected-label">{label}</div> <div className="sn-dropdown-button-label">
{icon ? (
<div className="flex mr-2">
<Icon type={icon} className="sn-icon--small" />
</div>
) : null}
<div className="dropdown-selected-label">{label}</div>
</div>
<ListboxArrow <ListboxArrow
className={`sn-dropdown-arrow ${ className={`sn-dropdown-arrow ${
isExpanded ? 'dropdown-arrow-rotated' : '' isExpanded ? 'sn-dropdown-arrow-flipped' : ''
}`} }`}
> >
<Icon type="menu-arrow-down" className="sn-icon--small color-grey-1" /> <Icon type="menu-arrow-down" className="sn-icon--small color-grey-1" />
@@ -74,7 +83,16 @@ export const Dropdown: FunctionComponent<DropdownProps> = ({
> >
<ListboxButton <ListboxButton
className="sn-dropdown-button" className="sn-dropdown-button"
children={customDropdownButton} children={({ value, label, isExpanded }) => {
const current = items.find((item) => item.value === value);
const icon = current ? current?.icon : null;
return customDropdownButton({
value,
label,
isExpanded,
...(icon ? { icon } : null),
});
}}
/> />
<ListboxPopover className="sn-dropdown sn-dropdown-popover"> <ListboxPopover className="sn-dropdown sn-dropdown-popover">
<div className="sn-component"> <div className="sn-component">

View File

@@ -69,11 +69,20 @@
@extend .border-solid; @extend .border-solid;
@extend .border-gray-300; @extend .border-gray-300;
@extend .border-1; @extend .border-1;
@extend .min-w-42; @extend .min-w-55;
}
.sn-dropdown-button-label {
@extend .flex;
@extend .items-center;
} }
.sn-dropdown-arrow { .sn-dropdown-arrow {
@extend .flex; @extend .flex;
&.sn-dropdown-arrow-flipped {
transform: rotate(180deg);
}
} }
/** Lesser specificity will give priority to reach's styles */ /** Lesser specificity will give priority to reach's styles */