// @flow import * as React from 'react'; import { FormattedMessage } from 'react-intl'; import isNaN from 'lodash/isNaN'; import DatePicker from '../../../../components/date-picker'; import SingleSelectField from '../../../../components/select-field/SingleSelectField'; import MultiSelectField from '../../../../components/select-field/MultiSelectField'; import TextInput from '../../../../components/text-input'; import { DATE, ENUM, FLOAT, MULTI_SELECT, NUMBER, STRING, VALUE } from '../../constants'; import messages from '../../messages'; import type { ConditionValueType } from '../../flowTypes'; import '../../styles/Condition.scss'; type Props = { error?: React.Node, onChange: (value: Array) => void, selectedValues: Array, valueOptions: Array, valueType: string, }; const getDateValue = selectedValues => { if (selectedValues.length === 0 || selectedValues[0] === null) { return undefined; } const value = selectedValues[0]; const date = new Date(value); if (!isNaN(date.valueOf())) { return date; } throw new Error('Expected Date'); }; const getStringValue = selectedValues => { if (selectedValues.length === 0) { return undefined; } const value = selectedValues[0]; if (typeof value === 'string') { return value !== '' ? value : null; } throw new Error('Expected string'); }; const ValueField = ({ error, onChange, selectedValues, valueOptions, valueType }: Props) => { const value = selectedValues.length > 0 ? selectedValues[0] : ''; const onInputChange = e => { return e.target.value !== '' ? onChange([e.target.value]) : onChange([]); }; switch (valueType) { case STRING: case NUMBER: case FLOAT: return (
); case DATE: return (
onChange([date])} placeholder="Date" value={getDateValue(selectedValues)} />
); case ENUM: return ( onChange([e.value])} options={valueOptions} placeholder={} selectedValue={getStringValue(selectedValues)} /> ); case MULTI_SELECT: return ( onChange(e.map(option => option.value))} options={valueOptions} placeholder={} selectedValues={selectedValues} /> ); default: return null; } }; export default ValueField;