import * as React from 'react'
import { connect } from 'react-redux'
import { FormFieldWrapper, BaseFormField } from '.'
import { FORM_REDUCER_KEY } from '../reducers'
import { mapStateToField } from '../selectors'
import { createEmptyFormField } from '../data'
import { TextInputProps, TextInputComponentProps, AbstractFormField } from '../interfaces'
import { setFormField } from '../actions'

class _TextInput extends BaseFormField<string> {

    public renderField() {
        const { name, disabled, placeholder } = this.props

        return (
            <input
                ref={this.setFieldRef}
                className={this.classNames()}
                name={name}
                id = {`${name}Field`}
                placeholder= {placeholder}
                type= "text"
                value= {this.getValueFromState()}
                disabled= {disabled}
                onChange= {this.onChange}
                onFocus= {this.onFocus}
                onBlur= {this.onBlur} />
        )
    }
}

const mapStateToProps = (state: any, ownProps: TextInputProps) => ({
    ...ownProps,
    field: mapStateToField(state[FORM_REDUCER_KEY], ownProps),
})

const mapDispatchToProps = {
    setFormField,
}

const ConnectedTextInput = connect(mapStateToProps, mapDispatchToProps)(_TextInput)

export class TextInput extends FormFieldWrapper<TextInputProps> {
    public render() {
        return <ConnectedTextInput {...this.props} formName={this.formName} />
    }
}
