Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pages/column-layout/permutations.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ const permutations = createPermutations<ColumnLayoutProps>([
variant: ['text-grid'],
columns: [1, 2, 3, 4],
},
// Above 4 columns, ColumnLayout renders through FlexibleColumnLayout instead of
// GridColumnLayout, so these permutations cover that second renderer.
{
variant: ['default'],
columns: [5, 7],
},
{
variant: ['text-grid'],
columns: [5, 6],
},
]);

export default function ColumnLayoutPermutationsPage() {
Expand Down
10 changes: 0 additions & 10 deletions pages/key-value-pairs/permutations.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,6 @@ const permutations = createPermutations<KeyValuePairsProps>([
],
],
},
{
columns: [5, 6],
minColumnWidth: [undefined, 100],
items: [
Array.from({ length: 7 }, (_, i) => ({
label: `Label for key ${i + 1}`,
value: `Value ${i + 1}`,
})),
],
},
]);

export default function KeyValueScenario() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10511,7 +10511,7 @@ exports[`Components definition for column-layout matches the snapshot: column-la
"defaultValue": "'none'",
"description": "Controls whether dividers are placed between rows and columns.

Note: This is not supported when used with \`minColumnWidth\`, or with more than 4 columns.",
Note: This is not supported when used with \`minColumnWidth\`.",
"inlineType": {
"name": "ColumnLayoutProps.Borders",
"type": "union",
Expand All @@ -10536,14 +10536,7 @@ Note: This is not supported when used with \`minColumnWidth\`, or with more than
{
"defaultValue": "1",
"description": "Specifies the number of columns in each grid row.

The number of columns is reduced as the available space narrows. One of two layout systems is
used, and which one applies determines how that reduction works:
* Grid - used for up to 4 columns when \`minColumnWidth\` is not set. Columns are reduced at
fixed breakpoints, to 2 and then to 1 as the container narrows. \`borders\` are supported.
* Flexible grid - used in all other cases. Columns are reduced based on the available width
and the desired column width, which is \`minColumnWidth\` when set and 150px otherwise.
\`borders\` are not supported.",
When \`minColumnWidth\` is not set, only up to 4 columns are supported.",
"name": "columns",
"optional": true,
"type": "number",
Expand All @@ -10568,10 +10561,7 @@ use the \`id\` attribute, consider setting it on a parent element instead.",
"description": "Use together with \`columns\` to specify the desired minimum width for each column in pixels.

The number of columns is determined by the value of this property, the available space,
and the maximum number of columns as defined by the \`columns\` property.

While reducing the count, odd numbers are skipped, so 5 columns becomes 4 and then 2.
A requested odd count is kept when it fits.",
and the maximum number of columns as defined by the \`columns\` property.",
"name": "minColumnWidth",
"optional": true,
"type": "number",
Expand Down Expand Up @@ -18715,9 +18705,8 @@ Don't use \`ariaLabel\` and \`ariaLabelledby\` at the same time.",
},
{
"defaultValue": "1",
"description": "Specifies the number of columns in each grid row. It defaults to 1.

The number of columns is reduced as the available space narrows, based on \`minColumnWidth\`.",
"description": "Specifies the number of columns in each grid row.
Valid values are any integer between 1 and 4. It defaults to 1.",
"name": "columns",
"optional": true,
"type": "number",
Expand Down Expand Up @@ -18757,10 +18746,7 @@ Each group definition has the following properties:

The number of columns is determined by the value of this property, the available space,
and the maximum number of columns as defined by the \`columns\` property.
If not set, defaults to 150.

While reducing the count, odd numbers are skipped, so 5 columns becomes 4 and then 2.
A requested odd count is kept when it fits.",
If not set, defaults to 150.",
"name": "minColumnWidth",
"optional": true,
"type": "number",
Expand Down
25 changes: 15 additions & 10 deletions src/column-layout/__tests__/column-layout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { warnOnce } from '@cloudscape-design/component-toolkit/internal';
import ColumnLayout, { ColumnLayoutProps } from '../../../lib/components/column-layout';
import createWrapper from '../../../lib/components/test-utils/dom';

import cssGridStyles from '../../../lib/components/column-layout/flexible-column-layout/styles.css.js';
import styles from '../../../lib/components/column-layout/styles.css.js';

jest.mock('@cloudscape-design/component-toolkit/internal', () => ({
Expand All @@ -35,23 +34,29 @@ describe('ColumnLayout component', () => {

[2, 3, 4].forEach(columnCount => {
it(`can have ${columnCount} columns`, () => {
const wrapper = renderColumnLayout({ columns: columnCount });
const wrapper = renderColumnLayout({ columns: columnCount as 2 | 3 | 4 });
expect(wrapper.getElement()).toHaveClass(styles[`grid-columns-${columnCount}`]);
});
});

[5, 6, 12].forEach(columnCount => {
it(`renders ${columnCount} columns through FlexibleColumnLayout`, () => {
const { container } = render(<ColumnLayout columns={columnCount} />);
// Above 4 columns, GridColumnLayout must not be used: findGrid() targets its root.
expect(createWrapper(container).findGrid()).toBeNull();
expect(container.querySelector(`.${cssGridStyles['css-grid']}`)).not.toBeNull();
[1, 2, 3, 4].forEach(columnCount => {
it(`does not warn for ${columnCount} columns`, () => {
render(<ColumnLayout columns={columnCount} />);
expect(warnOnce).not.toHaveBeenCalled();
});
});

[1, 4, 5, 12, 20].forEach(columnCount => {
it(`does not warn for ${columnCount} columns`, () => {
[5, 6, 10, 12].forEach(columnCount => {
it(`warns that ${columnCount} columns is unsupported without minColumnWidth`, () => {
render(<ColumnLayout columns={columnCount} />);
expect(warnOnce).toHaveBeenCalledWith(
'ColumnLayout',
expect.stringContaining(`\`columns\` (${columnCount}) is not supported without \`minColumnWidth\``)
);
});

