Panel.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (C) 2019 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import * as React from 'react'
  15. import styled, { css } from 'styled-components'
  16. import { observer } from 'mobx-react'
  17. import Palette from '../../styleUtils/Palette'
  18. import StyleProps from '../../styleUtils/StyleProps'
  19. import loadingImage from './images/loading.svg'
  20. const Wrapper = styled.div<any>`
  21. display: flex;
  22. min-height: 0;
  23. flex-grow: 1;
  24. position: relative;
  25. `
  26. const Navigation = styled.div<any>`
  27. width: 224px;
  28. background-image: linear-gradient(rgba(200, 204, 215, 0.54), rgba(164, 170, 181, 0.54));
  29. `
  30. const NavigationItemDiv = styled.div<any>`
  31. position: relative;
  32. height: 47px;
  33. border-bottom: 1px solid ${Palette.grayscale[2]};
  34. color: ${props => (props.disabled ? Palette.grayscale[3] : 'black')};
  35. display: flex;
  36. align-items: center;
  37. padding: 0 24px;
  38. font-size: 18px;
  39. cursor: ${props => (props.disabled ? 'not-allowed' : 'pointer')};
  40. ${props => (props.selected ? css`
  41. color: ${Palette.primary};
  42. background: ${Palette.grayscale[2]};
  43. cursor: default;
  44. ` : '')}
  45. `
  46. const Content = styled.div<any>`
  47. width: 576px;
  48. display: flex;
  49. flex-direction: column;
  50. min-height: 0;
  51. `
  52. const ReloadButton = styled.div<any>`
  53. font-size: 10px;
  54. color: ${Palette.grayscale[4]};
  55. &:hover {
  56. color: ${Palette.primary};
  57. }
  58. cursor: pointer;
  59. position: absolute;
  60. bottom: 42px;
  61. left: 32px;
  62. `
  63. const Loading = styled.span`
  64. position: absolute;
  65. top: 15px;
  66. right: 8px;
  67. width: 16px;
  68. height: 16px;
  69. background: url('${loadingImage}') center no-repeat;
  70. ${StyleProps.animations.rotation}
  71. `
  72. export type NavigationItem = {
  73. label: string,
  74. value: string,
  75. disabled?: boolean,
  76. title?: string,
  77. loading?: boolean,
  78. }
  79. export type Props = {
  80. navigationItems: NavigationItem[],
  81. content: React.ReactNode,
  82. selectedValue: string | null,
  83. onChange: (item: NavigationItem) => void,
  84. style?: any,
  85. reloadLabel: string,
  86. onReloadClick: () => void,
  87. }
  88. export const TEST_ID = 'panel'
  89. @observer
  90. class Panel extends React.Component<Props> {
  91. handleItemClick(item: NavigationItem) {
  92. if (item.disabled) {
  93. return
  94. }
  95. if (item.value !== this.props.selectedValue) {
  96. this.props.onChange(item)
  97. }
  98. }
  99. render() {
  100. return (
  101. <Wrapper style={this.props.style}>
  102. <Navigation>
  103. {this.props.navigationItems.map((item, i) => (
  104. <NavigationItemDiv
  105. key={item.value}
  106. selected={this.props.selectedValue
  107. ? this.props.selectedValue === item.value : i === 0}
  108. onClick={() => { this.handleItemClick(item) }}
  109. data-test-id={`${TEST_ID}-navItem-${item.value}`}
  110. disabled={item.disabled}
  111. title={item.title}
  112. >{item.label}{item.loading ? <Loading /> : null}
  113. </NavigationItemDiv>
  114. ))}
  115. </Navigation>
  116. <Content data-test-id={`${TEST_ID}-content`}>{this.props.content}</Content>
  117. <ReloadButton onClick={() => { this.props.onReloadClick() }}>
  118. {this.props.reloadLabel}
  119. </ReloadButton>
  120. </Wrapper>
  121. )
  122. }
  123. }
  124. export default Panel