Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Disallow invalid number of columns for narrow and medium Grids #1628

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
11 changes: 8 additions & 3 deletions packages/react/src/Breakout/BreakoutCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { forwardRef } from 'react'
import type { HTMLAttributes, PropsWithChildren } from 'react'
import type { BreakoutRowNumber, BreakoutRowNumbers } from './Breakout'
import { breakoutCellClasses } from './breakoutCellClasses'
import type { GridColumnNumber, GridColumnNumbers } from '../Grid/Grid'
import type {
GridColumnNumbers,
GridMediumColumnNumber,
GridNarrowColumnNumber,
GridWideColumnNumber,
} from '../Grid/Grid'

type BreakoutCellSpanAllProp = {
/** Lets the cell span the full width of all grid variants. */
Expand All @@ -21,9 +26,9 @@ type BreakoutCellSpanAllProp = {

type BreakoutCellSpanAndStartProps = {
/** The amount of grid columns the cell spans. */
colSpan?: 'all' | GridColumnNumber | GridColumnNumbers
colSpan?: 'all' | GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers
Copy link
Contributor

@VincentSmedinga VincentSmedinga Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has overlap: 1 | 2 | 3 | 4 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | …

We discussed Pick<>, but that’s for object types, not literals.

This may be an option:

type Subset<T, U> = T extends U ? T : never

export type GridColumnNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12

export type GridColumnNumbers = {
  narrow: Subset<GridColumnNumber, 1 | 2 | 3 | 4>
  medium: Subset<GridColumnNumber, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8>
  wide: GridColumnNumber
}

Types that accept all three variations can keep using GridColumnNumber.

Copy link
Contributor

@VincentSmedinga VincentSmedinga Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an alternative. The definition is more complex, but usage is simpler.

type Range<N extends number, Result extends Array<unknown> = []> = Result['length'] extends N
  ? Result[number]
  : Range<N, [...Result, Result['length']]>

export type GridColumnNumber = Range<12>

export type GridColumnNumbers = {
  narrow: Range<4>
  medium: Range<8>
  wide: Range<12>
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subset seems best?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I like how simple it is to use Range even if the definition is more complex.

I did something similar in the experimental branch:

type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N

Copy link
Contributor Author

@RubenSibon RubenSibon Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end we chose the KISS solution. :-)

/** The index of the grid column the cell starts at. */
colStart?: GridColumnNumber | GridColumnNumbers
colStart?: GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers
has?: 'figure'
}

Expand Down
10 changes: 8 additions & 2 deletions packages/react/src/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import type { ForwardedRef, HTMLAttributes, PropsWithChildren } from 'react'
import { GridCell } from './GridCell'
import { paddingClasses } from './paddingClasses'

export type GridColumnNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
export type GridColumnNumbers = { narrow: GridColumnNumber; medium: GridColumnNumber; wide: GridColumnNumber }
export type GridNarrowColumnNumber = 1 | 2 | 3 | 4
export type GridMediumColumnNumber = GridNarrowColumnNumber | 5 | 6 | 7 | 8
export type GridWideColumnNumber = GridMediumColumnNumber | 9 | 10 | 11 | 12
export type GridColumnNumbers = {
narrow: GridNarrowColumnNumber
medium: GridMediumColumnNumber
wide: GridWideColumnNumber
}
export type GridPaddingSize = 'small' | 'medium' | 'large'

type GridPaddingVerticalProp = {
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/Grid/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import clsx from 'clsx'
import { forwardRef } from 'react'
import type { HTMLAttributes, PropsWithChildren } from 'react'
import type { GridColumnNumber, GridColumnNumbers } from './Grid'
import type { GridColumnNumbers, GridMediumColumnNumber, GridNarrowColumnNumber, GridWideColumnNumber } from './Grid'
import { gridCellClasses } from './gridCellClasses'

type GridCellSpanAllProp = {
Expand All @@ -16,9 +16,9 @@ type GridCellSpanAllProp = {

type GridCellSpanAndStartProps = {
/** The amount of grid columns the cell spans. */
span?: GridColumnNumber | GridColumnNumbers
span?: GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers
/** The index of the grid column the cell starts at. */
start?: GridColumnNumber | GridColumnNumbers
start?: GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers
}

export type GridCellProps = {
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/Grid/gridCellClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* Copyright Gemeente Amsterdam
*/

import type { GridColumnNumbers, GridMediumColumnNumber, GridNarrowColumnNumber, GridWideColumnNumber } from './Grid'
import type { GridCellProps } from './GridCell'

export const addGridClass = (
prefix: string,
value?: number | { narrow: number; medium: number; wide: number } | 'all',
value?: GridNarrowColumnNumber | GridMediumColumnNumber | GridWideColumnNumber | GridColumnNumbers | 'all',
): string[] => {
if (value === 'all' || typeof value === 'number') {
return [`${prefix}${value}`]
Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/Grid/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export { Grid } from './Grid'
export type { GridProps, GridColumnNumber, GridColumnNumbers } from './Grid'
export type {
GridColumnNumbers,
GridMediumColumnNumber,
GridNarrowColumnNumber,
GridProps,
GridWideColumnNumber,
} from './Grid'
export type { GridCellProps } from './GridCell'