Skip to content

Commit

Permalink
feat: second icon on button (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazil-eightfold authored Jan 3, 2023
1 parent fd0e29e commit d06a7aa
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/components/Button/BaseButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
InternalButtonProps,
SplitButton,
} from './';
import { Icon, IconSize } from '../Icon';
import { Icon, IconProps, IconSize } from '../Icon';
import { Badge } from '../Badge';
import { Breakpoints, useMatchMedia } from '../../hooks/useMatchMedia';
import { mergeClasses } from '../../shared/utilities';
Expand Down Expand Up @@ -40,6 +40,7 @@ export const BaseButton: FC<InternalButtonProps> = React.forwardRef(
floatingButtonProps,
htmlType,
iconProps,
prefixIconProps,
id,
onClick,
onContextMenu,
Expand Down Expand Up @@ -76,6 +77,7 @@ export const BaseButton: FC<InternalButtonProps> = React.forwardRef(

const counterExists: boolean = !!counter;
const iconExists: boolean = !!iconProps;
const prefixIconExists: boolean = !!prefixIconProps;
const textExists: boolean = !!text;

const buttonBaseSharedClassNames: string = mergeClasses([
Expand Down Expand Up @@ -205,6 +207,18 @@ export const BaseButton: FC<InternalButtonProps> = React.forwardRef(
/>
);

const getPrefixIcon = (): JSX.Element => (
<Icon
{...prefixIconProps}
classNames={mergeClasses([
styles.icon,
styles.prefixIcon,
prefixIconProps.classNames,
])}
size={getButtonIconSize()}
/>
);

const getButtonContent = (
buttonTextClassNames: string,
text: string
Expand Down Expand Up @@ -232,14 +246,21 @@ export const BaseButton: FC<InternalButtonProps> = React.forwardRef(
style={style}
type={htmlType}
>
{iconExists && !textExists && getButtonIcon()}
{iconExists && prefixIconExists && !textExists && (
<span>
{getButtonIcon()}
{getPrefixIcon()}
</span>
)}
{iconExists && !textExists && !prefixIconExists && getButtonIcon()}
{counterExists && !textExists && !loading && (
<Badge classNames={badgeClassNames}>{counter}</Badge>
)}
{iconExists && textExists && (
<span>
{getButtonIcon()}
{getButtonContent(buttonTextClassNames, text)}
{prefixIconExists && getPrefixIcon()}
</span>
)}
{!iconExists && getButtonContent(buttonTextClassNames, text)}
Expand Down
4 changes: 4 additions & 0 deletions src/components/Button/Button.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export interface ButtonProps extends NativeButtonProps {
* The button icon props.
*/
iconProps?: IconProps;
/**
* The button icon props.
*/
prefixIconProps?: IconProps;
/**
* The button id.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/components/Button/button.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@

.icon + .button-text-large:not(:empty) {
margin-left: $button-spacer-large;

+ .prefix-icon {
margin-left: $button-spacer-large;
}
}

.icon + .button-text-medium:not(:empty) {
margin-left: $button-spacer-medium;

+ .prefix-icon {
margin-left: $button-spacer-medium;
}
}

.icon + .button-text-small:not(:empty) {
margin-left: $button-spacer-small;

+ .prefix-icon {
margin-left: $button-spacer-small;
}
}

&.pill-shape {
Expand Down
26 changes: 26 additions & 0 deletions src/components/Dropdown/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ const Dropdown_Button_Story: ComponentStory<typeof Dropdown> = (args) => {

export const Dropdown_Button = Dropdown_Button_Story.bind({});

const Dropdown_IconButton_Story: ComponentStory<typeof Dropdown> = (args) => {
const [visible, setVisibility] = useState(false);
return (
<Dropdown
{...args}
onVisibleChange={(isVisible) => setVisibility(isVisible)}
>
<DefaultButton
iconProps={{
path: IconName.mdiAccount,
}}
prefixIconProps={{
path: IconName.mdiChevronDown,
rotate: visible ? 180 : 0,
}}
/>
</Dropdown>
);
};

export const Dropdown_IconButton = Dropdown_IconButton_Story.bind({});

const Dropdown_Div_Story: ComponentStory<typeof Dropdown> = (args) => {
const [visible, setVisibility] = useState(false);
return (
Expand Down Expand Up @@ -189,3 +211,7 @@ Dropdown_Button.args = {
Dropdown_Div.args = {
...dropdownArgs,
};

Dropdown_IconButton.args = {
...dropdownArgs,
};

0 comments on commit d06a7aa

Please sign in to comment.