From 4c86ff95d70d0b9e23c018e220ab9a598b788deb Mon Sep 17 00:00:00 2001 From: Tobias Date: Mon, 21 Jan 2019 13:36:09 +0100 Subject: [PATCH] feat: refactor #tabs to make it more flexible and --- .../src/shared/inlineTags/Tabs.js | 135 +++++++++++++++++- .../src/shared/parts/uilib/ItemWrapper.js | 3 +- .../src/uilib/components/examples/Tabs.txt | 11 +- .../dnb-ui-lib/src/components/tabs/Example.js | 11 +- .../dnb-ui-lib/src/components/tabs/Tabs.js | 112 ++++++++++----- .../components/tabs/__tests__/Tabs.test.js | 8 +- .../__tests__/__snapshots__/Tabs.test.js.snap | 46 +++--- .../dnb-ui-lib/src/components/tabs/details.md | 2 +- .../src/components/tabs/style/_tabs.scss | 3 +- .../dnb-ui-lib/stories/componentExamples.js | 8 +- 10 files changed, 257 insertions(+), 82 deletions(-) diff --git a/packages/dnb-design-system-portal/src/shared/inlineTags/Tabs.js b/packages/dnb-design-system-portal/src/shared/inlineTags/Tabs.js index 32a3e6d39f9..6533981bb10 100644 --- a/packages/dnb-design-system-portal/src/shared/inlineTags/Tabs.js +++ b/packages/dnb-design-system-portal/src/shared/inlineTags/Tabs.js @@ -3,5 +3,136 @@ * */ -import { Tabs } from 'dnb-ui-lib/src' -export default Tabs +import React, { PureComponent } from 'react' +import PropTypes from 'prop-types' + +import { css } from '@emotion/core' +// import styled from '@emotion/styled' +import { navigate, parsePath } from 'gatsby' +import { CloseButton } from 'dnb-ui-lib/src/components/modal' +import { fullscreen as fullscreenIcon } from 'dnb-ui-lib/src/icons/secondary_icons' +import { Button, Tabs } from 'dnb-ui-lib/src' + +const pathPrefix = __PATH_PREFIX__ // eslint-disable-line + +const getLocation = () => { + if (typeof window === 'undefined') { + return null + } + const { pathname, search, hash } = window.location + return { + ...parsePath(pathname.replace(new RegExp(pathPrefix || '', 'g'), '')), + search, + hash + } +} + +// const DemoWrapper = styled.div` +// padding: 2rem 0 0; +// ` + +const tabsWrapperStyle = css` + .fullscreen-page & { + top: 0; + .is-sticky .dnb-tabs__tabs { + margin: 0 -2rem; + } + } + .dnb-modal__close-button { + position: relative; + top: auto; /* to force the button to center */ + right: auto; + } +` + +class CustomTabs extends PureComponent { + static propTypes = { + children: PropTypes.node.isRequired, + tabs: PropTypes.array + } + static defaultProps = { + tabs: [ + { title: 'Demo', key: 'demo' }, + { title: 'Details', key: 'info' }, + { title: 'Markup', key: 'code' } + ] + } + state = { + activeTabKey: 'demo', + wasFullscreen: null + } + constructor(props) { + super(props) + this._id = 'item-wrapper' + const location = getLocation() + if (location) + this.state.wasFullscreen = /fullscreen/.test(location.search) + } + openTab = ({ key }) => { + this.setState({ + activeTabKey: key + }) + } + isActive(tabKey) { + return this.state.activeTabKey === tabKey + } + componentWillMount() { + const location = getLocation() + if (location) + this.setState({ + wasFullscreen: /fullscreen/.test(location.search) + }) + } + openFullscreen = () => { + const location = getLocation() + if (location) + navigate( + `${location.pathname}?fullscreen#${this.state.activeTabKey}` + ) + } + quitFullscreen = () => { + const location = getLocation() + if (location) navigate([location.pathname, location.hash].join('')) + } + + render() { + const { tabs, children } = this.props + const data = Tabs.getData({ tabs, children }) + + return ( + { + return ( + + + + {this.state.wasFullscreen ? ( + + ) : ( +