// Modules import * as React from 'react'; import { WrappedFieldProps, Field, BaseFieldProps, WrappedFieldMetaProps, } from 'redux-form'; import { default as classNames } from 'classnames'; // Components import Help from '@amalto/help'; import Typeahead from '@amalto/typeahead-input'; /** * Typeahead input used on a [redux-form](#reduxform). */ namespace TypeaheadFormInput { export interface Props extends BaseFieldProps { /** Input's name. */ name: string; /** Collection of item to be display inside the dropdown list. */ collection: string[]; /** Form input label. */ label?: string | JSX.Element; /** Input's placeholder. */ placeholder?: string; /** Tooltip help displayed when hovering the ? icon next to label. */ help?: string; /** CSS class wrapping the component. */ containerClass?: string; /** * Remove the bottom margin which is the default height of the error message * displayed when input is invalid. * @default false */ collapseErrorSpace?: boolean; /** Current collection type. */ selectedCollectionType?: string; /** Define the collection type which will display different results on the dropdown list. */ collectionTypes?: string[]; /** Update collection type. */ setCollectionType?: (collectionType: string) => void; /** Hide props from documentation */ /** @ignore */ children?: React.ReactNode; /** @ignore */ key?: React.ReactText; /** @ignore */ ref?: React.Ref; /** redux-form props */ /** @ignore */ component?: any; /** @ignore */ format?: any; /** @ignore */ normalize?: any; /** @ignore */ props?: any; /** @ignore */ parse?: any; /** @ignore */ validate?: any; /** @ignore */ warn?: any; /** @ignore */ withRef?: any; } } class TypeaheadFormInput extends React.Component { private renderInput = (field: WrappedFieldProps) => { const { name, containerClass, collection, placeholder, selectedCollectionType, collectionTypes, setCollectionType, } = this.props; const { input, meta } = field; return (
{this.renderLabel()} {this.renderErrorMsg(meta)}
); }; private renderLabel = (): JSX.Element | null => { const { help, label } = this.props; return label ? ( ) : null; }; private renderErrorMsg = ( meta: WrappedFieldMetaProps, ): JSX.Element | null => { const { collapseErrorSpace } = this.props; if (meta.touched && !!meta.error) { return

{meta.error}

; } return collapseErrorSpace ? null : (

 

); }; render() { const { name, format, normalize, parse, validate, warn } = this.props; const baseFieldProps: BaseFieldProps = { name, format, normalize, parse, validate, warn, }; return name ? ( ) : null; } } export default TypeaheadFormInput;