'use strict';

import React from 'react';
import ReactDom from 'react-dom';
import Static from 'react-bootstrap/lib/FormControlStatic';
import BSFormControl from 'react-bootstrap/lib/FormControl';
import {FormControl} from './FormControl';

export class TextInputControl extends FormControl {
    static propTypes = {
        ...FormControl.propTypes
    };

    static defaultProps = {
        ...FormControl.defaultProps,
        maxLength: 64
    };

    render() {
        let {type, ...props} = this.getPropsFromContext();

        type = type || 'text';

        return props.readOnly ? (
            <Static ref="input" {...this.getSafeProps(props)} data-test-automation-id="input">
                {props.value}
            </Static>
        ) : (
            <BSFormControl
                {...this.getSafeProps(props)}
                data-test-automation-id="input"
                type={type}
                ref="input"
            />
        );
    }

    /** @public */
    triggerFocus() {
        ReactDom.findDOMNode(this.refs.input).focus();
    }
}
