import * as React from 'react'; import classNames from 'classnames'; import {Icon} from 'superdesk-ui-framework/react'; import nextId from 'react-id-generator'; interface IProps { type?: 'expanded' | 'collapsed' | 'boxed'; placeholder: string; // defaults to light (white) initialValue?: string; focused?: boolean; onChange?(newValue: string): void; onSubmit(newValue: string): void; } interface IState { value: string; } export class SearchBar extends React.Component { constructor(props: IProps) { super(props); this.state = { value: this.props.initialValue ?? '', }; this.handleKeydown = this.handleKeydown.bind(this); this.handleChange = this.handleChange.bind(this); this.clearValue = this.clearValue.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } htmlId = nextId(); handleKeydown(event: React.KeyboardEvent) { if (event.key === 'Enter') { event.preventDefault(); this.handleSubmit(); } } handleChange(event: React.ChangeEvent) { this.setValue(event.target.value); } clearValue() { this.setValue(''); this.props.onSubmit(''); } setValue(value: string) { this.setState({value: value}); if (this.props.onChange != null) { this.props.onChange(value); } } handleSubmit() { this.props.onSubmit(this.state.value); } render() { let classes = classNames('sd-searchbar', { [`sd-searchbar--${this.props.type}`]: this.props.type, 'sd-searchbar--expanded': this.props.type === 'expanded' || this.props.type === undefined, 'sd-searchbar--focused': this.props.focused, }); return (
); } }