PropertiesTable.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. import React from 'react'
  15. import { observer } from 'mobx-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. import Dropdown from '../Dropdown'
  23. import AutocompleteDropdown from '../AutocompleteDropdown'
  24. import { Field, EnumItem, isEnumSeparator } from '../../../@types/Field'
  25. const Wrapper = styled.div<any>`
  26. display: flex;
  27. ${props => (props.width ? `width: ${props.width - 2}px;` : '')}
  28. flex-direction: column;
  29. border: 1px solid ${Palette.grayscale[2]};
  30. border-radius: ${StyleProps.borderRadius};
  31. ${props => (props.disabled ? css`
  32. opacity: 0.5;
  33. ` : '')}
  34. ${props => (props.disabledLoading ? StyleProps.animations.disabledLoading : '')}
  35. `
  36. const Column = styled.div<any>`
  37. ${StyleProps.exactWidth('calc(50% - 24px)')}
  38. height: 32px;
  39. padding: 0 8px 0 16px;
  40. display: flex;
  41. align-items: center;
  42. > span {
  43. white-space: nowrap;
  44. overflow: hidden;
  45. text-overflow: ellipsis;
  46. min-width: 0;
  47. }
  48. ${props => (props.header ? css`
  49. color: ${Palette.grayscale[4]};
  50. background: ${Palette.grayscale[7]};
  51. ` : '')}
  52. `
  53. const Row = styled.div<any>`
  54. display: flex;
  55. align-items: center;
  56. border-bottom: 1px solid ${Palette.grayscale[2]};
  57. &:last-child {
  58. border-bottom: 0;
  59. }
  60. &:first-child ${Column} {
  61. border-top-left-radius: ${StyleProps.borderRadius};
  62. }
  63. &:last-child ${Column} {
  64. border-bottom-left-radius: ${StyleProps.borderRadius};
  65. }
  66. `
  67. const baseId = 'propertiesTable'
  68. type Props = {
  69. properties: Field[],
  70. onChange: (property: Field, value: any) => void,
  71. valueCallback: (property: Field) => any,
  72. hideRequiredSymbol?: boolean,
  73. disabled?: boolean,
  74. disabledLoading?: boolean,
  75. labelRenderer?: ((propName: string) => string) | null,
  76. width?: number,
  77. }
  78. @observer
  79. class PropertiesTable extends React.Component<Props> {
  80. getName(propName: string): string {
  81. if (this.props.labelRenderer) {
  82. return this.props.labelRenderer(propName)
  83. }
  84. if (propName.indexOf('/') > -1) {
  85. return LabelDictionary.get(propName.substr(propName.lastIndexOf('/') + 1))
  86. }
  87. return LabelDictionary.get(propName)
  88. }
  89. renderSwitch(prop: Field, opts: { triState: boolean }) {
  90. return (
  91. <Switch
  92. data-test-id={`${baseId}-switch-${prop.name}`}
  93. secondary
  94. disabled={this.props.disabledLoading}
  95. triState={opts.triState}
  96. height={16}
  97. checked={this.props.valueCallback(prop)}
  98. onChange={checked => { this.props.onChange(prop, checked) }}
  99. />
  100. )
  101. }
  102. renderTextInput(prop: Field) {
  103. return (
  104. <TextInput
  105. data-test-id={`${baseId}-textInput-${prop.name}`}
  106. width="100%"
  107. embedded
  108. type={prop.password ? 'password' : 'text'}
  109. value={this.props.valueCallback(prop)}
  110. onChange={e => { this.props.onChange(prop, e.target.value) }}
  111. placeholder={this.getName(prop.name)}
  112. required={typeof prop.required === 'boolean' && !this.props.hideRequiredSymbol ? prop.required : false}
  113. disabled={this.props.disabledLoading}
  114. />
  115. )
  116. }
  117. renderEnumDropdown(prop: Field) {
  118. if (!prop.enum) {
  119. return null
  120. }
  121. let items = prop.enum.map((e: EnumItem) => {
  122. if (typeof e === 'string') {
  123. return {
  124. label: this.getName(e),
  125. value: e,
  126. }
  127. } if (isEnumSeparator(e)) {
  128. return { separator: true }
  129. }
  130. return {
  131. label: e.name,
  132. value: e.id,
  133. }
  134. })
  135. items = [
  136. { label: 'Choose a value', value: null },
  137. ...items,
  138. ]
  139. const selectedItem = items.find(i => !i.separator && i.value === this.props.valueCallback(prop))
  140. const commonProps = {
  141. embedded: true,
  142. width: 320,
  143. selectedItem,
  144. items,
  145. disabled: this.props.disabledLoading,
  146. onChange: (item: { value: any }) => this.props.onChange(prop, item.value),
  147. required: typeof prop.required === 'boolean' && !this.props.hideRequiredSymbol ? prop.required : false,
  148. }
  149. if (items.length < 10) {
  150. return (
  151. <Dropdown
  152. data-test-id={`${baseId}-dropdown-${prop.name}`}
  153. noSelectionMessage="Choose a value"
  154. dimFirstItem
  155. // eslint-disable-next-line react/jsx-props-no-spreading
  156. {...commonProps}
  157. />
  158. )
  159. }
  160. return (
  161. <AutocompleteDropdown
  162. dimNullValue
  163. // eslint-disable-next-line react/jsx-props-no-spreading
  164. {...commonProps}
  165. />
  166. )
  167. }
  168. renderInput(prop: Field) {
  169. let input = null
  170. switch (prop.type) {
  171. case 'boolean':
  172. input = this.renderSwitch(prop, { triState: Boolean(prop.nullableBoolean) })
  173. break
  174. case 'string':
  175. if (prop.enum) {
  176. input = this.renderEnumDropdown(prop)
  177. } else {
  178. input = this.renderTextInput(prop)
  179. }
  180. break
  181. default:
  182. }
  183. return input
  184. }
  185. render() {
  186. return (
  187. <Wrapper
  188. disabled={this.props.disabled}
  189. disabledLoading={this.props.disabledLoading}
  190. width={this.props.width}
  191. >
  192. {this.props.properties.map(prop => (
  193. <Row key={prop.name}>
  194. <Column header>
  195. <span title={this.getName(prop.label || prop.name)}>
  196. {this.getName(prop.label || prop.name)}
  197. </span>
  198. </Column>
  199. <Column input>{this.renderInput(prop)}</Column>
  200. </Row>
  201. ))}
  202. </Wrapper>
  203. )
  204. }
  205. }
  206. export default PropertiesTable