import * as React from 'react'; import {BaseInput, IBaseInputProps, IBaseInputState} from './base-input'; import {IHTMLEvent} from './utility'; export interface ISelectInputOption { value: string; label: string; } export interface ISelectInputProps extends IBaseInputProps { options: Array; canHaveUnselected?: boolean; unselectedText?: string; } export class SelectInput extends BaseInput { protected type = 'select'; constructor(props: ISelectInputProps) { super(props); this.state = { value: this.props.value || '' } as IBaseInputState; this._onChange = this._onChange.bind(this); } public render() { const { onBlur, options, errorContainerClassName, canHaveUnselected, fieldMessage, unselectedText, ...rest } = this.props; return (
); } private mapOptionsToElements(options: Array) { const opts = options.slice(0); if (this.props.canHaveUnselected) { opts.unshift({ value: '', label: this.props.unselectedText || ''}); } return opts.map((o, i) => { return ; }); } private _onChange(e: IHTMLEvent) { const select = e.target as HTMLSelectElement; let value: string = null; if (select.selectedIndex !== -1) { value = select.options[select.selectedIndex].getAttribute('value'); } this.setState({ value }); const newEvent: IHTMLEvent = { target: { type: this.type, name: this.props.name, value }, persist: () => { /* do nothing */ } }; this.onChange(newEvent); } }