import * as React from 'react' import Icon from 'semantic-ui-react/dist/commonjs/elements/Icon/Icon' import SemanticDatepicker from 'react-semantic-ui-datepickers' import 'react-semantic-ui-datepickers/dist/react-semantic-ui-datepickers.css' import Input, { InputOnChangeData, InputProps } from 'semantic-ui-react/dist/commonjs/elements/Input/Input' import classnames from 'classnames' import { getInputValueLength } from '../../lib/input' import { Blockie } from '../Blockie/Blockie' import { Button } from '../Button/Button' import { Header } from '../Header/Header' import './Field.css' const DATE_TYPE = 'date' export type FieldProps = InputProps & { /** Input label*/ label?: string /** Boolean flag to show an error, default on false*/ error?: boolean /** Message to display below the input */ message?: React.ReactNode /** Button text to display before input to dispatch an action*/ action?: string /** On action function*/ onAction?: (event: React.MouseEvent) => void /** Style of input, default on simple*/ kind?: 'simple' | 'full' /** Boolean flag to determine if it is resizable, default on false*/ fitContent?: boolean /** Boolean flag to determine if datepicker can be cleared, default on true */ isClearable?: boolean /** Defines the maximum number of characters the user can enter into the input */ maxLength?: number } function renderMessage(props: FieldProps) { const { error, warning, info, message } = props if (error || message) { return ( <> {error && message ? : null} {message} ) } if (warning) { return ( <> {warning} ) } if (info) { return ( <> {info} ) } } export class Field extends React.PureComponent { static defaultProps: Partial = { kind: 'simple', fitContent: false } hasAction(): boolean { const { loading, error, action } = this.props const hasOnAction = this.props.onAction !== null && this.props.onAction !== undefined return !this.isAddress() && !loading && !error && action && hasOnAction } isAddress(): boolean { const { type } = this.props return type === 'address' } onChangeDatePicker = (e, data) => { let newValue = '' if (data.value) { const year: string = data.value && data.value.getFullYear() // Added 0 to dates to match format. e.g. 9-10-2000 -> 09-10-2000 const day = data.value && `${data.value.getDate() < 10 ? '0' : ''}${data.value.getDate()}` const month = data.value && `${data.value.getMonth() + 1 < 10 ? '0' : ''}${ data.value.getMonth() + 1 }` newValue = `${year}-${month}-${day}` } const inputProps: InputOnChangeData = { ...this.props, value: newValue } this.props.onChange && this.props.onChange(e, inputProps) } render(): JSX.Element { const { value, label, error, warning, message, info, type, loading, action, onAction, disabled, kind, fitContent, isClearable = true, id, maxLength, ...rest } = this.props const isAddress = this.isAddress() const icon = error && !message && !isAddress ? 'warning circle' : void 0 const classes = classnames('dcl field', kind, { error, warning: !error && warning, info: !error && !warning && info, disabled, address: isAddress, resizable: fitContent, ['has-label']: label }) if (isAddress && action) { console.warn( `The address fields don't support actions, "${action}" will be ignored` ) } return (
{label || maxLength !== undefined ? (
{label ? : null} {maxLength !== undefined ? ( {getInputValueLength(value)}/{maxLength} ) : null}
) : null} {type === DATE_TYPE ? ( ) : ( )} {this.hasAction() && (
)} {this.isAddress() && value ? : null}

{renderMessage(this.props)}  

) } }