ToggleButtonBar.jsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }
  56. @observer
  57. class ToggleButtonBar extends React.Component<Props> {
  58. render() {
  59. if (!this.props.items) {
  60. return null
  61. }
  62. return (
  63. <Wrapper className={this.props.className}>
  64. {this.props.items.map(item => {
  65. return (
  66. <Item
  67. data-test-id={`toggleButtonBar-${item.value}`}
  68. key={item.value}
  69. selected={this.props.selectedValue === item.value}
  70. onClick={() => { if (this.props.onChange) this.props.onChange(item) }}
  71. >{item.label}</Item>
  72. )
  73. })}
  74. </Wrapper>
  75. )
  76. }
  77. }
  78. export default ToggleButtonBar