it(`does not warn for ${columnCount} columns when minColumnWidth is set`, () => {
render(<ColumnLayout columns={columnCount} minColumnWidth={100} />);
expect(warnOnce).not.toHaveBeenCalled();
});
});
Expand Down
40 changes: 2 additions & 38 deletions src/column-layout/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
calculcateCssColumnCount,
DEFAULT_MIN_COLUMN_WIDTH,
} from '../../../lib/components/column-layout/flexible-column-layout';
import { calculcateCssColumnCount } from '../../../lib/components/column-layout/flexible-column-layout';

describe('ColumnLayout calculcateCssColumnCount', () => {
it('returns desired number of columns when container width is not defined', () => {
Expand All @@ -29,7 +26,7 @@ describe('ColumnLayout calculcateCssColumnCount', () => {
expect(calculcateCssColumnCount(3, 200, 220)).toBe(1);
});

it('supports more than 4 columns when minColumnWidth is set', () => {
it('supports more than 4 columns', () => {
// all fit into 10 columns
expect(calculcateCssColumnCount(10, 50, 900)).toBe(10);

Expand All @@ -39,36 +36,3 @@ describe('ColumnLayout calculcateCssColumnCount', () => {
expect(calculcateCssColumnCount(10, 50, 90)).toBe(1);
});
});

describe('ColumnLayout column count with the assumed default width', () => {
// Mirrors what the renderer does when `minColumnWidth` is not set: the same calculation runs with
// DEFAULT_MIN_COLUMN_WIDTH supplied in its place, so there is one reduction rule, not two.
const withDefault = (columns: number, containerWidth: number | null) =>
calculcateCssColumnCount(columns, DEFAULT_MIN_COLUMN_WIDTH, containerWidth);

it('renders the requested count before the container has been measured', () => {
expect(withDefault(12, null)).toBe(12);
expect(withDefault(5, null)).toBe(5);
});

it('steps the count down as the container narrows', () => {
expect(withDefault(12, 1840)).toBe(12);
expect(withDefault(12, 1200)).toBe(8);
expect(withDefault(12, 1000)).toBe(6);
expect(withDefault(12, 700)).toBe(4);
expect(withDefault(12, 450)).toBe(2);
expect(withDefault(12, 300)).toBe(2);
expect(withDefault(12, 140)).toBe(1);
});

it('skips odd counts while reducing, so 13 items wrap as 12-1, 8-5, 6-6-1, then 4-4-4-1', () => {
// floor(750/150) is 5 and floor(1100/150) is 7, both rounded down to the next even count.
expect(withDefault(12, 750)).toBe(4);
expect(withDefault(12, 1100)).toBe(6);
});

it('leaves an odd requested count alone when it fits', () => {
expect(withDefault(5, 800)).toBe(5);
expect(withDefault(7, 1100)).toBe(7);
});
});
36 changes: 0 additions & 36 deletions src/column-layout/__tests__/with-css.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,4 @@ describe('ColumnLayout (with CSS grid) component', () => {

expect(getGridColumns()).toBe('repeat(4, minmax(0, 1fr))');
});

it('supports more than 4 columns', () => {
const { getGridColumns } = renderColumnLayout({
minColumnWidth: 100,
columns: 5,
children: Array.from({ length: 5 }, (_, i) => <div key={i} />),
});

expect(getGridColumns()).toBe('repeat(5, minmax(0, 1fr))');
});

it('marks the first item of each row above 4 columns', () => {
// 5 columns, 7 items: row 1 is items 0-4, row 2 is items 5-6.
const { wrapper } = renderColumnLayout({
minColumnWidth: 100,
columns: 5,
children: Array.from({ length: 7 }, (_, i) => <div key={i} />),
});

const firstColumnIndexes = wrapper
.findAll(`.${styles.item}`)
.map((item, i) => (item.getElement().classList.contains(styles['first-column']) ? i : -1))
.filter(i => i !== -1);

expect(firstColumnIndexes).toEqual([0, 5]);
});

it('collapses responsively without minColumnWidth', () => {
// The mocked container width of 500 resolves to the xxs breakpoint, which caps at 2 columns.
const { getGridColumns } = renderColumnLayout({
columns: 5,
children: Array.from({ length: 5 }, (_, i) => <div key={i} />),
});

expect(getGridColumns()).toBe('repeat(2, minmax(0, 1fr))');
});
});
11 changes: 2 additions & 9 deletions src/column-layout/flexible-column-layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,17 @@ export function calculcateCssColumnCount(
);
}

/**
* Column width assumed when `minColumnWidth` is not set. Without a value to divide the container
* width by, the count could not be reduced at all, so the layout would keep every requested column
* at any width. Matches the default KeyValuePairs passes to this renderer.
*/
export const DEFAULT_MIN_COLUMN_WIDTH = 150;

interface FlexibleColumnLayoutProps
extends Pick<
InternalColumnLayoutProps,
'minColumnWidth' | 'columns' | 'variant' | 'disableGutters' | '__tagOverride'
'minColumnWidth' | 'columns' | 'variant' | 'borders' | 'disableGutters' | '__tagOverride'
> {
children: React.ReactNode;
}

export default function FlexibleColumnLayout({
columns = 1,
minColumnWidth = DEFAULT_MIN_COLUMN_WIDTH,
minColumnWidth = 0,
disableGutters,
variant,
children,
Expand Down
14 changes: 2 additions & 12 deletions src/column-layout/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ import { BaseComponentProps } from '../types/base-component';
export interface ColumnLayoutProps extends BaseComponentProps {
/**
* Specifies the number of columns in each grid row.
*
* The number of columns is reduced as the available space narrows. One of two layout systems is
* used, and which one applies determines how that reduction works:
* * Grid - used for up to 4 columns when `minColumnWidth` is not set. Columns are reduced at
* fixed breakpoints, to 2 and then to 1 as the container narrows. `borders` are supported.
* * Flexible grid - used in all other cases. Columns are reduced based on the available width
* and the desired column width, which is `minColumnWidth` when set and 150px otherwise.
* `borders` are not supported.
* When `minColumnWidth` is not set, only up to 4 columns are supported.
*/
columns?: number;

Expand All @@ -26,7 +19,7 @@ export interface ColumnLayoutProps extends BaseComponentProps {
/**
* Controls whether dividers are placed between rows and columns.
*
* Note: This is not supported when used with `minColumnWidth`, or with more than 4 columns.
* Note: This is not supported when used with `minColumnWidth`.
*/
borders?: ColumnLayoutProps.Borders;

Expand All @@ -40,9 +33,6 @@ export interface ColumnLayoutProps extends BaseComponentProps {
*
* The number of columns is determined by the value of this property, the available space,
* and the maximum number of columns as defined by the `columns` property.
*
* While reducing the count, odd numbers are skipped, so 5 columns becomes 4 and then 2.
* A requested odd count is kept when it fits.
*/
minColumnWidth?: number;

Expand Down
21 changes: 16 additions & 5 deletions src/column-layout/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import React from 'react';
import clsx from 'clsx';

import { warnOnce } from '@cloudscape-design/component-toolkit/internal';

import { getBaseProps } from '../internal/base-component';
import FlexibleColumnLayout from './flexible-column-layout';
import GridColumnLayout from './grid-column-layout';
Expand All @@ -14,10 +16,10 @@ export const COLUMN_TRIGGERS = ['default', 'xxs', 'xs'] as const;
export type ColumnLayoutBreakpoint = (typeof COLUMN_TRIGGERS)[number] | null;

/**
* The 12-column grid renderer can only express column counts that divide 12 evenly, and its
* divider styles are generated per count. Anything above this uses the CSS grid renderer instead.
* Highest `columns` value supported without `minColumnWidth`, as stated in the `columns` docs.
* The grid renderer's column definitions only cover 1 to 4.
*/
export const MAX_GRID_COLUMNS = 4;
const MAX_SUPPORTED_COLUMNS = 4;

/**
* A responsive grid layout.
Expand All @@ -36,13 +38,22 @@ export default function ColumnLayout({
}: InternalColumnLayoutProps) {
const baseProps = getBaseProps(restProps);

const useCssGrid = Boolean(minColumnWidth) || columns > MAX_GRID_COLUMNS;
// this combination type-checks is outside the documented contract and renders
// unexpected behavior: the grid renderer has no column definition above
// MAX_SUPPORTED_COLUMNS
if (columns > MAX_SUPPORTED_COLUMNS && !minColumnWidth) {
warnOnce(
'ColumnLayout',
`\`columns\` (${columns}) is not supported without \`minColumnWidth\`: only up to ${MAX_SUPPORTED_COLUMNS} columns are supported, so the value is ignored and the columns are sized by their content instead. Set \`minColumnWidth\`, or use ${MAX_SUPPORTED_COLUMNS} columns or fewer.`
);
}

return (
<div {...baseProps} className={clsx(baseProps.className, styles['column-layout'])} ref={__internalRootRef}>
{useCssGrid ? (
{minColumnWidth ? (
<FlexibleColumnLayout
columns={columns}
borders={borders}
variant={variant}
minColumnWidth={minColumnWidth}
disableGutters={disableGutters}
Expand Down
34 changes: 15 additions & 19 deletions src/key-value-pairs/__tests__/key-value-pairs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,22 @@ describe('KeyValuePairs', () => {
});
});

describe('columns property', () => {
test.each([1, 2, 3, 4, 5, 6, 12])('does not warn when columns is set to %i', columns => {
describe('warnOnce when columns property exceeds max', () => {
test.each([
{ columns: 1, warnOnceMessage: false },
{ columns: 2, warnOnceCalled: false },
{
columns: 3,
warnOnceCalled: false,
},
{ columns: 4, warnOnceCalled: false },
{
columns: 5,
warnOnceCalled: true,
},
])(`warnOnce called = $warnOnceCalled when columns is set to $columns`, ({ columns, warnOnceCalled }) => {
renderKeyValuePairs(<KeyValuePairs items={[]} columns={columns} />);
expect(warnOnce).not.toHaveBeenCalled();
});

// Asserts the resolved grid rather than the number of items, because every item renders either
// way
test.each([3, 6, 12])('lays out %i columns', columns => {
const { wrapper } = renderKeyValuePairs(
<KeyValuePairs
columns={columns}
items={Array.from({ length: columns }, (_, index) => ({
label: `Label ${index}`,
value: `Value ${index}`,
}))}
/>
);

expect(wrapper.find('dl')!.getElement().style.gridTemplateColumns).toBe(`repeat(${columns}, minmax(0, 1fr))`);
expect(warnOnce).toHaveBeenCalledTimes(warnOnceCalled ? 1 : 0);
});
});

Expand Down
Loading
Loading