/* 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 . */ 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 '../AutocompleteDropdown' import Stepper from '../../atoms/Stepper' import type { Field, EnumItem } from '../../../@types/Field' import { isEnumSeparator } 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` ${props => (props.width ? `width: ${props.width}px;` : '')} 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 : '')} ${props => (props.disabled ? css` opacity: 0.5; ` : '')} ` 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[], enum?: EnumItem[], 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, description?: string, addNullValue?: boolean, nullableBoolean?: boolean, labelRenderer?: ((prop: string) => string) | null, style?: React.CSSProperties, } @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' } : {}} required={this.props.required} highlight={this.props.highlight} /> ) } renderTextInput() { return ( { if (this.props.onChange) this.props.onChange(e.target.value) }} placeholder={this.props.label} 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) { const 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.disabled && this.props.onChange) { this.props.onChange(value, field) } }} labelRenderer={this.props.labelRenderer} hideRequiredSymbol={this.props.layout === 'page'} disabledLoading={this.props.disabledLoading} disabled={this.props.disabled} /> ) } renderTextArea() { return (