index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React, { Component } from 'react';
  2. import { Tabs, Radio } from 'antd';
  3. import './index.less';
  4. class TabLayout extends Component {
  5. constructor(props) {
  6. super(props);
  7. const key = props.itemList && props.itemList.length > 0 ? props.itemList[0].key : '';
  8. const defaultActiveKey = props.defaultActiveKey || key;
  9. this.state = { defaultActiveKey };
  10. if (defaultActiveKey !== props.defaultActiveKey) this.onClick(defaultActiveKey);
  11. }
  12. onClick(tab) {
  13. if (this.props.onChange) this.props.onChange(tab);
  14. }
  15. getItem(item) {
  16. const { button = false } = this.props;
  17. return button ? (
  18. <Radio.Button key={item.key} value={item.key}>
  19. {item.name}
  20. </Radio.Button>
  21. ) : (
  22. <Tabs.TabPane tab={item.name} key={item.key} />
  23. );
  24. }
  25. render() {
  26. const { button = false, itemList = [] } = this.props;
  27. const { defaultActiveKey } = this.state;
  28. return (
  29. <div className="tab-layout">
  30. {button ? (
  31. <Radio.Group
  32. defaultValue={defaultActiveKey}
  33. onChange={e => {
  34. this.onClick(e.target.value);
  35. }}
  36. buttonStyle="solid"
  37. >
  38. {itemList.map(item => {
  39. return this.getItem(item);
  40. })}
  41. </Radio.Group>
  42. ) : (
  43. <Tabs defaultActiveKey={defaultActiveKey} onChange={key => this.onClick(key)}>
  44. {itemList.map(item => {
  45. return this.getItem(item);
  46. })}
  47. </Tabs>
  48. )}
  49. </div>
  50. );
  51. }
  52. }
  53. export default TabLayout;