UNPKG

apier-react-tabtab

Version:

A mobile support, draggable, editable and api based Tab for ReactJS

78 lines (67 loc) 2.08 kB
// @flow import * as React from 'react'; import styled from 'styled-components'; import CloseButton from './CloseButton'; const TabStyle = styled.li` display: ${props => props.vertical ? 'block': 'inline-block'}; ${props => props.vertical ? ` background-color: white; color: black; padding: 10px 10px; z-index: 100000; ` : props => props.closable ? 'padding: 10px 10px 10px 15px;' : 'padding: 10px 15px;' } user-select: none; &:hover { cursor: pointer; color: black; } `; type Props = { children: React.Element<any> }; export default class Tab extends React.PureComponent<Props> { __INTERNAL_NODE: React.ElementRef<any>; constructor(props: Props) { super(props); (this: any).clickTab = this.clickTab.bind(this); (this: any).clickDelete = this.clickDelete.bind(this); } clickTab() { const {handleTabChange, index} = this.props; handleTabChange(index); } clickDelete(event: SyntheticEvent<HTMLButtonElement>) { event.stopPropagation(); // prevent trigger clickTab event. const {handleEdit, index} = this.props; handleEdit({type: 'delete', index}); } render() { const {CustomTabStyle, active, closable, vertical, index} = this.props; const TabComponent = CustomTabStyle || TabStyle; return ( <TabComponent ref={node => this.__INTERNAL_NODE = node} onClick={this.clickTab} active={active} vertical={vertical} closable={closable} role="tab" id={`react-tabtab-tab-${index}`} aria-controls={`react-tabtab-panel-${index}`} aria-selected={active} className={'tabbar-tab ' + (active ? 'tabbar-tab-active ' : '') + (vertical ? 'tabbar-tab-vertical ' : '')} > {this.props.children} {closable ? <CloseButton handleDelete={this.clickDelete}/> : null} </TabComponent> ) } } Tab.displayName = 'Tab'; export { TabStyle };