ContentPlugin.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 styled from 'styled-components'
  17. import EndpointField from '../../../components/molecules/EndpointField'
  18. import type { Field } from '../../../types/Field'
  19. export const Wrapper = styled.div`
  20. display: flex;
  21. flex-direction: column;
  22. min-height: 0;
  23. `
  24. export const Fields = styled.div`
  25. display: flex;
  26. margin-top: 32px;
  27. flex-direction: column;
  28. overflow: auto;
  29. `
  30. export const FieldStyled = styled(EndpointField) `
  31. min-width: ${props => props.useTextArea ? '100%' : '224px'};
  32. max-width: 224px;
  33. margin-bottom: 16px;
  34. `
  35. export const Row = styled.div`
  36. display: flex;
  37. flex-shrink: 0;
  38. justify-content: space-between;
  39. `
  40. type Props = {
  41. connectionInfoSchema: Field[],
  42. validation: { valid: boolean, validation: { message: string } },
  43. invalidFields: string[],
  44. getFieldValue: (field: ?Field) => any,
  45. handleFieldChange: (field: Field, value: any) => void,
  46. disabled: boolean,
  47. cancelButtonText: string,
  48. validating: boolean,
  49. onRef: (contentPlugin: any) => void,
  50. }
  51. class ContentPlugin extends React.Component<Props> {
  52. componentDidMount() {
  53. this.props.onRef(this)
  54. }
  55. componentWillUnmount() {
  56. this.props.onRef(undefined)
  57. }
  58. findInvalidFields = () => {
  59. const invalidFields = this.props.connectionInfoSchema.filter(field => {
  60. if (field.required) {
  61. let value = this.props.getFieldValue(field)
  62. return !value
  63. }
  64. return false
  65. }).map(f => f.name)
  66. return invalidFields
  67. }
  68. renderFields() {
  69. const rows = []
  70. let lastField
  71. this.props.connectionInfoSchema.forEach((field, i) => {
  72. const currentField = (
  73. <FieldStyled
  74. {...field}
  75. large
  76. disabled={this.props.disabled}
  77. password={field.name === 'password'}
  78. highlight={this.props.invalidFields.findIndex(fn => fn === field.name) > -1}
  79. value={this.props.getFieldValue(field)}
  80. onChange={value => { this.props.handleFieldChange(field, value) }}
  81. />
  82. )
  83. if (i % 2 !== 0 && !field.useTextArea && !this.props.connectionInfoSchema[i - 1].useTextArea) {
  84. rows.push((
  85. <Row key={field.name}>
  86. {lastField}
  87. {currentField}
  88. </Row>
  89. ))
  90. } else if (field.useTextArea || i === this.props.connectionInfoSchema.length - 1) {
  91. rows.push((
  92. <Row key={field.name}>
  93. {currentField}
  94. </Row>
  95. ))
  96. }
  97. lastField = currentField
  98. })
  99. return (
  100. <Fields>
  101. {rows}
  102. </Fields>
  103. )
  104. }
  105. render() {
  106. return (
  107. <Wrapper>
  108. {this.renderFields()}
  109. </Wrapper>
  110. )
  111. }
  112. }
  113. export default ContentPlugin