forked from elastic/kibana
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI Framework] [K7] KuiTabs component (elastic#13280)
* Add KuiTabs React component.
- Loading branch information
Showing
16 changed files
with
379 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
|
||
import { | ||
KuiTabs, | ||
KuiTab, | ||
} from '../../../../components'; | ||
|
||
class KuiTabsExample extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.tabs = [{ | ||
id: 'cobalt', | ||
name: 'Cobalt', | ||
}, { | ||
id: 'dextrose', | ||
name: 'Dextrose', | ||
}, { | ||
id: 'hydrogen', | ||
name: 'Hydrogen', | ||
}, { | ||
id: 'monosodium_glutammate', | ||
name: 'Monosodium Glutamate', | ||
}]; | ||
|
||
this.state = { | ||
selectedTabId: 'cobalt', | ||
}; | ||
} | ||
|
||
onSelectedTabChanged = id => { | ||
this.setState({ | ||
selectedTabId: id, | ||
}); | ||
} | ||
|
||
renderTabs() { | ||
return this.tabs.map((tab,index) => ( | ||
<KuiTab | ||
onClick={() => this.onSelectedTabChanged(tab.id)} | ||
isSelected={tab.id === this.state.selectedTabId} | ||
key={index} | ||
> | ||
{tab.name} | ||
</KuiTab> | ||
)); | ||
} | ||
|
||
render() { | ||
return ( | ||
<KuiTabs> | ||
{this.renderTabs()} | ||
</KuiTabs> | ||
); | ||
} | ||
} | ||
|
||
export default KuiTabsExample; |
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,39 @@ | ||
import React from 'react'; | ||
import { renderToHtml } from '../../services'; | ||
|
||
import { | ||
GuideDemo, | ||
GuidePage, | ||
GuideSection, | ||
GuideSectionTypes, | ||
GuideText, | ||
GuideCode | ||
} from '../../components'; | ||
|
||
import Tabs from './tabs'; | ||
const tabsSource = require('!!raw!./tabs'); | ||
const tabsHtml = renderToHtml(Tabs); | ||
|
||
export default props => ( | ||
<GuidePage title={props.route.name}> | ||
<GuideSection | ||
title="Tabs" | ||
source={[{ | ||
type: GuideSectionTypes.JS, | ||
code: tabsSource, | ||
}, { | ||
type: GuideSectionTypes.HTML, | ||
code: tabsHtml, | ||
}]} | ||
> | ||
<GuideText> | ||
The <GuideCode>KuiTabs</GuideCode> component should have <GuideCode>KuiTab</GuideCode> | ||
components as children. | ||
</GuideText> | ||
|
||
<GuideDemo> | ||
<Tabs /> | ||
</GuideDemo> | ||
</GuideSection> | ||
</GuidePage> | ||
); |
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
21 changes: 21 additions & 0 deletions
21
ui_framework/src/components/tabs/__snapshots__/tab.test.js.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,21 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`KuiTab renders 1`] = ` | ||
<button | ||
aria-label="aria-label" | ||
class="kuiTab testClass1 testClass2" | ||
data-test-subj="test subject string" | ||
> | ||
children | ||
</button> | ||
`; | ||
|
||
exports[`KuiTab renders isSelected 1`] = ` | ||
<button | ||
aria-label="aria-label" | ||
class="kuiTab testClass1 testClass2 kuiTab-isSelected" | ||
data-test-subj="test subject string" | ||
> | ||
children | ||
</button> | ||
`; |
9 changes: 9 additions & 0 deletions
9
ui_framework/src/components/tabs/__snapshots__/tabs.test.js.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,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`KuiTabs renders 1`] = ` | ||
<div | ||
aria-label="aria-label" | ||
class="kuiTabs testClass1 testClass2" | ||
data-test-subj="test subject string" | ||
/> | ||
`; |
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,4 @@ | ||
$tabBackgroundColor: #FFF; | ||
$tabHoverBackgroundColor: #F2F2F2; | ||
|
||
@import "tabs"; |
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,57 @@ | ||
.kuiTabs { | ||
display: flex; | ||
border-bottom: $kuiBorderThin; | ||
} | ||
|
||
.kuiTab { | ||
@include kuiFontSize; | ||
|
||
position: relative; | ||
cursor: pointer; | ||
padding: ($kuiSize - $kuiSizeXS) $kuiSize; | ||
color: $kuiColorDarkShade; | ||
background-color: $kuiColorEmptyShade; | ||
transition: all $kuiAnimSpeedNormal $kuiAnimSlightResistance; | ||
|
||
&:hover:not(.kuiTab-isSelected) { | ||
color: $kuiTextColor; | ||
text-decoration: underline; | ||
} | ||
|
||
&:focus { | ||
background-color: $kuiFocusBackgroundColor; | ||
text-decoration: underline; | ||
} | ||
|
||
&.kuiTab-isSelected { | ||
cursor: default; | ||
color: $kuiColorSecondary; | ||
|
||
&:after { | ||
position: absolute; | ||
bottom: -1px; | ||
left: 0; | ||
content: ' '; | ||
width: 100%; | ||
height: $kuiBorderWidthThick; | ||
background-color: $kuiColorSecondary; | ||
animation: kuiTab $kuiAnimSpeedFast $kuiAnimSlightResistance; | ||
} | ||
} | ||
} | ||
|
||
.kuiTab__content { | ||
display: block; | ||
transition: transform $kuiAnimSpeedFast $kuiAnimSlightBounce; | ||
transform: translateY(0); | ||
} | ||
|
||
|
||
@keyframes kuiTab { | ||
0% { | ||
transform: scaleX(0); | ||
} | ||
100% { | ||
transform: scaleX(1); | ||
} | ||
} |
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,2 @@ | ||
export { KuiTab } from './tab'; | ||
export { KuiTabs } from './tabs'; |
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,32 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
export const KuiTab = ({ isSelected, onClick, children, className, ...rest }) => { | ||
const classes = classNames('kuiTab', className, { | ||
'kuiTab-isSelected': isSelected | ||
}); | ||
|
||
return ( | ||
<button | ||
className={classes} | ||
onClick={onClick} | ||
{...rest} | ||
> | ||
<span className="kuiTab__content"> | ||
{children} | ||
</span> | ||
</button> | ||
); | ||
}; | ||
|
||
KuiTab.defaultProps = { | ||
isSelected: false, | ||
}; | ||
|
||
KuiTab.propTypes = { | ||
isSelected: PropTypes.bool, | ||
onClick: PropTypes.func.isRequired, | ||
children: PropTypes.node, | ||
className: PropTypes.string, | ||
}; |
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,36 @@ | ||
import React from 'react'; | ||
import { render, shallow } from 'enzyme'; | ||
import { requiredProps } from '../../test/required_props'; | ||
import sinon from 'sinon'; | ||
|
||
import { | ||
KuiTab, | ||
} from './tab'; | ||
|
||
describe('KuiTab', () => { | ||
test('renders', () => { | ||
const component = <KuiTab onClick={()=>{}} { ...requiredProps }>children</KuiTab>; | ||
expect(render(component)).toMatchSnapshot(); | ||
}); | ||
|
||
test('renders isSelected', () => { | ||
const component = <KuiTab onClick={()=>{}} isSelected { ...requiredProps }>children</KuiTab>; | ||
expect(render(component)).toMatchSnapshot(); | ||
}); | ||
|
||
describe('Props', () => { | ||
describe('onClick', () => { | ||
test('is called when the button is clicked', () => { | ||
const onClickHandler = sinon.stub(); | ||
|
||
const $button = shallow( | ||
<KuiTab onClick={onClickHandler} /> | ||
); | ||
|
||
$button.simulate('click'); | ||
|
||
sinon.assert.calledOnce(onClickHandler); | ||
}); | ||
}); | ||
}); | ||
}); |
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,25 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
export const KuiTabs = ({ | ||
children, | ||
className, | ||
...rest | ||
}) => { | ||
const classes = classNames('kuiTabs', className); | ||
|
||
return ( | ||
<div | ||
className={classes} | ||
{...rest} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
KuiTabs.propTypes = { | ||
children: PropTypes.node, | ||
className: PropTypes.string | ||
}; |
Oops, something went wrong.