ToggleButtonBar.jsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright (C) 2017 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. // @flow
  15. import React from 'react'
  16. import { observer } from 'mobx-react'
  17. import styled from 'styled-components'
  18. import Palette from '../../styleUtils/Palette'
  19. import StyleProps from '../../styleUtils/StyleProps'
  20. const Wrapper = styled.div`
  21. display: flex;
  22. justify-content: center;
  23. `
  24. const Item = styled.div`
  25. width: 112px;
  26. height: 18px;
  27. background: ${props => props.selected ? Palette.primary : 'white'};
  28. color: ${props => props.selected ? 'white' : Palette.primary};
  29. border: 1px solid ${Palette.primary};
  30. border-right: 1px solid white;
  31. text-align: center;
  32. line-height: 19px;
  33. text-transform: uppercase;
  34. font-size: 9px;
  35. font-weight: ${StyleProps.fontWeights.medium};
  36. transition: all ${StyleProps.animations.swift};
  37. cursor: pointer;
  38. &:first-child {
  39. border-top-left-radius: ${StyleProps.borderRadius};
  40. border-bottom-left-radius: ${StyleProps.borderRadius};
  41. border-right: 1px solid ${Palette.primary};
  42. }
  43. &:last-child {
  44. border-top-right-radius: ${StyleProps.borderRadius};
  45. border-bottom-right-radius: ${StyleProps.borderRadius};
  46. border-right: 1px solid ${Palette.primary};
  47. }
  48. `
  49. type ItemType = { value: string, label: string }
  50. type Props = {
  51. items: Array<ItemType>,
  52. selectedValue?: string,
  53. onChange?: (item: ItemType) => void,
  54. className?: string,
  55. 'data-test-id'?: string,
  56. }
  57. @observer
  58. class ToggleButtonBar extends React.Component<Props> {
  59. render() {
  60. if (!this.props.items) {
  61. return null
  62. }
  63. return (
  64. <Wrapper
  65. data-test-id={this.props['data-test-id'] || 'toggleButtonBar-wrapper'}
  66. className={this.props.className}
  67. >
  68. {this.props.items.map(item => {
  69. return (
  70. <Item
  71. data-test-id={`toggleButtonBar-${item.value}`}
  72. key={item.value}
  73. selected={this.props.selectedValue === item.value}
  74. onClick={() => { if (this.props.onChange) this.props.onChange(item) }}
  75. >{item.label}</Item>
  76. )
  77. })}
  78. </Wrapper>
  79. )
  80. }
  81. }
  82. export default ToggleButtonBar