index.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, { css } from 'styled-components'
  18. import Switch from '../../atoms/Switch'
  19. import TextInput from '../../atoms/TextInput'
  20. import LabelDictionary from '../../../utils/LabelDictionary'
  21. import Palette from '../../styleUtils/Palette'
  22. import StyleProps from '../../styleUtils/StyleProps'
  23. import Dropdown from '../../molecules/Dropdown'
  24. import type { Field } from '../../../types/Field'
  25. const Wrapper = styled.div`
  26. display: flex;
  27. flex-direction: column;
  28. border: 1px solid ${Palette.grayscale[2]};
  29. border-radius: ${StyleProps.borderRadius};
  30. `
  31. const Column = styled.div`
  32. ${StyleProps.exactWidth('calc(50% - 16px)')}
  33. height: 32px;
  34. display: flex;
  35. align-items: center;
  36. padding-left: 16px;
  37. ${props => props.header ? css`
  38. color: ${Palette.grayscale[4]};
  39. background: ${Palette.grayscale[7]};
  40. ` : ''}
  41. `
  42. const Row = styled.div`
  43. display: flex;
  44. align-items: center;
  45. border-bottom: 1px solid ${Palette.grayscale[2]};
  46. &:last-child {
  47. border-bottom: 0;
  48. }
  49. &:first-child ${Column} {
  50. border-top-left-radius: ${StyleProps.borderRadius};
  51. }
  52. &:last-child ${Column} {
  53. border-bottom-left-radius: ${StyleProps.borderRadius};
  54. }
  55. `
  56. const baseId = 'propertiesTable'
  57. type Props = {
  58. properties: Field[],
  59. onChange: (property: Field, value: any) => void,
  60. valueCallback: (property: Field) => any,
  61. }
  62. @observer
  63. class PropertiesTable extends React.Component<Props> {
  64. getName(propName: string): string {
  65. if (propName.indexOf('/') > -1) {
  66. return LabelDictionary.get(propName.substr(propName.lastIndexOf('/') + 1))
  67. }
  68. return LabelDictionary.get(propName)
  69. }
  70. renderSwitch(prop: Field, opts: { triState: boolean }) {
  71. return (
  72. <Switch
  73. data-test-id={`${baseId}-switch-${prop.name}`}
  74. secondary
  75. triState={opts.triState}
  76. height={16}
  77. checked={this.props.valueCallback(prop)}
  78. onChange={checked => { this.props.onChange(prop, checked) }}
  79. />
  80. )
  81. }
  82. renderTextInput(prop: Field) {
  83. return (
  84. <TextInput
  85. data-test-id={`${baseId}-textInput-${prop.name}`}
  86. width="100%"
  87. embedded
  88. value={this.props.valueCallback(prop)}
  89. onChange={e => { this.props.onChange(prop, e.target.value) }}
  90. placeholder={this.getName(prop.name)}
  91. required={typeof prop.required === 'boolean' ? prop.required : false}
  92. />
  93. )
  94. }
  95. renderEnumDropdown(prop: Field) {
  96. if (!prop.enum) {
  97. return null
  98. }
  99. let items = prop.enum.map(e => {
  100. if (typeof e === 'string') {
  101. return {
  102. label: this.getName(e),
  103. value: e,
  104. }
  105. } else if (e.separator === true) {
  106. return e
  107. }
  108. return {
  109. label: e.name,
  110. value: e.id,
  111. }
  112. })
  113. items = [
  114. { label: 'Choose a value', value: null },
  115. ...items,
  116. ]
  117. let selectedItem = items.find(i => !i.separator && i.value === this.props.valueCallback(prop))
  118. return (
  119. <Dropdown
  120. embedded
  121. data-test-id={`${baseId}-dropdown-${prop.name}`}
  122. width={320}
  123. noSelectionMessage="Choose a value"
  124. selectedItem={selectedItem}
  125. items={items}
  126. onChange={item => this.props.onChange(prop, item.value)}
  127. />
  128. )
  129. }
  130. renderInput(prop: Field) {
  131. let input = null
  132. switch (prop.type) {
  133. case 'boolean':
  134. input = this.renderSwitch(prop, { triState: true })
  135. break
  136. case 'strict-boolean':
  137. input = this.renderSwitch(prop, { triState: false })
  138. break
  139. case 'string':
  140. if (prop.enum) {
  141. input = this.renderEnumDropdown(prop)
  142. } else {
  143. input = this.renderTextInput(prop)
  144. }
  145. break
  146. default:
  147. }
  148. return input
  149. }
  150. render() {
  151. return (
  152. <Wrapper>
  153. {this.props.properties.map(prop => {
  154. return (
  155. <Row key={prop.name} data-test-id={`${baseId}-row-${prop.name}`}>
  156. <Column header data-test-id={`${baseId}-header`}>{this.getName(prop.name)}</Column>
  157. <Column input>{this.renderInput(prop)}</Column>
  158. </Row>
  159. )
  160. })}
  161. </Wrapper>
  162. )
  163. }
  164. }
  165. export default PropertiesTable