From ba490fca87d78d56fb7488e41792e3d760f08ad1 Mon Sep 17 00:00:00 2001 From: Yuri Drabik Date: Tue, 8 Aug 2017 16:46:09 +0200 Subject: [PATCH] Add header to SlidingPanel component (#137) * Add header to SlidingPanel component * fix unit tests and update cov script command * adjust font for the close button icon --- components/index.scss | 1 + components/slidingPanel/slidingPanel.js | 25 ++-- components/slidingPanel/slidingPanel.md | 7 +- components/slidingPanel/slidingPanel.scss | 5 +- components/slidingPanel/slidingPanelHeader.js | 28 ++++ .../slidingPanel/slidingPanelHeader.scss | 38 ++++++ package.json | 2 +- .../__snapshots__/slidingPanel.spec.js.snap | 124 +++++++++++++++--- .../slidingPanelHeader.spec.js.snap | 29 ++++ tests/unit/slidingPanel/slidingPanel.spec.js | 16 ++- .../slidingPanel/slidingPanelHeader.spec.js | 34 +++++ 11 files changed, 277 insertions(+), 32 deletions(-) create mode 100644 components/slidingPanel/slidingPanelHeader.js create mode 100644 components/slidingPanel/slidingPanelHeader.scss create mode 100644 tests/unit/slidingPanel/__snapshots__/slidingPanelHeader.spec.js.snap create mode 100644 tests/unit/slidingPanel/slidingPanelHeader.spec.js diff --git a/components/index.scss b/components/index.scss index dd756ab1..e9e3a20e 100644 --- a/components/index.scss +++ b/components/index.scss @@ -17,6 +17,7 @@ @import "./price/price.scss"; @import "./radioButton/radioButton.scss"; @import "./slidingPanel/slidingPanel.scss"; +@import "./slidingPanel/slidingPanelHeader.scss"; @import "./spinner/spinner.scss"; @import "./rating/rating.scss"; @import "~react-select/scss/default.scss"; diff --git a/components/slidingPanel/slidingPanel.js b/components/slidingPanel/slidingPanel.js index 9d83a03b..58b83951 100644 --- a/components/slidingPanel/slidingPanel.js +++ b/components/slidingPanel/slidingPanel.js @@ -1,4 +1,6 @@ -import React, { Component, PropTypes } from 'react'; +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import SlidingPanelHeader from './slidingPanelHeader'; import { getClassNamesWithMods, getDataAttributes } from '../_helpers'; export default class SlidingPanel extends Component { @@ -48,6 +50,11 @@ export default class SlidingPanel extends Component { * Defines if the panel is open. */ active: PropTypes.bool, + + /** + * Defines title for header. Optional. If it's defined header will be shown. + */ + title: PropTypes.string, } static defaultProps = { @@ -72,17 +79,13 @@ export default class SlidingPanel extends Component { this.panel.addEventListener('transitionend', this.handleTransitionEnd); - this.closeButton = this.panel.querySelector('[rel="close"]'); - if (this.closeButton) { - this.closeButton.addEventListener('click', this.handleClose); - } + this.closeButtons = this.panel.querySelectorAll('[rel="close"]'); + this.closeButtons.forEach(b => b.addEventListener('click', this.handleClose)); } componentWillUnmount() { this.panel.removeEventListener('transitionend', this.handleTransitionEnd); - if (this.closeButton) { - this.closeButton.removeEventListener('click', this.handleClose); - } + this.closeButtons.forEach(b => b.removeEventListener('click', this.handleClose)); } /** @@ -141,6 +144,7 @@ export default class SlidingPanel extends Component { const { dataAttrs, children, + title, } = this.props; const overlayMods = []; @@ -167,7 +171,10 @@ export default class SlidingPanel extends Component { ref={(e) => { this.panel = e; }} {...getDataAttributes(dataAttrs)} > - {children} + {title && } +
+ {children} +
); diff --git a/components/slidingPanel/slidingPanel.md b/components/slidingPanel/slidingPanel.md index a7359dc9..45ef4cf4 100644 --- a/components/slidingPanel/slidingPanel.md +++ b/components/slidingPanel/slidingPanel.md @@ -4,11 +4,14 @@ Basic Sliding Panel: - setState({ isSlidingPanelOpen: false })}> + setState({ isSlidingPanelOpen: false })} + title="Panel Title" + > This is an example
Of how simple it is to use
Our sliding panel.

- diff --git a/components/slidingPanel/slidingPanel.scss b/components/slidingPanel/slidingPanel.scss index 43d34611..01af6eb8 100644 --- a/components/slidingPanel/slidingPanel.scss +++ b/components/slidingPanel/slidingPanel.scss @@ -24,7 +24,6 @@ margin: var(--tx-sliding-panel-panel-margin); max-width: var(--tx-sliding-panel-panel-max-width); min-width: var(--tx-sliding-panel-panel-min-width); - padding: var(--tx-sliding-panel-panel-padding); position: var(--tx-sliding-panel-panel-position); right: var(--tx-sliding-panel-panel-right); top: var(--tx-sliding-panel-panel-top); @@ -36,3 +35,7 @@ transform: var(--tx-sliding-panel-panel-open-transform); } } + +.ui-sliding-panel__content { + padding: var(--tx-sliding-panel-panel-padding); +} diff --git a/components/slidingPanel/slidingPanelHeader.js b/components/slidingPanel/slidingPanelHeader.js new file mode 100644 index 00000000..854d8346 --- /dev/null +++ b/components/slidingPanel/slidingPanelHeader.js @@ -0,0 +1,28 @@ +import React from 'react'; +import PropTypes from 'prop-types'; + +const SlidingPanelHeader = ({ title }) => { + if (!title) { + return