EndpointField.jsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 Switch from '../../atoms/Switch'
  19. import TextInput from '../../atoms/TextInput'
  20. import RadioInput from '../../atoms/RadioInput'
  21. import InfoIcon from '../../atoms/InfoIcon'
  22. import Dropdown from '../../molecules/Dropdown'
  23. import DropdownInput from '../../molecules/DropdownInput'
  24. import TextArea from '../../atoms/TextArea'
  25. import LabelDictionary from '../../../utils/LabelDictionary'
  26. import StyleProps from '../../styleUtils/StyleProps'
  27. import Palette from '../../styleUtils/Palette'
  28. import asteriskImage from './images/asterisk.svg'
  29. const Wrapper = styled.div``
  30. const Label = styled.div`
  31. font-size: 10px;
  32. font-weight: ${StyleProps.fontWeights.medium};
  33. color: ${Palette.grayscale[3]};
  34. text-transform: uppercase;
  35. margin-bottom: 2px;
  36. display: flex;
  37. align-items: center;
  38. `
  39. const LabelText = styled.span`
  40. margin-right: 24px;
  41. `
  42. export const Asterisk = styled.div`
  43. ${StyleProps.exactSize('12px')}
  44. display: inline-block;
  45. background: url('${asteriskImage}') center no-repeat;
  46. margin-bottom: 2px;
  47. margin-left: ${props => props.marginLeft || '0px'};
  48. opacity: 0.8;
  49. `
  50. type Props = {
  51. name: string,
  52. type: string,
  53. value: any,
  54. onChange?: (value: any) => void,
  55. getFieldValue?: (fieldName: string) => string,
  56. onFieldChange?: (fieldName: string, fieldValue: string) => void,
  57. className?: string,
  58. minimum?: number,
  59. maximum?: number,
  60. password?: boolean,
  61. required?: boolean,
  62. large?: boolean,
  63. highlight?: boolean,
  64. disabled?: boolean,
  65. // $FlowIssue
  66. enum?: string[] | { label: string, value: string }[],
  67. items?: any[],
  68. useTextArea?: boolean,
  69. noSelectionMessage?: string,
  70. noItemsMessage?: string,
  71. selectedItems?: string[],
  72. 'data-test-id'?: string,
  73. }
  74. @observer
  75. class Field extends React.Component<Props> {
  76. renderSwitch() {
  77. return (
  78. <Switch
  79. data-test-id={`endpointField-switch-${this.props.name}`}
  80. disabled={this.props.disabled}
  81. checked={this.props.value || false}
  82. onChange={checked => { if (this.props.onChange) this.props.onChange(checked) }}
  83. />
  84. )
  85. }
  86. renderTextInput() {
  87. return (
  88. <TextInput
  89. data-test-id={`endpointField-textInput-${this.props.name}`}
  90. highlight={this.props.highlight}
  91. type={this.props.password ? 'password' : 'text'}
  92. large={this.props.large}
  93. value={this.props.value}
  94. onChange={e => { if (this.props.onChange) this.props.onChange(e.target.value) }}
  95. placeholder={LabelDictionary.get(this.props.name)}
  96. disabled={this.props.disabled}
  97. />
  98. )
  99. }
  100. renderTextArea() {
  101. return (
  102. <TextArea
  103. data-test-id={`endpointField-textArea-${this.props.name}`}
  104. style={{ width: '100%' }}
  105. highlight={this.props.highlight}
  106. value={this.props.value}
  107. onChange={e => { console.log('changing', e); if (this.props.onChange) this.props.onChange(e.target.value) }}
  108. placeholder={LabelDictionary.get(this.props.name)}
  109. disabled={this.props.disabled}
  110. />
  111. )
  112. }
  113. renderEnumDropdown() {
  114. if (!this.props.enum) {
  115. return null
  116. }
  117. let items = this.props.enum.map(e => {
  118. if (typeof e === 'string') {
  119. return {
  120. label: LabelDictionary.get(e),
  121. value: e,
  122. }
  123. }
  124. return e
  125. })
  126. let selectedItem = items.find(i => i.value === this.props.value)
  127. return (
  128. <Dropdown
  129. data-test-id={`endpointField-dropdown-${this.props.name}`}
  130. large={this.props.large}
  131. selectedItem={selectedItem}
  132. noSelectionMessage={this.props.noSelectionMessage}
  133. noItemsMessage={this.props.noItemsMessage}
  134. items={items}
  135. onChange={item => { if (this.props.onChange) this.props.onChange(item.value) }}
  136. disabled={this.props.disabled}
  137. highlight={this.props.highlight}
  138. />
  139. )
  140. }
  141. renderArrayDropdown() {
  142. return (
  143. <Dropdown
  144. data-test-id={`endpointField-multidropdown-${this.props.name}`}
  145. multipleSelection
  146. large={this.props.large}
  147. disabled={this.props.disabled}
  148. noSelectionMessage={this.props.noSelectionMessage}
  149. noItemsMessage={this.props.noItemsMessage}
  150. items={this.props.items}
  151. selectedItems={this.props.selectedItems}
  152. onChange={item => { if (this.props.onChange) this.props.onChange(item.value) }}
  153. highlight={this.props.highlight}
  154. />
  155. )
  156. }
  157. renderIntDropdown() {
  158. if (!this.props.minimum || !this.props.maximum) {
  159. return null
  160. }
  161. let items = []
  162. for (let i = this.props.minimum; i <= this.props.maximum; i += 1) {
  163. items.push({
  164. label: i.toString(),
  165. value: i,
  166. })
  167. }
  168. return (
  169. <Dropdown
  170. data-test-id={`endpointField-dropdown-${this.props.name}`}
  171. large={this.props.large}
  172. selectedItem={this.props.value}
  173. items={items}
  174. onChange={item => { if (this.props.onChange) this.props.onChange(item.value) }}
  175. disabled={this.props.disabled}
  176. highlight={this.props.highlight}
  177. />
  178. )
  179. }
  180. renderRadioInput() {
  181. return (
  182. <RadioInput
  183. data-test-id={`endpointField-radioInput-${this.props.name}`}
  184. checked={this.props.value}
  185. label={LabelDictionary.get(this.props.name)}
  186. onChange={e => { if (this.props.onChange) this.props.onChange(e.target.checked) }}
  187. disabled={this.props.disabled}
  188. />
  189. )
  190. }
  191. renderDropdownInput() {
  192. if (!this.props.items) {
  193. return null
  194. }
  195. let items = this.props.items.map(field => {
  196. return {
  197. value: field.name,
  198. label: field.label || LabelDictionary.get(field.name),
  199. }
  200. })
  201. let fieldName = this.props.value || items[0].value
  202. return (
  203. <DropdownInput
  204. items={items}
  205. selectedItem={fieldName}
  206. onItemChange={item => { if (this.props.onChange) this.props.onChange(item.value) }}
  207. inputValue={this.props.getFieldValue ? this.props.getFieldValue(fieldName) : ''}
  208. onInputChange={value => { if (this.props.onFieldChange) this.props.onFieldChange(fieldName, value) }}
  209. placeholder={LabelDictionary.get(fieldName)}
  210. highlight={this.props.highlight}
  211. disabled={this.props.disabled}
  212. />
  213. )
  214. }
  215. renderInput() {
  216. switch (this.props.type) {
  217. case 'input-choice':
  218. return this.renderDropdownInput()
  219. case 'boolean':
  220. return this.renderSwitch()
  221. case 'string':
  222. if (this.props.enum) {
  223. return this.renderEnumDropdown()
  224. }
  225. if (this.props.useTextArea) {
  226. return this.renderTextArea()
  227. }
  228. return this.renderTextInput()
  229. case 'integer':
  230. if (this.props.minimum || this.props.maximum) {
  231. return this.renderIntDropdown()
  232. }
  233. return this.renderTextInput()
  234. case 'radio':
  235. return this.renderRadioInput()
  236. case 'array':
  237. return this.renderArrayDropdown()
  238. default:
  239. return null
  240. }
  241. }
  242. renderLabel() {
  243. if (this.props.type === 'radio') {
  244. return null
  245. }
  246. let description = LabelDictionary.getDescription(this.props.name)
  247. let infoIcon = null
  248. if (description) {
  249. infoIcon = <InfoIcon text={description} marginLeft={-20} />
  250. }
  251. return (
  252. <Label>
  253. <LabelText data-test-id="endpointField-label">{LabelDictionary.get(this.props.name)}</LabelText>
  254. {infoIcon}
  255. {this.props.required ? <Asterisk data-test-id="endpointField-required" marginLeft={description ? '4px' : '-16px'} /> : null}
  256. </Label>
  257. )
  258. }
  259. render() {
  260. return (
  261. <Wrapper data-test-id={this.props['data-test-id'] || 'endpointField-wrapper'} className={this.props.className}>
  262. {this.renderLabel()}
  263. {this.renderInput()}
  264. </Wrapper>
  265. )
  266. }
  267. }
  268. export default Field