Skip to content

Commit aa258e4

Browse files
authored
Merge pull request #158 from yaswanth-deriv/yaswanth/FEQ-1913_Add_hoverstyle_prop_to_button
[FEQ]Yaswanth/FEQ_1913/Improve/Add hoverstyle prop to button
2 parents 57a90e0 + 1c6e8b6 commit aa258e4

File tree

4 files changed

+82
-38
lines changed

4 files changed

+82
-38
lines changed

src/components/Button/Button.scss

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ $black-outline-hover: rgba(0, 0, 0, 0.08);
2424
cursor: not-allowed;
2525
}
2626

27+
&__hover--disabled {
28+
pointer-events: none;
29+
cursor: default;
30+
31+
&:hover {
32+
background-color: inherit;
33+
color: inherit;
34+
}
35+
}
36+
2737
&__size {
2838
&--sm {
2939
height: 2.4rem;
@@ -196,4 +206,5 @@ $black-outline-hover: rgba(0, 0, 0, 0.08);
196206
}
197207
}
198208
}
209+
199210
}
+55-38
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,109 @@
1-
import React from 'react';
2-
import { render, screen } from '@testing-library/react';
3-
import { Button } from '..';
1+
import React from "react";
2+
import { render, screen } from "@testing-library/react";
3+
import { Button } from "..";
44

