🐛(react) disable button link
According to its prop, the Button component can be rendered as a link but in this case, the prop `disabled` has no effect. So to fix that if the Button receives a `href` prop and is disabled, we apply a modifier class `.c__button--disabled`.
This commit is contained in:
committed by
Jean-Baptiste PENRATH
parent
d976c6f51c
commit
8e049bc21e
5
.changeset/fresh-dots-happen.md
Normal file
5
.changeset/fresh-dots-happen.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@openfun/cunningham-react": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Apply disable style to disabled button link
|
||||||
@@ -32,7 +32,7 @@
|
|||||||
border-color: currentColor;
|
border-color: currentColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
&:disabled {
|
&:disabled, &.c__button--disabled {
|
||||||
background-color: var(--c--theme--colors--greyscale-200);
|
background-color: var(--c--theme--colors--greyscale-200);
|
||||||
color: var(--c--theme--colors--greyscale-600);
|
color: var(--c--theme--colors--greyscale-600);
|
||||||
cursor: not-allowed;
|
cursor: not-allowed;
|
||||||
|
|||||||
@@ -60,11 +60,22 @@ describe("<Button/>", () => {
|
|||||||
</Button>,
|
</Button>,
|
||||||
);
|
);
|
||||||
const button = screen.getByRole("button", { name: "Test button" });
|
const button = screen.getByRole("button", { name: "Test button" });
|
||||||
|
expect(button).toHaveAttribute("disabled");
|
||||||
expect(handleClick).not.toBeCalled();
|
expect(handleClick).not.toBeCalled();
|
||||||
await act(async () => user.click(button));
|
await act(async () => user.click(button));
|
||||||
expect(handleClick).not.toHaveBeenCalled();
|
expect(handleClick).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("renders disabled link", async () => {
|
||||||
|
render(
|
||||||
|
<Button href="https://perdu.com" disabled={true}>
|
||||||
|
Test button link
|
||||||
|
</Button>,
|
||||||
|
);
|
||||||
|
const button = screen.getByRole("link", { name: "Test button link" });
|
||||||
|
expect(button.classList).toContain("c__button--disabled");
|
||||||
|
});
|
||||||
|
|
||||||
it("renders as link when href is used", () => {
|
it("renders as link when href is used", () => {
|
||||||
render(
|
render(
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export const All: Story = {
|
|||||||
<Button {...Tertiary.args} />
|
<Button {...Tertiary.args} />
|
||||||
<Button {...TertiaryText.args} />
|
<Button {...TertiaryText.args} />
|
||||||
<Button {...Disabled.args} />
|
<Button {...Disabled.args} />
|
||||||
|
<Button {...LinkDisabled.args} />
|
||||||
<Button {...Danger.args} />
|
<Button {...Danger.args} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -70,6 +71,15 @@ export const Disabled: Story = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const LinkDisabled: Story = {
|
||||||
|
args: {
|
||||||
|
children: "Disabled",
|
||||||
|
color: "primary",
|
||||||
|
disabled: true,
|
||||||
|
href: "https://perdu.com",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
export const Danger: Story = {
|
export const Danger: Story = {
|
||||||
args: {
|
args: {
|
||||||
children: "Danger",
|
children: "Danger",
|
||||||
|
|||||||
@@ -59,6 +59,9 @@ export const Button = ({
|
|||||||
if (["primary-text", "tertiary-text"].includes(color)) {
|
if (["primary-text", "tertiary-text"].includes(color)) {
|
||||||
classes.push("c__button--text");
|
classes.push("c__button--text");
|
||||||
}
|
}
|
||||||
|
if (props.href && props.disabled) {
|
||||||
|
classes.push("c__button--disabled");
|
||||||
|
}
|
||||||
const iconElement = <span className="c__button__icon">{icon}</span>;
|
const iconElement = <span className="c__button__icon">{icon}</span>;
|
||||||
const tagName = props.href ? "a" : "button";
|
const tagName = props.href ? "a" : "button";
|
||||||
return createElement(
|
return createElement(
|
||||||
|
|||||||
Reference in New Issue
Block a user