EndpointField.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 styled from 'styled-components'
  16. import PropTypes from 'prop-types'
  17. import {
  18. Switch,
  19. TextInput,
  20. Dropdown,
  21. RadioInput,
  22. InfoIcon,
  23. } from 'components'
  24. import LabelDictionary from '../../../utils/LabelDictionary'
  25. import StyleProps from '../../styleUtils/StyleProps'
  26. import Palette from '../../styleUtils/Palette'
  27. const Wrapper = styled.div``
  28. const Label = styled.div`
  29. font-size: 10px;
  30. font-weight: ${StyleProps.fontWeights.medium};
  31. color: ${Palette.grayscale[3]};
  32. text-transform: uppercase;
  33. margin-bottom: 4px;
  34. `
  35. const LabelText = styled.span`
  36. margin-right: 24px;
  37. `
  38. class Field extends React.Component {
  39. static propTypes = {
  40. name: PropTypes.string,
  41. type: PropTypes.string,
  42. value: PropTypes.any,
  43. onChange: PropTypes.func,
  44. className: PropTypes.string,
  45. minimum: PropTypes.number,
  46. maximum: PropTypes.number,
  47. password: PropTypes.bool,
  48. required: PropTypes.bool,
  49. large: PropTypes.bool,
  50. highlight: PropTypes.bool,
  51. disabled: PropTypes.bool,
  52. enum: PropTypes.array,
  53. }
  54. renderSwitch() {
  55. return (
  56. <Switch
  57. disabled={this.props.disabled}
  58. checked={this.props.value || false}
  59. onChange={checked => { this.props.onChange(checked) }}
  60. />
  61. )
  62. }
  63. renderTextInput() {
  64. return (
  65. <TextInput
  66. required={this.props.required}
  67. highlight={this.props.highlight}
  68. type={this.props.password ? 'password' : 'text'}
  69. large={this.props.large}
  70. value={this.props.value}
  71. onChange={e => { this.props.onChange(e.target.value) }}
  72. placeholder={LabelDictionary.get(this.props.name)}
  73. disabled={this.props.disabled}
  74. />
  75. )
  76. }
  77. renderEnumDropdown() {
  78. let items = this.props.enum.map(e => {
  79. return {
  80. label: LabelDictionary.get(e),
  81. value: e,
  82. }
  83. })
  84. let selectedItem = items.find(i => i.value === this.props.value)
  85. return (
  86. <Dropdown
  87. large={this.props.large}
  88. selectedItem={selectedItem}
  89. items={items}
  90. onChange={item => this.props.onChange(item.value)}
  91. disabled={this.props.disabled}
  92. />
  93. )
  94. }
  95. renderIntDropdown() {
  96. let items = []
  97. for (let i = this.props.minimum; i <= this.props.maximum; i += 1) {
  98. items.push({
  99. label: i.toString(),
  100. value: i,
  101. })
  102. }
  103. return (
  104. <Dropdown
  105. large={this.props.large}
  106. selectedItem={this.props.value}
  107. items={items}
  108. onChange={item => this.props.onChange(item.value)}
  109. disabled={this.props.disabled}
  110. />
  111. )
  112. }
  113. renderRadioInput() {
  114. return (
  115. <RadioInput
  116. checked={this.props.value}
  117. label={LabelDictionary.get(this.props.name)}
  118. onChange={e => this.props.onChange(e.target.checked)}
  119. disabled={this.props.disabled}
  120. />
  121. )
  122. }
  123. renderInput() {
  124. switch (this.props.type) {
  125. case 'boolean':
  126. return this.renderSwitch()
  127. case 'string':
  128. if (this.props.enum) {
  129. return this.renderEnumDropdown()
  130. }
  131. return this.renderTextInput()
  132. case 'integer':
  133. if (this.props.minimum || this.props.maximum) {
  134. return this.renderIntDropdown()
  135. }
  136. return this.renderTextInput()
  137. case 'radio':
  138. return this.renderRadioInput()
  139. default:
  140. return null
  141. }
  142. }
  143. renderLabel() {
  144. if (this.props.type === 'radio') {
  145. return null
  146. }
  147. let description = LabelDictionary.getDescription(this.props.name)
  148. let infoIcon = null
  149. if (description) {
  150. infoIcon = <InfoIcon text={description} marginLeft={-20} />
  151. }
  152. return (
  153. <Label>
  154. <LabelText>{LabelDictionary.get(this.props.name)}</LabelText>
  155. {infoIcon}
  156. </Label>
  157. )
  158. }
  159. render() {
  160. return (
  161. <Wrapper className={this.props.className}>
  162. {this.renderLabel()}
  163. {this.renderInput()}
  164. </Wrapper>
  165. )
  166. }
  167. }
  168. export default Field