5-
describe('Button component', () => {
6-
it('renders without crashing', () => {
5+
describe("Button component", () => {
6+
it("renders without crashing", () => {
77
render(<Button>Test Button</Button>);
8-
expect(screen.getByRole('button', { name: /test button/i })).toBeInTheDocument();
8+
expect(screen.getByRole("button", { name: /test button/i })).toBeInTheDocument();
99
});
1010

11-
it('applies the "contained" variant classes when variant is "contained"', () => {
11+
it("applies the contained variant classes when variant is contained", () => {
1212
const { container } = render(<Button variant="contained">Test Button</Button>);
13-
expect(container.firstChild).toHaveClass('deriv-button__variant--contained');
13+
expect(container.firstChild).toHaveClass("deriv-button__variant--contained");
1414
});
1515

16-
it('applies the "ghost" variant classes when variant is "ghost"', () => {
16+
it("applies the ghost variant classes when variant is ghost", () => {
1717
const { container } = render(<Button variant="ghost">Test Button</Button>);
18-
expect(container.firstChild).toHaveClass('deriv-button__variant--ghost');
18+
expect(container.firstChild).toHaveClass("deriv-button__variant--ghost");
1919
});
2020

21-
it('applies the "outlined" variant classes when variant is "outlined"', () => {
21+
it("applies the outlined variant classes when variant is outlined", () => {
2222
const { container } = render(<Button variant="outlined">Test Button</Button>);
23-
expect(container.firstChild).toHaveClass('deriv-button__variant--outlined');
23+
expect(container.firstChild).toHaveClass("deriv-button__variant--outlined");
2424
});
2525

26-
it('applies the correct color class based on the "color" prop', () => {
26+
it("applies the correct color class based on the color prop", () => {
2727
const { container } = render(<Button color="primary">Test Button</Button>);
28-
expect(container.firstChild).toHaveClass('deriv-button__color--primary');
28+
expect(container.firstChild).toHaveClass("deriv-button__color--primary");
2929
});
3030

31-
it('applies full width class when "isFullWidth" prop is true', () => {
31+
it("applies full width class when isFullWidth prop is true", () => {
3232
const { container } = render(<Button isFullWidth>Test Button</Button>);
33-
expect(container.firstChild).toHaveClass('deriv-button__full-width');
33+
expect(container.firstChild).toHaveClass("deriv-button__full-width");
3434
});
3535

36-
it('does not apply full width class when "isFullWidth" prop is false', () => {
36+
it("does not apply full width class when isFullWidth prop is false", () => {
3737
const { container } = render(<Button>Test Button</Button>);
38-
expect(container.firstChild).not.toHaveClass('deriv-button__full-width');
38+
expect(container.firstChild).not.toHaveClass("deriv-button__full-width");
3939
});
4040

41-
it('shows loader when "isLoading" prop is true', () => {
41+
it("shows loader when isLoading prop is true", () => {
4242
render(<Button isLoading>Test Button</Button>);
43-
expect(screen.getByTestId('dt_derivs-loader')).toBeInTheDocument();
43+
expect(screen.getByTestId("dt_derivs-loader")).toBeInTheDocument();
4444
});
4545

46-
it('does not show loader when "isLoading" prop is false', () => {
46+
it("does not show loader when isLoading prop is false", () => {
4747
render(<Button>Test Button</Button>);
48-
expect(screen.queryByTestId('loader')).toBeNull();
48+
expect(screen.queryByTestId("loader")).toBeNull();
4949
});
5050

51-
it('disables the button when "disabled" prop is true', () => {
51+
it("disables the button when disabled prop is true", () => {
5252
render(<Button disabled>Test Button</Button>);
53-
expect(screen.getByRole('button', { name: /test button/i })).toBeDisabled();
53+
expect(screen.getByRole("button", { name: /test button/i })).toBeDisabled();
5454
});
5555

56-
it('disables the button when "isLoading" prop is true', () => {
56+
it("disables the button when isLoading prop is true", () => {
5757
render(<Button isLoading>Test Button</Button>);
58-
expect(screen.getByRole('button', { name: /test button/i })).toBeDisabled();
58+
expect(screen.getByRole("button", { name: /test button/i })).toBeDisabled();
5959
});
6060

61-
it('applies the correct size class based on the "size" prop', () => {
61+
it("applies the correct size class based on the size prop", () => {
6262
const { container } = render(<Button size="lg">Test Button</Button>);
63-
expect(container.firstChild).toHaveClass('deriv-button__size--lg');
63+
expect(container.firstChild).toHaveClass("deriv-button__size--lg");
6464
});
6565

66-
it('applies the correct rounded class based on the "rounded" prop', () => {
66+
it("applies the correct rounded class based on the rounded prop", () => {
6767
const { container } = render(<Button rounded="md">Test Button</Button>);
68-
expect(container.firstChild).toHaveClass('deriv-button__rounded--md');
68+
expect(container.firstChild).toHaveClass("deriv-button__rounded--md");
6969
});
7070

71-
it('shows the icon when provided and not loading', () => {
71+
it("shows the icon when provided and not loading", () => {
7272
const Icon = () => <span>Icon</span>;
7373
render(<Button icon={<Icon />}>Test Button</Button>);
74-
expect(screen.getByText('Icon')).toBeInTheDocument();
74+
expect(screen.getByText("Icon")).toBeInTheDocument();
7575
});
7676

77-
it('does not show the icon when "isLoading" prop is true', () => {
77+
it("does not show the icon when isLoading prop is true", () => {
7878
const Icon = () => <span>Icon</span>;
7979
render(<Button isLoading icon={<Icon />}>Test Button</Button>);
80-
expect(screen.queryByText('Icon')).toBeNull();
80+
expect(screen.queryByText("Icon")).toBeNull();
8181
});
8282

83-
it('renders children text when not loading', () => {
83+
it("renders children text when not loading", () => {
8484
render(<Button>Test Button</Button>);
85-
expect(screen.getByRole('button', { name: /test button/i })).toHaveTextContent('Test Button');
85+
expect(screen.getByRole("button", { name: /test button/i })).toHaveTextContent("Test Button");
8686
});
8787

88-
it('does not render children text when "isLoading" prop is true', () => {
88+
it("does not render children text when isLoading prop is true", () => {
8989
render(<Button isLoading>Test Button</Button>);
90-
expect(screen.queryByText('Test Button')).toBeNull();
90+
expect(screen.queryByText("Test Button")).toBeNull();
9191
});
92+
93+
it("hover styles are applied when hideHoverStyles is true", () => {
94+
render(
95+
<Button hideHoverStyles>Hover Button</Button>
96+
);
97+
const button = screen.getByRole("button");
98+
expect(button).toHaveClass("deriv-button__hover--disabled");
99+
});
100+
101+
it("hover styles are not applied when hideHoverStyles is false", () => {
102+
render(
103+
<Button>Hover Button</Button>
104+
);
105+
const button = screen.getByRole("button");
106+
expect(button).not.toHaveClass("deriv-button__hover--disabled");
107+
});
108+
92109
});

src/components/Button/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface ButtonProps extends ComponentProps<"button"> {
1515
isLoading?: boolean;
1616
rounded?: Extract<TGenericSizes, "lg" | "md" | "sm">;
1717
size?: Extract<TGenericSizes, "lg" | "md" | "sm">;
18+
hideHoverStyles?:boolean;
1819
textSize?: ComponentProps<typeof Text>["size"];
1920
variant?: TVariant;
2021
}
@@ -65,6 +66,7 @@ export const Button = ({
6566
isLoading = false,
6667
rounded = "sm",
6768
size = "md",
69+
hideHoverStyles=false,
6870
textSize,
6971
variant = "contained",
7072
...rest
@@ -80,6 +82,7 @@ export const Button = ({
8082
ButtonRounded[rounded],
8183
{
8284
"deriv-button__full-width": isFullWidth,
85+
"deriv-button__hover--disabled": hideHoverStyles,
8386
},
8487
className,
8588
)}

stories/Button.stories.ts

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const meta = {
1515
isLoading: false,
1616
disabled: false,
1717
size: "md",
18+
hideHoverStyles:false,
1819
isFullWidth: false,
1920
rounded: "sm",
2021
type: "button",
@@ -35,6 +36,10 @@ const meta = {
3536
options: ["true", "false"],
3637
control: { type: "boolean" },
3738
},
39+
hideHoverStyles: {
40+
options: ["true", "false"],
41+
control: { type: "boolean" },
42+
},
3843
rounded: {
3944
options: ["sm", "md", "lg"],
4045
control: { type: "radio" },
@@ -85,6 +90,14 @@ export const ContainedPrimary: Story = {
8590
args: { ...meta.args },
8691
};
8792

93+
export const ContainedPrimaryWithNoHover: Story = {
94+
name: "Contained (Primary No Hover Style)",
95+
args: {
96+
...meta.args,
97+
hideHoverStyles:true,
98+
},
99+
};
100+
88101
export const ContainedPrimaryLight: Story = {
89102
name: "Contained (Primary Light)",
90103
args: {

0 commit comments

Comments
 (0)