index.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 styled, { css } from 'styled-components'
  17. import Switch from '../../atoms/Switch'
  18. import TextInput from '../../atoms/TextInput'
  19. import LabelDictionary from '../../../utils/LabelDictionary'
  20. import Palette from '../../styleUtils/Palette'
  21. import StyleProps from '../../styleUtils/StyleProps'
  22. const Wrapper = styled.div`
  23. display: flex;
  24. flex-direction: column;
  25. border: 1px solid ${Palette.grayscale[2]};
  26. border-radius: ${StyleProps.borderRadius};
  27. `
  28. const Column = styled.div`
  29. width: 50%;
  30. height: 32px;
  31. display: flex;
  32. align-items: center;
  33. padding-left: 16px;
  34. ${props => props.header ? css`
  35. color: ${Palette.grayscale[4]};
  36. background: ${Palette.grayscale[7]};
  37. ` : ''}
  38. `
  39. const Row = styled.div`
  40. display: flex;
  41. align-items: center;
  42. border-bottom: 1px solid ${Palette.grayscale[2]};
  43. &:last-child {
  44. border-bottom-color: transparent;
  45. }
  46. &:first-child ${Column} {
  47. border-top-left-radius: ${StyleProps.borderRadius};
  48. }
  49. &:last-child ${Column} {
  50. border-bottom-left-radius: ${StyleProps.borderRadius};
  51. }
  52. `
  53. type PropertyType = {
  54. name: string,
  55. type: string,
  56. }
  57. type Props = {
  58. properties: PropertyType[],
  59. onChange: (property: PropertyType, value: any) => void,
  60. valueCallback: (property: PropertyType) => any,
  61. }
  62. class PropertiesTable extends React.Component<Props> {
  63. renderSwitch(prop: PropertyType) {
  64. return (
  65. <Switch
  66. secondary
  67. height={16}
  68. checked={this.props.valueCallback(prop)}
  69. onChange={checked => { this.props.onChange(prop, checked) }}
  70. />
  71. )
  72. }
  73. renderTextInput() {
  74. return (
  75. <TextInput />
  76. )
  77. }
  78. renderInput(prop: PropertyType) {
  79. let input = null
  80. switch (prop.type) {
  81. case 'boolean':
  82. input = this.renderSwitch(prop)
  83. break
  84. case 'string':
  85. input = this.renderTextInput()
  86. break
  87. default:
  88. }
  89. return input
  90. }
  91. render() {
  92. return (
  93. <Wrapper>
  94. {this.props.properties.map(prop => {
  95. return (
  96. <Row key={prop.name}>
  97. <Column header>{LabelDictionary.get(prop.name)}</Column>
  98. <Column input>{this.renderInput(prop)}</Column>
  99. </Row>
  100. )
  101. })}
  102. </Wrapper>
  103. )
  104. }
  105. }
  106. export default PropertiesTable