EndpointField.jsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. }
  73. @observer
  74. class Field extends React.Component<Props> {
  75. renderSwitch() {
  76. return (
  77. <Switch
  78. data-test-id={`endpointField-switch-${this.props.name}`}
  79. disabled={this.props.disabled}
  80. checked={this.props.value || false}
  81. onChange={checked => { if (this.props.onChange) this.props.onChange(checked) }}
  82. />
  83. )
  84. }
  85. renderTextInput() {
  86. return (
  87. <TextInput
  88. data-test-id={`endpointField-textInput-${this.props.name}`}
  89. highlight={this.props.highlight}
  90. type={this.props.password ? 'password' : 'text'}
  91. large={this.props.large}
  92. value={this.props.value}
  93. onChange={e => { if (this.props.onChange) this.props.onChange(e.target.value) }}
  94. placeholder={LabelDictionary.get(this.props.name)}
  95. disabled={this.props.disabled}
  96. />
  97. )
  98. }
  99. renderTextArea() {
  100. return (
  101. <TextArea
  102. data-test-id={`endpointField-textArea-${this.props.name}`}
  103. style={{ width: '100%' }}
  104. highlight={this.props.highlight}
  105. value={this.props.value}
  106. onChange={e => { console.log('changing', e); if (this.props.onChange) this.props.onChange(e.target.value) }}
  107. placeholder={LabelDictionary.get(this.props.name)}
  108. disabled={this.props.disabled}
  109. />
  110. )
  111. }
  112. renderEnumDropdown() {
  113. if (!this.props.enum) {
  114. return null
  115. }
  116. let items = this.props.enum.map(e => {
  117. if (typeof e === 'string') {
  118. return {
  119. label: LabelDictionary.get(e),
  120. value: e,
  121. }
  122. }
  123. return e
  124. })
  125. let selectedItem = items.find(i => i.value === this.props.value)
  126. return (
  127. <Dropdown
  128. data-test-id={`endpointField-dropdown-${this.props.name}`}
  129. large={this.props.large}
  130. selectedItem={selectedItem}
  131. noSelectionMessage={this.props.noSelectionMessage}
  132. noItemsMessage={this.props.noItemsMessage}
  133. items={items}
  134. onChange={item => { if (this.props.onChange) this.props.onChange(item.value) }}
  135. disabled={this.props.disabled}
  136. highlight={this.props.highlight}
  137. />
  138. )
  139. }
  140. renderArrayDropdown() {
  141. return (
  142. <Dropdown
  143. multipleSelection
  144. large={this.props.large}
  145. disabled={this.props.disabled}
  146. noSelectionMessage={this.props.noSelectionMessage}
  147. noItemsMessage={this.props.noItemsMessage}
  148. items={this.props.items}
  149. selectedItems={this.props.selectedItems}
  150. onChange={item => { if (this.props.onChange) this.props.onChange(item.value) }}
  151. highlight={this.props.highlight}
  152. />
  153. )
  154. }
  155. renderIntDropdown() {
  156. if (!this.props.minimum || !this.props.maximum) {
  157. return null
  158. }
  159. let items = []
  160. for (let i = this.props.minimum; i <= this.props.maximum; i += 1) {
  161. items.push({
  162. label: i.toString(),
  163. value: i,
  164. })
  165. }
  166. return (
  167. <Dropdown
  168. data-test-id={`endpointField-dropdown-${this.props.name}`}
  169. large={this.props.large}
  170. selectedItem={this.props.value}
  171. items={items}
  172. onChange={item => { if (this.props.onChange) this.props.onChange(item.value) }}
  173. disabled={this.props.disabled}
  174. highlight={this.props.highlight}
  175. />
  176. )
  177. }
  178. renderRadioInput() {
  179. return (
  180. <RadioInput
  181. data-test-id={`endpointField-radioInput-${this.props.name}`}
  182. checked={this.props.value}
  183. label={LabelDictionary.get(this.props.name)}
  184. onChange={e => { if (this.props.onChange) this.props.onChange(e.target.checked) }}
  185. disabled={this.props.disabled}
  186. />
  187. )
  188. }
  189. renderDropdownInput() {
  190. if (!this.props.items) {
  191. return null
  192. }
  193. let items = this.props.items.map(field => {
  194. return {
  195. value: field.name,
  196. label: field.label || LabelDictionary.get(field.name),
  197. }
  198. })
  199. let fieldName = this.props.value || items[0].value
  200. return (
  201. <DropdownInput
  202. items={items}
  203. selectedItem={fieldName}
  204. onItemChange={item => { if (this.props.onChange) this.props.onChange(item.value) }}
  205. inputValue={this.props.getFieldValue ? this.props.getFieldValue(fieldName) : ''}
  206. onInputChange={value => { if (this.props.onFieldChange) this.props.onFieldChange(fieldName, value) }}
  207. placeholder={LabelDictionary.get(fieldName)}
  208. highlight={this.props.highlight}
  209. disabled={this.props.disabled}
  210. />
  211. )
  212. }
  213. renderInput() {
  214. switch (this.props.type) {
  215. case 'input-choice':
  216. return this.renderDropdownInput()
  217. case 'boolean':
  218. return this.renderSwitch()
  219. case 'string':
  220. if (this.props.enum) {
  221. return this.renderEnumDropdown()
  222. }
  223. if (this.props.useTextArea) {
  224. return this.renderTextArea()
  225. }
  226. return this.renderTextInput()
  227. case 'integer':
  228. if (this.props.minimum || this.props.maximum) {
  229. return this.renderIntDropdown()
  230. }
  231. return this.renderTextInput()
  232. case 'radio':
  233. return this.renderRadioInput()
  234. case 'array':
  235. return this.renderArrayDropdown()
  236. default:
  237. return null
  238. }
  239. }
  240. renderLabel() {
  241. if (this.props.type === 'radio') {
  242. return null
  243. }
  244. let description = LabelDictionary.getDescription(this.props.name)
  245. let infoIcon = null
  246. if (description) {
  247. infoIcon = <InfoIcon text={description} marginLeft={-20} />
  248. }
  249. return (
  250. <Label>
  251. <LabelText data-test-id="endpointField-label">{LabelDictionary.get(this.props.name)}</LabelText>
  252. {infoIcon}
  253. {this.props.required ? <Asterisk data-test-id="endpointField-required" marginLeft={description ? '4px' : '-16px'} /> : null}
  254. </Label>
  255. )
  256. }
  257. render() {
  258. return (
  259. <Wrapper className={this.props.className}>
  260. {this.renderLabel()}
  261. {this.renderInput()}
  262. </Wrapper>
  263. )
  264. }
  265. }
  266. export default Field