Skip to content

Commit

Permalink
Grid: Convert component to TypeScript (#41923)
Browse files Browse the repository at this point in the history
* Grid: Convert component to TypeScript

* Update CHANGELOG.md

* Add `columnGap` to readme

* Sort props

* Update types

* Apply suggestions from code review

Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>

* Move changelog entry to unreleased section

Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
  • Loading branch information
Petter Walbø Johnsgård and aaronrobertshaw authored Jul 1, 2022
1 parent 24acf99 commit 4b4d709
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 111 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Internal

- `Grid`: Convert to TypeScript ([#41923](https://github.com/WordPress/gutenberg/pull/41923)).
- `TextHighlight`: Convert to TypeScript ([#41698](https://github.com/WordPress/gutenberg/pull/41698)).
- `TreeSelect`: Refactor away from `_.repeat()` ([#42070](https://github.com/WordPress/gutenberg/pull/42070/)).

Expand Down
52 changes: 33 additions & 19 deletions packages/components/src/grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,70 @@ function Example() {

## Props

##### align

**Type**: `CSS['alignItems']`
##### `align`: `CSS['alignItems']`

Adjusts the block alignment of children.

##### alignment
- Required: No

**Type**: `GridAlignment`
##### `alignment`: `GridAlignment`

Adjusts the horizontal and vertical alignment of children.

##### columns
- Required: No

##### `columnGap`: `CSSProperties['gridColumnGap']`

Adjusts the `grid-column-gap`.

- Required: No

**Type**: `number`,`(number`,`null)[]`
##### `columns`: `number`

Adjusts the number of columns of the `Grid`.

##### gap
- Required: No
- Default: `2`

**Type**: `number`
##### `gap`: `number`

Gap between each child.

##### isInline
- Required: No
- Default: `3`

**Type**: `boolean`
##### `isInline`: `boolean`

Changes the CSS display from `grid` to `inline-grid`.

##### justify
- Required: No

**Type**: `CSS['justifyContent']`
##### `justify`: `CSS['justifyContent']`

Adjusts the inline alignment of children.

##### rows
- Required: No

**Type**: `number`,`(number`,`null)[]`
##### `rowGap`: `CSSProperties['gridRowGap']`

Adjusts the `grid-row-gap`.

- Required: No

##### `rows`: `number`

Adjusts the number of rows of the `Grid`.

##### templateColumns
- Required: No

**Type**: `CSS['gridTemplateColumns']`
##### `templateColumns`: `CSS['gridTemplateColumns']`

Adjusts the CSS grid `template-columns`.

##### templateRows
- Required: No

**Type**: `CSS['gridTemplateRows']`
##### `templateRows`: `CSS['gridTemplateRows']`

Adjusts the CSS grid `template-rows`.

- Required: No
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';

/**
* Internal dependencies
*/
import { contextConnect } from '../ui/context';
import { contextConnect, WordPressComponentProps } from '../ui/context';
import { View } from '../view';
import useGrid from './hook';
import type { GridProps } from './types';

/**
* @param {import('../ui/context').WordPressComponentProps<import('./types').Props, 'div'>} props
* @param {import('react').ForwardedRef<any>} forwardedRef
*/
function Grid( props, forwardedRef ) {
function UnconnectedGrid(
props: WordPressComponentProps< GridProps, 'div' >,
forwardedRef: ForwardedRef< any >
) {
const gridProps = useGrid( props );

return <View { ...gridProps } ref={ forwardedRef } />;
Expand All @@ -18,7 +23,6 @@ function Grid( props, forwardedRef ) {
/**
* `Grid` is a primitive layout component that can arrange content in a grid configuration.
*
* @example
* ```jsx
* import {
* __experimentalGrid as Grid,
Expand All @@ -36,6 +40,6 @@ function Grid( props, forwardedRef ) {
* }
* ```
*/
const ConnectedGrid = contextConnect( Grid, 'Grid' );
export const Grid = contextConnect( UnconnectedGrid, 'Grid' );

export default ConnectedGrid;
export default Grid;
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useContextSystem } from '../ui/context';
import { useContextSystem, WordPressComponentProps } from '../ui/context';
import { getAlignmentProps } from './utils';
import { useResponsiveValue } from '../ui/utils/use-responsive-value';
import CONFIG from '../utils/config-values';
import { useCx } from '../utils/hooks/use-cx';
import type { GridProps } from './types';

/**
* @param {import('../ui/context').WordPressComponentProps<import('./types').Props, 'div'>} props
*/
export default function useGrid( props ) {
export default function useGrid(
props: WordPressComponentProps< GridProps, 'div' >
) {
const {
align,
alignment,
Expand Down
File renamed without changes.
49 changes: 0 additions & 49 deletions packages/components/src/grid/stories/index.js

This file was deleted.

72 changes: 72 additions & 0 deletions packages/components/src/grid/stories/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* Internal dependencies
*/
import { View } from '../../view';
import { Grid } from '..';

const meta: ComponentMeta< typeof Grid > = {
component: Grid,
title: 'Components (Experimental)/Grid',
argTypes: {
as: { control: { type: 'text' } },
align: { control: { type: 'text' } },
children: { control: { type: null } },
columnGap: { control: { type: 'text' } },
columns: {
table: { type: { summary: 'number' } },
control: { type: 'number' },
},
justify: { control: { type: 'text' } },
rowGap: { control: { type: 'text' } },
rows: {
table: { type: { summary: 'number' } },
control: { type: 'number' },
},
templateColumns: { control: { type: 'text' } },
templateRows: { control: { type: 'text' } },
},
parameters: {
controls: {
expanded: true,
},
docs: { source: { state: 'open' } },
},
};
export default meta;

const Item = ( props: { children: string } ) => (
<View
style={ {
borderRadius: 8,
background: '#eee',
padding: 8,
textAlign: 'center',
} }
{ ...props }
/>
);

const Template: ComponentStory< typeof Grid > = ( props ) => (
<Grid { ...props }>
<Item>One</Item>
<Item>Two</Item>
<Item>Three</Item>
<Item>Four</Item>
<Item>Five</Item>
<Item>Six</Item>
<Item>Seven</Item>
<Item>Eight</Item>
</Grid>
);

export const Default: ComponentStory< typeof Grid > = Template.bind( {} );
Default.args = {
alignment: 'bottom',
columns: 4,
gap: 2,
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe( 'props', () => {

test( 'should render gap', () => {
const { container } = render(
<Grid columns="3" gap={ 4 }>
<Grid columns={ 3 } gap={ 4 }>
<View />
<View />
<View />
Expand All @@ -44,7 +44,7 @@ describe( 'props', () => {

test( 'should render custom columns', () => {
const { container } = render(
<Grid columns="7">
<Grid columns={ 7 }>
<View />
<View />
<View />
Expand All @@ -59,7 +59,7 @@ describe( 'props', () => {

test( 'should render custom rows', () => {
const { container } = render(
<Grid rows="7">
<Grid rows={ 7 }>
<View />
<View />
<View />
Expand Down Expand Up @@ -120,7 +120,7 @@ describe( 'props', () => {

test( 'should render isInline', () => {
const { container } = render(
<Grid columns="3" isInline>
<Grid columns={ 3 } isInline>
<View />
<View />
<View />
Expand Down
Loading

0 comments on commit 4b4d709

Please sign in to comment.