import * as React from "react";
import CloseIcon from "../../../../images/close-black.svg";
import * as InputTypeaheadComponent from "../../Controls/InputTypeahead/InputTypeaheadComponent";
export interface QueryContainerComponentProps {
initialQuery: string;
latestPartialQueries: InputTypeaheadComponent.Item[];
onExecuteClick: (query: string) => void;
isLoading: boolean;
onIsValidQueryChange: (isValidQuery: boolean) => void;
}
interface QueryContainerComponentState {
query: string;
}
export class QueryContainerComponent extends React.Component<
QueryContainerComponentProps,
QueryContainerComponentState
> {
public constructor(props: QueryContainerComponentProps) {
super(props);
this.state = {
query: this.props.initialQuery,
};
}
public render(): JSX.Element {
return (
this.onNewValue(val)}
placeholder='g.V().has("name", "value")'
typeaheadOverrideOptions={{ dynamic: false }}
showSearchButton={false}
onSelected={(item: InputTypeaheadComponent.Item) => this.onNewValue(item.value)}
submitFct={(inputValue: string, selection: InputTypeaheadComponent.Item) =>
this.onSubmit(inputValue, selection)
}
useTextarea={true}
/>
{this.renderQueryInputButton()}
);
}
private static isQueryValid(query: string) {
return query.length > 0;
}
/**
* InputValue takes precedence over dropdown selection
* @param inputValue
* @param selection
*/
private onSubmit(inputValue: string, selection: InputTypeaheadComponent.Item) {
let newValue = inputValue;
if (selection && typeof newValue === "undefined") {
newValue = selection.value;
}
this.onNewValue(newValue);
if (QueryContainerComponent.isQueryValid(newValue)) {
this.props.onExecuteClick(newValue);
}
}
private onNewValue(newValue: string): void {
this.setState({ query: newValue });
this.props.onIsValidQueryChange(QueryContainerComponent.isQueryValid(newValue));
}
private onClearFilterClick(): void {
this.setState({ query: "" });
}
private renderQueryInputButton(): JSX.Element {
return (
this.onClearFilterClick()}
onKeyPress={() => this.onClearFilterClick()}
aria-label="Clear query"
>
);
}
}