| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /*
- 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 <http://www.gnu.org/licenses/>.
- */
- import React from 'react'
- import styled from 'styled-components'
- import PropTypes from 'prop-types'
- import {
- Switch,
- TextInput,
- Dropdown,
- RadioInput,
- InfoIcon,
- } from 'components'
- import LabelDictionary from '../../../utils/LabelDictionary'
- import StyleProps from '../../styleUtils/StyleProps'
- import Palette from '../../styleUtils/Palette'
- const Wrapper = styled.div``
- const Label = styled.div`
- font-size: 10px;
- font-weight: ${StyleProps.fontWeights.medium};
- color: ${Palette.grayscale[3]};
- text-transform: uppercase;
- margin-bottom: 4px;
- `
- const LabelText = styled.span`
- margin-right: 24px;
- `
- class Field extends React.Component {
- static propTypes = {
- name: PropTypes.string,
- type: PropTypes.string,
- value: PropTypes.any,
- onChange: PropTypes.func,
- className: PropTypes.string,
- minimum: PropTypes.number,
- maximum: PropTypes.number,
- password: PropTypes.bool,
- required: PropTypes.bool,
- large: PropTypes.bool,
- highlight: PropTypes.bool,
- disabled: PropTypes.bool,
- enum: PropTypes.array,
- }
- renderSwitch() {
- return (
- <Switch
- disabled={this.props.disabled}
- checked={this.props.value || false}
- onChange={checked => { this.props.onChange(checked) }}
- />
- )
- }
- renderTextInput() {
- return (
- <TextInput
- required={this.props.required}
- highlight={this.props.highlight}
- type={this.props.password ? 'password' : 'text'}
- large={this.props.large}
- value={this.props.value}
- onChange={e => { this.props.onChange(e.target.value) }}
- placeholder={LabelDictionary.get(this.props.name)}
- disabled={this.props.disabled}
- />
- )
- }
- renderEnumDropdown() {
- let items = this.props.enum.map(e => {
- return {
- label: LabelDictionary.get(e),
- value: e,
- }
- })
- let selectedItem = items.find(i => i.value === this.props.value)
- return (
- <Dropdown
- large={this.props.large}
- selectedItem={selectedItem}
- items={items}
- onChange={item => this.props.onChange(item.value)}
- disabled={this.props.disabled}
- />
- )
- }
- renderIntDropdown() {
- let items = []
- for (let i = this.props.minimum; i <= this.props.maximum; i += 1) {
- items.push({
- label: i.toString(),
- value: i,
- })
- }
- return (
- <Dropdown
- large={this.props.large}
- selectedItem={this.props.value}
- items={items}
- onChange={item => this.props.onChange(item.value)}
- disabled={this.props.disabled}
- />
- )
- }
- renderRadioInput() {
- return (
- <RadioInput
- checked={this.props.value}
- label={LabelDictionary.get(this.props.name)}
- onChange={e => this.props.onChange(e.target.checked)}
- disabled={this.props.disabled}
- />
- )
- }
- renderInput() {
- switch (this.props.type) {
- case 'boolean':
- return this.renderSwitch()
- case 'string':
- if (this.props.enum) {
- return this.renderEnumDropdown()
- }
- return this.renderTextInput()
- case 'integer':
- if (this.props.minimum || this.props.maximum) {
- return this.renderIntDropdown()
- }
- return this.renderTextInput()
- case 'radio':
- return this.renderRadioInput()
- default:
- return null
- }
- }
- renderLabel() {
- if (this.props.type === 'radio') {
- return null
- }
- let description = LabelDictionary.getDescription(this.props.name)
- let infoIcon = null
- if (description) {
- infoIcon = <InfoIcon text={description} marginLeft={-20} />
- }
- return (
- <Label>
- <LabelText>{LabelDictionary.get(this.props.name)}</LabelText>
- {infoIcon}
- </Label>
- )
- }
- render() {
- return (
- <Wrapper className={this.props.className}>
- {this.renderLabel()}
- {this.renderInput()}
- </Wrapper>
- )
- }
- }
- export default Field
|