index.community/frontend/src/components/InstanceSearch.tsx

76 lines
2.5 KiB
TypeScript
Raw Normal View History

2018-08-27 15:27:09 +00:00
import * as React from 'react';
import { connect } from 'react-redux';
2018-08-29 00:17:08 +00:00
// import { List, ListRowProps } from 'react-virtualized';
2018-08-27 15:27:09 +00:00
import { Dispatch } from 'redux';
2018-08-29 00:17:08 +00:00
import { Button, MenuItem } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import { ItemPredicate, ItemRenderer, Select } from '@blueprintjs/select';
2018-08-27 15:27:09 +00:00
import { selectInstance } from '../redux/actions';
import { IAppState, IInstance } from '../redux/types';
interface IInstanceSearchProps {
2018-08-29 00:17:08 +00:00
currentInstance: IInstance | null;
2018-08-27 15:27:09 +00:00
instances?: IInstance[];
selectInstance: (instanceName: string) => void;
}
2018-08-29 00:17:08 +00:00
const InstanceSelect = Select.ofType<IInstance>();
2018-08-27 15:27:09 +00:00
class InstanceSearchImpl extends React.Component<IInstanceSearchProps> {
2018-08-27 21:31:27 +00:00
2018-08-27 15:27:09 +00:00
public render() {
return (
2018-08-29 00:17:08 +00:00
<InstanceSelect
items={this.props.instances && this.props.instances.slice(50) || []}
2018-08-27 15:27:09 +00:00
itemRenderer={this.itemRenderer}
onItemSelect={this.onItemSelect}
2018-08-29 00:17:08 +00:00
itemPredicate={this.itemPredicate}
disabled={!this.props.instances}
>
<Button
icon={IconNames.SELECTION}
rightIcon={IconNames.CARET_DOWN}
text={(this.props.currentInstance && this.props.currentInstance.name) || ("Select an instance")}
disabled={!this.props.instances}
/>
</InstanceSelect>
);
2018-08-27 15:27:09 +00:00
}
2018-08-29 00:17:08 +00:00
private itemRenderer: ItemRenderer<IInstance> = (item, { handleClick, modifiers }) => {
if (!modifiers.matchesPredicate) {
return null;
}
return (
<MenuItem
text={item.name}
key={item.name}
active={modifiers.active}
onClick={handleClick}
/>
);
2018-08-27 15:27:09 +00:00
}
2018-08-29 00:17:08 +00:00
private itemPredicate: ItemPredicate<IInstance> = (query, item, index) => {
if (!item.name) {
return false;
}
return item.name.toLowerCase().indexOf(query.toLowerCase()) >= 0;
2018-08-27 15:27:09 +00:00
}
2018-08-27 21:31:27 +00:00
private onItemSelect = (item: IInstance, event?: React.SyntheticEvent<HTMLElement>) => {
this.props.selectInstance(item.name);
2018-08-27 15:27:09 +00:00
}
}
const mapStateToProps = (state: IAppState) => ({
currentInstance: state.currentInstance,
instances: state.data.instances,
})
const mapDispatchToProps = (dispatch: Dispatch) => ({
selectInstance: (instanceName: string) => dispatch(selectInstance(instanceName)),
})
export const InstanceSearch = connect(mapStateToProps, mapDispatchToProps)(InstanceSearchImpl)