/*
Copyright (C) 2017 Cloudbase Solutions SRL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
// @flow
import React from 'react'
import { observer } from 'mobx-react'
import styled, { css } from 'styled-components'
import Switch from '../../atoms/Switch/Switch'
import TextInput from '../../atoms/TextInput/TextInput'
import RadioInput from '../../atoms/RadioInput/RadioInput'
import InfoIcon from '../../atoms/InfoIcon/InfoIcon'
import Dropdown from '../Dropdown/Dropdown'
import DropdownInput from '../DropdownInput/DropdownInput'
import TextArea from '../../atoms/TextArea/TextArea'
import PropertiesTable from '../PropertiesTable/PropertiesTable'
import AutocompleteDropdown from '../../molecules/AutocompleteDropdown'
import Stepper from '../../atoms/Stepper'
import type { Field } from '../../../types/Field'
import LabelDictionary from '../../../utils/LabelDictionary'
import StyleProps from '../../styleUtils/StyleProps'
import Palette from '../../styleUtils/Palette'
import asteriskImage from './images/asterisk.svg'
const Wrapper = styled.div`
${props => props.layout === 'page' ? css`
display: flex;
flex-direction: ${props.inline ? 'row' : 'column'};
${props.inline ? '' : css`justify-content: center;`}
` : ''}
`
const Label = styled.div`
font-weight: ${StyleProps.fontWeights.medium};
flex-grow: 1;
${props => props.layout === 'page' ? css`
margin-bottom: 8px;
` : css`
margin-bottom: 2px;
font-size: 10px;
color: ${Palette.grayscale[3]};
text-transform: uppercase;
display: flex;
align-items: center;
`}
${props => props.disabledLoading ? StyleProps.animations.disabledLoading : ''}
`
const LabelText = styled.span``
const Asterisk = styled.div`
${StyleProps.exactSize('16px')}
display: inline-block;
background: url('${asteriskImage}') center no-repeat;
margin-bottom: -3px;
margin-left: ${props => props.marginLeft || '0px'};
`
type Props = {
name: string,
type: string,
value: any,
onChange?: (value: any, field?: Field) => void,
valueCallback?: (field: Field) => any,
getFieldValue?: (fieldName: string) => string,
onFieldChange?: (fieldName: string, fieldValue: string) => void,
className?: string,
properties?: Field[],
// $FlowIgnore
enum?: string[] | { label: string, value: string }[] | { name: string, id: string }[],
required?: boolean,
minimum?: number,
maximum?: number,
password?: boolean,
highlight?: boolean,
disabled?: boolean,
disabledLoading?: boolean,
items?: any[],
useTextArea?: boolean,
noSelectionMessage?: string,
noItemsMessage?: string,
layout: 'modal' | 'page',
width?: number,
label?: string,
addNullValue?: boolean,
nullableBoolean?: boolean,
description?: string,
style?: { [string]: mixed },
}
@observer
class FieldInput extends React.Component {
renderSwitch(propss: { triState: boolean }) {
return (
{ if (this.props.onChange) this.props.onChange(checked) }}
leftLabel={this.props.layout === 'page'}
style={this.props.layout === 'page' ? { marginTop: '-8px' } : {}}
/>
)
}
renderTextInput() {
return (
{ if (this.props.onChange) this.props.onChange(e.target.value) }}
placeholder={LabelDictionary.get(this.props.name)}
disabled={this.props.disabled}
required={this.props.layout === 'page' ? false : this.props.required}
disabledLoading={this.props.disabledLoading}
/>
)
}
renderIntInput() {
if (this.props.minimum && this.props.maximum && this.props.maximum - this.props.minimum <= 10) {
let items = []
for (let i = this.props.minimum; i <= this.props.maximum; i += 1) {
items.push({
label: i.toString(),
value: i,
})
}
return (
{ if (this.props.onChange) this.props.onChange(item.value) }}
disabled={this.props.disabled}
disabledLoading={this.props.disabledLoading}
highlight={this.props.highlight}
required={this.props.layout === 'page' ? false : this.props.required}
/>
)
}
return (
{ if (this.props.onChange) this.props.onChange(value) }}
minimum={this.props.minimum}
maximum={this.props.maximum}
/>
)
}
renderObjectTable() {
if (!this.props.properties || !this.props.properties.length) {
return null
}
return (
this.props.valueCallback && this.props.valueCallback(field)}
onChange={(field, value) => {
if (this.props.onChange) {
this.props.onChange(value, field)
}
}}
hideRequiredSymbol={this.props.layout === 'page'}
disabledLoading={this.props.disabledLoading}
/>
)
}
renderTextArea() {
return (