Skip to content

Commit dac772c

Browse files
feat(ui): add alignRight to DataGridToolbar (#1515)
* add alignRight * remove unused import * remove unused const * remove search and update story * tidy * tidy * fix storybook
1 parent f357218 commit dac772c

4 files changed

Lines changed: 92 additions & 25 deletions

File tree

.changeset/violet-towns-wear.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@cloudoperators/juno-ui-components": minor
3+
---
4+
5+
- Added `alignRight` prop to `DataGridToolbar` for flexible alignment control, allowing users to customize layout.
6+
- Removed `search` prop, which is a BREAKING CHANGE. All elements should be passed as `children`.

packages/ui-components/src/components/DataGridToolbar/DataGridToolbar.component.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,37 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import React, { HTMLAttributes, ReactElement, ReactNode } from "react"
6+
import React, { HTMLAttributes, ReactNode } from "react"
77

8-
const datagridtoolbarstyles = `
9-
jn:flex
10-
jn:items-center
8+
const baseToolbarStyles = `
119
jn:bg-theme-background-lvl-1
1210
jn:py-3
1311
jn:px-6
1412
jn:mb-px
1513
`
1614

17-
const childrenWrapperStyles = `
18-
jn:ml-auto
19-
`
20-
2115
/**
2216
* `DataGridToolbar` provides an action bar for a `DataGrid`, designed to hold controls like buttons and search inputs
2317
* for performing group operations and interfacing with the grid content.
2418
* @see https://cloudoperators.github.io/juno/?path=/docs/components-datagrid-datagridtoolbar--docs
2519
* @see {@link DataGridToolbarProps}
2620
*/
27-
export const DataGridToolbar = ({ search, className = "", children, ...props }: DataGridToolbarProps): ReactNode => {
21+
export const DataGridToolbar = ({
22+
className = "",
23+
children,
24+
alignRight = true,
25+
...props
26+
}: DataGridToolbarProps): ReactNode => {
27+
const childrenWrapperStyles = alignRight ? "jn:ml-auto" : ""
28+
const alignmentToolbarStyles = alignRight ? "jn:flex jn:items-center" : ""
2829
return (
29-
<div className={`juno-datagrid-toolbar ${datagridtoolbarstyles} ${className}`} {...props}>
30-
{search && <div>{search}</div>}
30+
<div className={`juno-datagrid-toolbar ${baseToolbarStyles} ${alignmentToolbarStyles} ${className}`} {...props}>
3131
<div className={childrenWrapperStyles}>{children}</div>
3232
</div>
3333
)
3434
}
3535

3636
export interface DataGridToolbarProps extends HTMLAttributes<HTMLDivElement> {
37-
/**
38-
* An optional `SearchInput` component for inclusion in the toolbar.
39-
*/
40-
search?: ReactElement
41-
4237
/**
4338
* Elements or components to render within the DataGridToolbar.
4439
*/
@@ -49,4 +44,12 @@ export interface DataGridToolbarProps extends HTMLAttributes<HTMLDivElement> {
4944
* @default ""
5045
*/
5146
className?: string
47+
48+
/**
49+
* Determines whether the children are automatically aligned to the right side within the toolbar.
50+
* When true, applies `ml-auto` to the children wrapper, pushing content right.
51+
* When false, no automatic alignment is applied, allowing for custom layouts.
52+
* @default true
53+
*/
54+
alignRight?: boolean
5255
}

packages/ui-components/src/components/DataGridToolbar/DataGridToolbar.stories.tsx

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
import React from "react"
77
import type { Meta, StoryObj } from "@storybook/react-vite"
88
import { DataGridToolbar, DataGridToolbarProps } from "./index"
9+
import { Pill } from "../Pill"
10+
import { Stack } from "../Stack"
11+
import { SearchInput } from "../SearchInput"
12+
import { ComboBox } from "../ComboBox"
13+
import { ComboBoxOption } from "../ComboBoxOption"
14+
import { Button } from "../Button"
15+
import { NativeSelectOption } from "../NativeSelectOption"
16+
import { NativeSelect } from "../NativeSelect"
17+
import { InputGroup } from "../InputGroup"
918

1019
const meta: Meta<typeof DataGridToolbar> = {
1120
title: "Components/DataGrid/DataGridToolbar",
@@ -17,12 +26,6 @@ const meta: Meta<typeof DataGridToolbar> = {
1726
type: { summary: "ReactNode" },
1827
},
1928
},
20-
search: {
21-
control: false,
22-
table: {
23-
type: { summary: "ReactElement" },
24-
},
25-
},
2629
},
2730
}
2831

@@ -32,15 +35,58 @@ type Story = StoryObj<typeof meta>
3235
export const Default: Story = {
3336
render: (args: DataGridToolbarProps) => (
3437
<DataGridToolbar {...args}>
35-
<button>Add</button>
38+
<Button label="Create" />
3639
</DataGridToolbar>
3740
),
3841
parameters: {
3942
docs: {
4043
description: {
41-
story: "Optional toolbar for use in DataGrid. Use ButtonRow for multiple buttons",
44+
story:
45+
"Demonstrates a simple toolbar layout with children right-aligned by default. Use ButtonRow for multiple buttons.",
46+
},
47+
},
48+
},
49+
}
50+
51+
export const ComplexCustomLayout: Story = {
52+
render: (args: DataGridToolbarProps) => (
53+
<DataGridToolbar {...args}>
54+
<Stack direction="horizontal" distribution="between">
55+
<Stack direction="vertical" gap="4">
56+
<Stack alignment="center" gap="4">
57+
<InputGroup>
58+
<NativeSelect name="Filter" value="category" wrapperClassName="jn:w-full">
59+
<NativeSelectOption value="category" label="Category" />
60+
<NativeSelectOption value="status" label="Status" />
61+
<NativeSelectOption value="priority" label="Priority" />
62+
</NativeSelect>
63+
<ComboBox>
64+
<ComboBoxOption value="Electronics" />
65+
<ComboBoxOption value="Clothing" />
66+
<ComboBoxOption value="Furniture" />
67+
</ComboBox>
68+
</InputGroup>
69+
<Button label="Clear all" variant="subdued" />
70+
</Stack>
71+
<Stack gap="2" wrap>
72+
<Pill pillKey="category" pillValue="electronics" closeable />
73+
<Pill pillKey="status" pillValue="active" closeable />
74+
<Pill pillKey="priority" pillValue="high" closeable />
75+
</Stack>
76+
</Stack>
77+
<SearchInput placeholder="Search items..." />
78+
</Stack>
79+
</DataGridToolbar>
80+
),
81+
args: {
82+
alignRight: false,
83+
},
84+
parameters: {
85+
docs: {
86+
description: {
87+
story:
88+
"Demonstrates a complex toolbar layout with custom styling - children aligned left and search aligned right.",
4289
},
4390
},
4491
},
45-
args: {},
4692
}

packages/ui-components/src/components/DataGridToolbar/DataGridToolbar.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ describe("DataGridToolbar", () => {
2424
expect(screen.getByTestId("23")).toBeInTheDocument()
2525
expect(screen.getByTestId("23")).toHaveAttribute("data-lolol")
2626
})
27+
28+
test("applies jn:ml-auto correctly when alignRight true", () => {
29+
render(<DataGridToolbar data-testid="my-datagridtoolbar" />)
30+
expect(screen.getByTestId("my-datagridtoolbar")).toBeInTheDocument()
31+
expect(screen.getByTestId("my-datagridtoolbar").firstChild).toHaveClass("jn:ml-auto")
32+
})
33+
34+
test("doesn't apply jn:ml-auto correctly when alignRight false", () => {
35+
render(<DataGridToolbar data-testid="my-datagridtoolbar" alignRight={false} />)
36+
expect(screen.getByTestId("my-datagridtoolbar")).toBeInTheDocument()
37+
expect(screen.getByTestId("my-datagridtoolbar").firstChild).not.toHaveClass("jn:ml-auto")
38+
})
2739
})

0 commit comments

Comments
 (0)