-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(web-react): Introduce Grid Item component #DS-961
- Loading branch information
Showing
16 changed files
with
766 additions
and
12 deletions.
There are no files selected for viewing
18 changes: 11 additions & 7 deletions
18
packages/web-react/docs/stories/examples/LoginForm.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
import React from 'react'; | ||
import { TextField, Checkbox, Button, Container, Stack } from '../../../src/components'; | ||
import { Button, Checkbox, Container, Grid, GridItem, Stack, TextField } from '../../../src/components'; | ||
|
||
export default { | ||
title: 'Examples/Forms', | ||
}; | ||
|
||
export const LoginForm = () => ( | ||
<Container> | ||
<Stack hasSpacing> | ||
<TextField type="text" id="name" label="Name" isFluid /> | ||
<TextField type="password" id="password" label="Password" isFluid /> | ||
<Checkbox id="keep-logged" label="Stay Logged In" /> | ||
<Button isBlock>Login</Button> | ||
</Stack> | ||
<Grid> | ||
<GridItem columnStart={{ mobile: 1, tablet: 5 }} columnEnd={{ mobile: -1, tablet: 9 }}> | ||
<Stack hasSpacing> | ||
<TextField type="text" id="name" label="Name" isFluid /> | ||
<TextField type="password" id="password" label="Password" isFluid /> | ||
<Checkbox id="keep-logged" label="Stay Logged In" /> | ||
<Button isBlock>Login</Button> | ||
</Stack> | ||
</GridItem> | ||
</Grid> | ||
</Container> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { ElementType } from 'react'; | ||
import classNames from 'classnames'; | ||
import { useStyleProps } from '../../hooks'; | ||
import { SpiritGridItemProps } from '../../types'; | ||
import { useGridItemStyleProps } from './useGridItemStyleProps'; | ||
|
||
export const GridItem = <T extends ElementType = 'div'>(props: SpiritGridItemProps<T>): JSX.Element => { | ||
const { elementType: ElementTag = 'div', children, ...restProps } = props; | ||
const { classProps, styleProps: gridItemStyle, props: modifiedProps } = useGridItemStyleProps(restProps); | ||
const { styleProps, props: otherProps } = useStyleProps(modifiedProps); | ||
|
||
const gridItemStyleProps = { | ||
style: { | ||
...styleProps.style, | ||
...gridItemStyle, | ||
}, | ||
}; | ||
|
||
return ( | ||
<ElementTag {...otherProps} {...gridItemStyleProps} className={classNames(classProps, styleProps.className)}> | ||
{children} | ||
</ElementTag> | ||
); | ||
}; | ||
|
||
export default GridItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
packages/web-react/src/components/Grid/__tests__/GridItem.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
import '@testing-library/jest-dom'; | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest'; | ||
import { stylePropsTest } from '../../../../tests/providerTests/stylePropsTest'; | ||
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest'; | ||
import GridItem from '../GridItem'; | ||
|
||
describe('Grid', () => { | ||
classNamePrefixProviderTest(GridItem, 'GridItem'); | ||
|
||
stylePropsTest(GridItem); | ||
|
||
restPropsTest(GridItem, 'div'); | ||
|
||
it('should render text children', () => { | ||
const dom = render(<GridItem>Hello World</GridItem>); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.textContent).toBe('Hello World'); | ||
}); | ||
|
||
it('should have start CSS variable', () => { | ||
const dom = render(<GridItem columnStart={4} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-start')).toBe('4'); | ||
}); | ||
|
||
it('should have start CSS variable for mobile', () => { | ||
const dom = render(<GridItem columnStart={{ mobile: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-start')).toBe('4'); | ||
}); | ||
|
||
it('should have start CSS variable for tablet', () => { | ||
const dom = render(<GridItem columnStart={{ tablet: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-start-tablet')).toBe('4'); | ||
}); | ||
|
||
it('should have start CSS variable for desktop', () => { | ||
const dom = render(<GridItem columnStart={{ desktop: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-start-desktop')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable', () => { | ||
const dom = render(<GridItem columnEnd={4} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-end')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable for mobile', () => { | ||
const dom = render(<GridItem columnEnd={{ mobile: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-end')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable for tablet', () => { | ||
const dom = render(<GridItem columnEnd={{ tablet: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-end-tablet')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable for desktop', () => { | ||
const dom = render(<GridItem columnEnd={{ desktop: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-end-desktop')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable with span', () => { | ||
const dom = render(<GridItem columnEnd="span 4" />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-end')).toBe('span 4'); | ||
}); | ||
|
||
it('should have end CSS variable for mobile with span', () => { | ||
const dom = render(<GridItem columnEnd={{ mobile: 'span 4' }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-end')).toBe('span 4'); | ||
}); | ||
|
||
it('should have end CSS variable for tablet with span', () => { | ||
const dom = render(<GridItem columnEnd={{ tablet: 'span 4' }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-end-tablet')).toBe('span 4'); | ||
}); | ||
|
||
it('should have end CSS variable for desktop with span', () => { | ||
const dom = render(<GridItem columnEnd={{ desktop: 'span 4' }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-end-desktop')).toBe('span 4'); | ||
}); | ||
|
||
it('should have all CSS variables', () => { | ||
const dom = render(<GridItem columnStart={1} columnEnd={2} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-start')).toBe('1'); | ||
expect(element.style.getPropertyValue('--grid-item-column-end')).toBe('2'); | ||
}); | ||
|
||
it('should have mobile, tablet and desktop CSS variables', () => { | ||
const dom = render( | ||
<GridItem columnStart={{ mobile: 1, tablet: 2, desktop: 3 }} columnEnd={{ mobile: 4, tablet: 5, desktop: 6 }} />, | ||
); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-start')).toBe('1'); | ||
expect(element.style.getPropertyValue('--grid-item-column-start-tablet')).toBe('2'); | ||
expect(element.style.getPropertyValue('--grid-item-column-start-desktop')).toBe('3'); | ||
expect(element.style.getPropertyValue('--grid-item-column-end')).toBe('4'); | ||
expect(element.style.getPropertyValue('--grid-item-column-end-tablet')).toBe('5'); | ||
expect(element.style.getPropertyValue('--grid-item-column-end-desktop')).toBe('6'); | ||
}); | ||
|
||
it('should have start CSS variable for row', () => { | ||
const dom = render(<GridItem rowStart={4} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-start')).toBe('4'); | ||
}); | ||
|
||
it('should have start CSS variable for mobile row', () => { | ||
const dom = render(<GridItem rowStart={{ mobile: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-start')).toBe('4'); | ||
}); | ||
|
||
it('should have start CSS variable for tablet row', () => { | ||
const dom = render(<GridItem rowStart={{ tablet: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-start-tablet')).toBe('4'); | ||
}); | ||
|
||
it('should have start CSS variable for desktop row', () => { | ||
const dom = render(<GridItem rowStart={{ desktop: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-start-desktop')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable for row', () => { | ||
const dom = render(<GridItem rowEnd={4} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-end')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable for mobile row', () => { | ||
const dom = render(<GridItem rowEnd={{ mobile: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-end')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable for tablet row', () => { | ||
const dom = render(<GridItem rowEnd={{ tablet: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-end-tablet')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable for desktop row', () => { | ||
const dom = render(<GridItem rowEnd={{ desktop: 4 }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-end-desktop')).toBe('4'); | ||
}); | ||
|
||
it('should have end CSS variable with span for row', () => { | ||
const dom = render(<GridItem rowEnd="span 4" />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-end')).toBe('span 4'); | ||
}); | ||
|
||
it('should have end CSS variable for mobile with span for row', () => { | ||
const dom = render(<GridItem rowEnd={{ mobile: 'span 4' }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-end')).toBe('span 4'); | ||
}); | ||
|
||
it('should have end CSS variable for tablet with span for row', () => { | ||
const dom = render(<GridItem rowEnd={{ tablet: 'span 4' }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-end-tablet')).toBe('span 4'); | ||
}); | ||
|
||
it('should have end CSS variable for desktop with span for row', () => { | ||
const dom = render(<GridItem rowEnd={{ desktop: 'span 4' }} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-end-desktop')).toBe('span 4'); | ||
}); | ||
|
||
it('should have all CSS variables for row', () => { | ||
const dom = render(<GridItem rowStart={1} rowEnd={2} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-start')).toBe('1'); | ||
expect(element.style.getPropertyValue('--grid-item-row-end')).toBe('2'); | ||
}); | ||
|
||
it('should have mobile, tablet and desktop CSS variables for row', () => { | ||
const dom = render( | ||
<GridItem rowStart={{ mobile: 1, tablet: 2, desktop: 3 }} rowEnd={{ mobile: 4, tablet: 5, desktop: 6 }} />, | ||
); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-row-start')).toBe('1'); | ||
expect(element.style.getPropertyValue('--grid-item-row-start-tablet')).toBe('2'); | ||
expect(element.style.getPropertyValue('--grid-item-row-start-desktop')).toBe('3'); | ||
expect(element.style.getPropertyValue('--grid-item-row-end')).toBe('4'); | ||
expect(element.style.getPropertyValue('--grid-item-row-end-tablet')).toBe('5'); | ||
expect(element.style.getPropertyValue('--grid-item-row-end-desktop')).toBe('6'); | ||
}); | ||
|
||
it('should have all CSS variables for row and column', () => { | ||
const dom = render(<GridItem columnStart={1} columnEnd={2} rowStart={1} rowEnd={2} />); | ||
|
||
const element = dom.container.querySelector('div') as HTMLElement; | ||
expect(element.style.getPropertyValue('--grid-item-column-start')).toBe('1'); | ||
expect(element.style.getPropertyValue('--grid-item-column-end')).toBe('2'); | ||
expect(element.style.getPropertyValue('--grid-item-row-start')).toBe('1'); | ||
expect(element.style.getPropertyValue('--grid-item-row-end')).toBe('2'); | ||
}); | ||
}); |
Oops, something went wrong.