Skip to content

Commit 0450165

Browse files
authored
Merge pull request #1869 from dxc-technology/jialecl/dropdown-icon
Icon implemented in the dropdown
2 parents ead0374 + 5f1b5c4 commit 0450165

File tree

4 files changed

+19
-39
lines changed

4 files changed

+19
-39
lines changed

lib/src/dropdown/Dropdown.stories.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ export default {
1515
component: DxcDropdown,
1616
};
1717

18-
const hamburguerIcon = (
19-
<svg xmlns="http://www.w3.org/2000/svg" height="20" width="20">
20-
<path d="M3 14.5V13h14v1.5Zm0-3.75v-1.5h14v1.5ZM3 7V5.5h14V7Z" />
21-
</svg>
22-
);
2318
const iconSVG = (
2419
<svg viewBox="0 0 24 24" height="24" width="24" fill="currentColor">
2520
<path d="M0 0h24v24H0z" fill="none" />
@@ -31,8 +26,7 @@ const iconSVGLarge = (
3126
<path d="M11 44q-1.2 0-2.1-.9Q8 42.2 8 41V15q0-1.2.9-2.1.9-.9 2.1-.9h5.5v-.5q0-3.15 2.175-5.325Q20.85 4 24 4q3.15 0 5.325 2.175Q31.5 8.35 31.5 11.5v.5H37q1.2 0 2.1.9.9.9.9 2.1v26q0 1.2-.9 2.1-.9.9-2.1.9Zm0-3h26V15h-5.5v4.5q0 .65-.425 1.075Q30.65 21 30 21q-.65 0-1.075-.425-.425-.425-.425-1.075V15h-9v4.5q0 .65-.425 1.075Q18.65 21 18 21q-.65 0-1.075-.425-.425-.425-.425-1.075V15H11v26Zm8.5-29h9v-.5q0-1.9-1.3-3.2Q25.9 7 24 7q-1.9 0-3.2 1.3-1.3 1.3-1.3 3.2ZM11 41V15v26Z" />
3227
</svg>
3328
);
34-
const iconUrl = "https://iconape.com/wp-content/files/yd/367773/svg/logo-linkedin-logo-icon-png-svg.png";
35-
const icons = [iconSVG, iconSVGLarge, iconUrl];
29+
const icons = [iconSVG, iconSVGLarge, "nutrition"];
3630

3731
const defaultOptions: Option[] = [
3832
{
@@ -86,7 +80,7 @@ const optionWithIcon: Option[] = [
8680
{
8781
value: "1",
8882
label: "Ebay",
89-
icon: iconUrl,
83+
icon: "shopping_cart",
9084
},
9185
];
9286

@@ -136,7 +130,7 @@ const Dropdown = () => (
136130
label="Icon after"
137131
options={options}
138132
onSelectOption={(value) => {}}
139-
icon="https://iconape.com/wp-content/files/yd/367773/svg/logo-linkedin-logo-icon-png-svg.png"
133+
icon="shopping_cart"
140134
iconPosition="after"
141135
/>
142136
</ExampleContainer>
@@ -146,20 +140,15 @@ const Dropdown = () => (
146140
</ExampleContainer>
147141
<ExampleContainer>
148142
<Title title="Only icon without caret" theme="light" level={4} />
149-
<DxcDropdown options={options} onSelectOption={(value) => {}} icon={hamburguerIcon} caretHidden />
143+
<DxcDropdown options={options} onSelectOption={(value) => {}} icon="menu" caretHidden />
150144
</ExampleContainer>
151145
<ExampleContainer>
152146
<Title title="Large icon (SVG)" theme="light" level={4} />
153147
<DxcDropdown label="Large icon" options={options} onSelectOption={(value) => {}} icon={iconSVGLarge} />
154148
</ExampleContainer>
155149
<ExampleContainer>
156150
<Title title="Large icon (image)" theme="light" level={4} />
157-
<DxcDropdown
158-
label="Large icon"
159-
options={options}
160-
onSelectOption={(value) => {}}
161-
icon="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Hamburger_icon.svg/2048px-Hamburger_icon.svg.png"
162-
/>
151+
<DxcDropdown label="Large icon" options={options} onSelectOption={(value) => {}} icon="menu" />
163152
</ExampleContainer>
164153
<ExampleContainer>
165154
<Title title="Disabled with icon" theme="light" level={4} />

lib/src/dropdown/Dropdown.tsx

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,7 @@ import useTheme from "../useTheme";
77
import { v4 as uuidv4 } from "uuid";
88
import * as Popover from "@radix-ui/react-popover";
99
import DropdownMenu from "./DropdownMenu";
10-
11-
const upArrowIcon = (
12-
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
13-
<path d="M7 14l5-5 5 5z" />
14-
<path d="M0 0h24v24H0z" fill="none" />
15-
</svg>
16-
);
17-
18-
const downArrowIcon = (
19-
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
20-
<path d="M7 10l5 5 5-5z" />
21-
<path d="M0 0h24v24H0z" fill="none" />
22-
</svg>
23-
);
10+
import DxcIcon from "../icon/Icon";
2411

2512
const useWidth = (target) => {
2613
const [width, setWidth] = useState(0);
@@ -199,12 +186,16 @@ const DxcDropdown = ({
199186
{label && iconPosition === "after" && <DropdownTriggerLabel>{label}</DropdownTriggerLabel>}
200187
{icon && (
201188
<DropdownTriggerIcon disabled={disabled} role={typeof icon === "string" ? undefined : "img"}>
202-
{typeof icon === "string" ? <img src={icon} /> : icon}
189+
{typeof icon === "string" ? <DxcIcon icon={icon} /> : icon}
203190
</DropdownTriggerIcon>
204191
)}
205192
{label && iconPosition === "before" && <DropdownTriggerLabel>{label}</DropdownTriggerLabel>}
206193
</DropdownTriggerContent>
207-
{!caretHidden && <CaretIcon disabled={disabled}>{isOpen ? upArrowIcon : downArrowIcon}</CaretIcon>}
194+
{!caretHidden && (
195+
<CaretIcon disabled={disabled}>
196+
<DxcIcon icon={isOpen ? "arrow_drop_up" : "arrow_drop_down"} />{" "}
197+
</CaretIcon>
198+
)}
208199
</DropdownTrigger>
209200
</Popover.Trigger>
210201
<Popover.Portal>
@@ -317,8 +308,8 @@ const DropdownTriggerLabel = styled.span`
317308
const DropdownTriggerIcon = styled.span<{ disabled: DropdownPropsType["disabled"] }>`
318309
display: flex;
319310
color: ${(props) => (props.disabled ? props.theme.disabledColor : props.theme.buttonIconColor)};
311+
font-size: ${(props) => props.theme.buttonIconSize};
320312
321-
img,
322313
svg {
323314
width: ${(props) => props.theme.buttonIconSize};
324315
height: ${(props) => props.theme.buttonIconSize};
@@ -328,6 +319,7 @@ const DropdownTriggerIcon = styled.span<{ disabled: DropdownPropsType["disabled"
328319
const CaretIcon = styled.span<{ disabled: DropdownPropsType["disabled"] }>`
329320
display: flex;
330321
color: ${(props) => (props.disabled ? props.theme.disabledColor : props.theme.caretIconColor)};
322+
font-size: ${(props) => props.theme.caretIconSize};
331323
332324
svg {
333325
width: ${(props) => props.theme.caretIconSize};

lib/src/dropdown/DropdownMenuItem.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import styled from "styled-components";
33
import { DropdownMenuItemProps } from "./types";
4+
import DxcIcon from "../icon/Icon";
45

56
const DropdownMenuItem = ({
67
id,
@@ -21,7 +22,7 @@ const DropdownMenuItem = ({
2122
{iconPosition === "after" && <DropdownMenuItemLabel>{option.label}</DropdownMenuItemLabel>}
2223
{option.icon && (
2324
<DropdownMenuItemIcon role={typeof option.icon === "string" ? undefined : "img"}>
24-
{typeof option.icon === "string" ? <img src={option.icon} /> : option.icon}
25+
{typeof option.icon === "string" ? <DxcIcon icon={option.icon} /> : option.icon}
2526
</DropdownMenuItemIcon>
2627
)}
2728
{iconPosition === "before" && <DropdownMenuItemLabel>{option.label}</DropdownMenuItemLabel>}
@@ -62,8 +63,8 @@ const DropdownMenuItemLabel = styled.span`
6263
const DropdownMenuItemIcon = styled.div`
6364
display: flex;
6465
color: ${(props) => props.theme.optionIconColor};
66+
font-size: ${(props) => props.theme.optionIconSize};
6567
66-
img,
6768
svg {
6869
width: ${(props) => props.theme.optionIconSize};
6970
height: ${(props) => props.theme.optionIconSize};

lib/src/dropdown/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export type Option = {
1414
*/
1515
label?: string;
1616
/**
17-
* Element or path used as the icon that will be placed next to the
18-
* option label.
17+
* Material Symbol name or SVG element as the icon that will be placed next to the label.
1918
*/
2019
icon?: string | SVG;
2120
/**
@@ -35,8 +34,7 @@ type Props = {
3534
*/
3635
optionsIconPosition?: "before" | "after";
3736
/**
38-
* Element or path used as the icon that will be placed next to the
39-
* dropdown label.
37+
* Material Symbol name or SVG element as the icon that will be placed next to the label.
4038
*/
4139
icon?: string | SVG;
4240
/**

0 commit comments

Comments
 (0)