Skip to content

Commit

Permalink
[UI Framework] Reactify kuiCollapseButton (elastic#12225)
Browse files Browse the repository at this point in the history
* add KuiCollapseButton
* Export DIRECTIONS in collapse_button.js and use it in its test
  • Loading branch information
PopradiArpad authored and cjcenizal committed Jun 9, 2017
1 parent 45e966b commit 49f37e6
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`KuiCollapseButton Props direction down renders the down class 1`] = `
<button
aria-label="aria-label"
class="kuiCollapseButton testClass1 testClass2"
data-test-subj="test subject string"
type="button"
>
<div
class="kuiIcon fa-chevron-circle-down"
/>
</button>
`;

exports[`KuiCollapseButton Props direction left renders the left class 1`] = `
<button
aria-label="aria-label"
class="kuiCollapseButton testClass1 testClass2"
data-test-subj="test subject string"
type="button"
>
<div
class="kuiIcon fa-chevron-circle-left"
/>
</button>
`;

exports[`KuiCollapseButton Props direction right renders the right class 1`] = `
<button
aria-label="aria-label"
class="kuiCollapseButton testClass1 testClass2"
data-test-subj="test subject string"
type="button"
>
<div
class="kuiIcon fa-chevron-circle-right"
/>
</button>
`;

exports[`KuiCollapseButton Props direction up renders the up class 1`] = `
<button
aria-label="aria-label"
class="kuiCollapseButton testClass1 testClass2"
data-test-subj="test subject string"
type="button"
>
<div
class="kuiIcon fa-chevron-circle-up"
/>
</button>
`;
41 changes: 41 additions & 0 deletions ui_framework/components/collapse_button/collapse_button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, {
PropTypes,
} from 'react';
import classNames from 'classnames';

const DIRECTIONS = [
'down',
'up',
'left',
'right'
];

const directionToClassNameMap = {
down: 'fa-chevron-circle-down',
up: 'fa-chevron-circle-up',
left: 'fa-chevron-circle-left',
right: 'fa-chevron-circle-right',
};

const KuiCollapseButton = ({ className, direction, ...rest }) => {
const classes = classNames('kuiCollapseButton', className);
const childClasses = classNames('kuiIcon', directionToClassNameMap[direction]);

return (<button
type="button"
className={classes}
{...rest}
>
<div className={childClasses}/>
</button>);
};

KuiCollapseButton.propTypes = {
className: PropTypes.string,
direction: PropTypes.oneOf(DIRECTIONS).isRequired
};

export {
DIRECTIONS,
KuiCollapseButton
};
48 changes: 48 additions & 0 deletions ui_framework/components/collapse_button/collapse_button.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { render,shallow } from 'enzyme';
import { requiredProps } from '../../test/required_props';
import sinon from 'sinon';

import {
DIRECTIONS,
KuiCollapseButton,
} from './collapse_button';

describe('KuiCollapseButton', () => {
describe('Props', () => {
describe('direction', () => {
DIRECTIONS.forEach(direction => {
describe(direction, () => {
test(`renders the ${direction} class`, () => {
const component = <KuiCollapseButton direction={direction} { ...requiredProps }/>;
expect(render(component)).toMatchSnapshot();
});
});
});
});

describe('onClick', () => {
test(`isn't called upon instantiation`, () => {
const onClickHandler = sinon.stub();

shallow(
<KuiCollapseButton direction='left' onClick={onClickHandler} />
);

sinon.assert.notCalled(onClickHandler);
});

test('is called when the button is clicked', () => {
const onClickHandler = sinon.stub();

const $button = shallow(
<KuiCollapseButton direction='left' onClick={onClickHandler} />
);

$button.simulate('click');

sinon.assert.calledOnce(onClickHandler);
});
});
});
});
1 change: 1 addition & 0 deletions ui_framework/components/collapse_button/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { KuiCollapseButton } from './collapse_button';
4 changes: 4 additions & 0 deletions ui_framework/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export {
KuiSubmitButton,
} from './button';

export {
KuiCollapseButton
} from './collapse_button';

export {
KuiEvent,
KuiEventSymbol,
Expand Down
1 change: 1 addition & 0 deletions ui_framework/doc_site/src/services/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const components = [{
}, {
name: 'CollapseButton',
component: CollapseButtonExample,
hasReact: true,
}, {
name: 'EmptyTablePrompt',
component: EmptyTablePromptExample,
Expand Down

This file was deleted.

14 changes: 14 additions & 0 deletions ui_framework/doc_site/src/views/collapse_button/collapse_button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';

import {
KuiCollapseButton
} from '../../../../components';

export default () => (
<div>
<KuiCollapseButton direction='down'/>
<KuiCollapseButton direction='up'/>
<KuiCollapseButton direction='left'/>
<KuiCollapseButton direction='right'/>
</div>
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { renderToHtml } from '../../services';

import {
GuideDemo,
Expand All @@ -8,13 +9,18 @@ import {
GuideText,
} from '../../components';

const collapseButtonHtml = require('./collapse_button.html');
import CollapseButton from './collapse_button';
const collapseButtonSource = require('!!raw!./collapse_button');
const collapseButtonHtml = renderToHtml(CollapseButton);

export default props => (
<GuidePage title={props.route.name}>
<GuideSection
title="CollapseButton"
source={[{
type: GuideSectionTypes.JS,
code: collapseButtonSource,
}, {
type: GuideSectionTypes.HTML,
code: collapseButtonHtml,
}]}
Expand All @@ -24,9 +30,9 @@ export default props => (
containers.
</GuideText>

<GuideDemo
html={collapseButtonHtml}
/>
<GuideDemo>
<CollapseButton />
</GuideDemo>
</GuideSection>
</GuidePage>
);

0 comments on commit 49f37e6

Please sign in to comment.