-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add display modifiers to EuiShowFor; Convert EuiShowFor and EuiHideFo…
…r to TS (#2503)
- Loading branch information
Showing
18 changed files
with
382 additions
and
197 deletions.
There are no files selected for viewing
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
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
55 changes: 55 additions & 0 deletions
55
src/components/responsive/__snapshots__/hide_for.test.tsx.snap
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,55 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`EuiHideFor l is rendered 1`] = ` | ||
<span | ||
class="eui-hideFor--l" | ||
/> | ||
`; | ||
|
||
exports[`EuiHideFor m is rendered 1`] = ` | ||
<span | ||
class="eui-hideFor--m" | ||
/> | ||
`; | ||
|
||
exports[`EuiHideFor renders and copies classes 1`] = ` | ||
<div | ||
class="eui-hideFor--xs" | ||
> | ||
Child | ||
</div> | ||
`; | ||
|
||
exports[`EuiHideFor renders for multiple breakpoints 1`] = ` | ||
<span | ||
class="eui-hideFor--xs eui-hideFor--l" | ||
> | ||
Child | ||
</span> | ||
`; | ||
|
||
exports[`EuiHideFor renders wraps children in a span 1`] = ` | ||
<span | ||
class="eui-hideFor--xs" | ||
> | ||
Child | ||
</span> | ||
`; | ||
|
||
exports[`EuiHideFor s is rendered 1`] = ` | ||
<span | ||
class="eui-hideFor--s" | ||
/> | ||
`; | ||
|
||
exports[`EuiHideFor xl is rendered 1`] = ` | ||
<span | ||
class="eui-hideFor--xl" | ||
/> | ||
`; | ||
|
||
exports[`EuiHideFor xs is rendered 1`] = ` | ||
<span | ||
class="eui-hideFor--xs" | ||
/> | ||
`; |
41 changes: 0 additions & 41 deletions
41
src/components/responsive/__snapshots__/hide_from.test.js.snap
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
src/components/responsive/__snapshots__/show_for.test.js.snap
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
src/components/responsive/__snapshots__/show_for.test.tsx.snap
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,73 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`EuiShowFor block is rendered 1`] = ` | ||
<span | ||
class="eui-showFor--xs--block" | ||
/> | ||
`; | ||
|
||
exports[`EuiShowFor flex is rendered 1`] = ` | ||
<span | ||
class="eui-showFor--xs--flex" | ||
/> | ||
`; | ||
|
||
exports[`EuiShowFor inlineBlock is rendered 1`] = ` | ||
<span | ||
class="eui-showFor--xs--inlineBlock" | ||
/> | ||
`; | ||
|
||
exports[`EuiShowFor l is rendered 1`] = ` | ||
<span | ||
class="eui-showFor--l" | ||
/> | ||
`; | ||
|
||
exports[`EuiShowFor m is rendered 1`] = ` | ||
<span | ||
class="eui-showFor--m" | ||
/> | ||
`; | ||
|
||
exports[`EuiShowFor renders and copies classes 1`] = ` | ||
<div | ||
class="eui-showFor--xs" | ||
> | ||
Child | ||
</div> | ||
`; | ||
|
||
exports[`EuiShowFor renders for multiple breakpoints 1`] = ` | ||
<span | ||
class="eui-showFor--xs eui-showFor--l" | ||
> | ||
Child | ||
</span> | ||
`; | ||
|
||
exports[`EuiShowFor renders wraps children in a span 1`] = ` | ||
<span | ||
class="eui-showFor--xs" | ||
> | ||
Child | ||
</span> | ||
`; | ||
|
||
exports[`EuiShowFor s is rendered 1`] = ` | ||
<span | ||
class="eui-showFor--s" | ||
/> | ||
`; | ||
|
||
exports[`EuiShowFor xl is rendered 1`] = ` | ||
<span | ||
class="eui-showFor--xl" | ||
/> | ||
`; | ||
|
||
exports[`EuiShowFor xs is rendered 1`] = ` | ||
<span | ||
class="eui-showFor--xs" | ||
/> | ||
`; |
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,47 @@ | ||
import React from 'react'; | ||
import { render } from 'enzyme'; | ||
import { requiredProps } from '../../test'; | ||
|
||
import { EuiHideForBreakpoints, EuiHideFor } from './hide_for'; | ||
|
||
const BREAKPOINTS: EuiHideForBreakpoints[] = ['xs', 's', 'm', 'l', 'xl']; | ||
|
||
describe('EuiHideFor', () => { | ||
test('renders wraps children in a span', () => { | ||
const component = render( | ||
<EuiHideFor sizes={['xs']} {...requiredProps}> | ||
Child | ||
</EuiHideFor> | ||
); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); | ||
|
||
test('renders and copies classes', () => { | ||
const component = render( | ||
<EuiHideFor sizes={['xs']}> | ||
<div>Child</div> | ||
</EuiHideFor> | ||
); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); | ||
|
||
BREAKPOINTS.forEach(size => { | ||
test(`${size} is rendered`, () => { | ||
const component = render( | ||
<EuiHideFor sizes={[size]} {...requiredProps} /> | ||
); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
test('renders for multiple breakpoints', () => { | ||
const component = render( | ||
<EuiHideFor sizes={['xs', 'l']}>Child</EuiHideFor> | ||
); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); | ||
}); |
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,45 @@ | ||
import React, { FunctionComponent, ReactElement } from 'react'; | ||
import classNames from 'classnames'; | ||
import { CommonProps } from '../common'; | ||
|
||
export type EuiHideForBreakpoints = 'xs' | 's' | 'm' | 'l' | 'xl'; | ||
|
||
export interface EuiHideForProps { | ||
children?: React.ReactNode; | ||
/** | ||
* List of all the responsive sizes to show the children for. | ||
* Options are `'xs' | 's' | 'm' | 'l' | 'xl'` | ||
*/ | ||
sizes: EuiHideForBreakpoints[]; | ||
} | ||
|
||
const responsiveSizesToClassNameMap = { | ||
xs: 'eui-hideFor--xs', | ||
s: 'eui-hideFor--s', | ||
m: 'eui-hideFor--m', | ||
l: 'eui-hideFor--l', | ||
xl: 'eui-hideFor--xl', | ||
}; | ||
|
||
export const EuiHideFor: FunctionComponent<EuiHideForProps> = ({ | ||
children, | ||
sizes, | ||
}) => { | ||
const utilityClasses = sizes.map(function(item) { | ||
return responsiveSizesToClassNameMap[item]; | ||
}); | ||
|
||
if (React.isValidElement(children)) { | ||
return ( | ||
<React.Fragment> | ||
{React.Children.map(children, (child: ReactElement<CommonProps>) => | ||
React.cloneElement(child, { | ||
className: classNames(child.props.className, utilityClasses), | ||
}) | ||
)} | ||
</React.Fragment> | ||
); | ||
} else { | ||
return <span className={classNames(utilityClasses)}>{children}</span>; | ||
} | ||
}; |
Oops, something went wrong.