Skip to content

Commit

Permalink
Merge pull request #442 from ClickHouse/label_next_to_loading
Browse files Browse the repository at this point in the history
Showing button label next to loading icon
  • Loading branch information
guykoh authored Jul 3, 2024
2 parents 31ac76a + a52747d commit 131d2c7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/Button/Button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export const Playground = {
align: "center",
fillWidth: false,
loading: false,
showLabelWithLoading: false,
},
};
12 changes: 12 additions & 0 deletions src/components/Button/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ describe("Button", () => {
expect(loadingButton.length).toEqual(1);
});

it("given a loading button and showLabelWithLoading, it should render the loading icon and the label", async () => {
const { getAllByTestId, getByTestId } = renderButton({
label: "Button",
loading: true,
showLabelWithLoading: true
});

const loadingButton = getAllByTestId("click-ui-loading-icon");
expect(loadingButton.length).toEqual(1);
expect(getByTestId("click-ui-loading-icon-wrapper")).toHaveTextContent("Button");
});

it("given a non-loading button, it should not render the loading icon", async () => {
const { queryAllByTestId } = renderButton({
label: "Button",
Expand Down
9 changes: 7 additions & 2 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
fillWidth?: boolean;
loading?: boolean;
autoFocus?: boolean;
showLabelWithLoading?: boolean;
}

export const Button = ({
Expand All @@ -28,6 +29,7 @@ export const Button = ({
label,
loading = false,
disabled,
showLabelWithLoading = false,
...delegated
}: ButtonProps) => (
<StyledButton
Expand Down Expand Up @@ -56,12 +58,13 @@ export const Button = ({
/>
)}
{loading && (
<LoadingIconWrapper>
<LoadingIconWrapper data-testid="click-ui-loading-icon-wrapper">
<Icon
name="loading-animated"
data-testid="click-ui-loading-icon"
aria-label="loading"
/>
></Icon>
{showLabelWithLoading? label ?? children : ""}
</LoadingIconWrapper>
)}
</StyledButton>
Expand All @@ -77,6 +80,8 @@ const LoadingIconWrapper = styled.div`
display: flex;
align-content: center;
justify-content: center;
align-items: center;
gap: 0.5rem;
`;

const StyledButton = styled(BaseButton)<{
Expand Down

0 comments on commit 131d2c7

Please sign in to comment.