'use strict';

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

export class TextInputControl extends FormControl<any> {
    static propTypes: any = {
        ...FormControl.propTypes
    };

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

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

        type = type || 'text';

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

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