grafana/public/app/core/components/Select/UserPicker.tsx

81 lines
1.8 KiB
TypeScript
Raw Normal View History

// Libraries
import React, { Component } from 'react';
2018-12-17 07:31:43 -06:00
import _ from 'lodash';
// Components
import { AsyncSelect } from '@grafana/ui';
// Utils & Services
import { debounce } from 'lodash';
import { getBackendSrv } from 'app/core/services/backend_srv';
// Types
import { User } from 'app/types';
export interface Props {
onSelected: (user: User) => void;
className?: string;
}
export interface State {
isLoading: boolean;
}
export class UserPicker extends Component<Props, State> {
debouncedSearch: any;
constructor(props: Props) {
super(props);
this.state = { isLoading: false };
this.search = this.search.bind(this);
this.debouncedSearch = debounce(this.search, 300, {
leading: true,
trailing: true,
});
}
search(query?: string) {
const backendSrv = getBackendSrv();
this.setState({ isLoading: true });
2018-12-17 07:31:43 -06:00
if (_.isNil(query)) {
query = '';
}
return backendSrv
.get(`/api/org/users/lookup?query=${query}&limit=10`)
.then((result: any) => {
return result.map((user: any) => ({
2018-10-07 14:08:22 -05:00
id: user.userId,
value: user.userId,
label: user.login,
2018-12-11 15:17:32 -06:00
imgUrl: user.avatarUrl,
2018-10-07 14:08:22 -05:00
login: user.login,
}));
})
.finally(() => {
this.setState({ isLoading: false });
});
}
render() {
2018-10-07 14:08:22 -05:00
const { className, onSelected } = this.props;
const { isLoading } = this.state;
return (
<div className="user-picker">
2018-10-07 14:08:22 -05:00
<AsyncSelect
className={className}
isLoading={isLoading}
2018-10-07 14:08:22 -05:00
defaultOptions={true}
loadOptions={this.debouncedSearch}
2018-10-07 14:08:22 -05:00
onChange={onSelected}
placeholder="Select user"
2018-10-07 14:08:22 -05:00
noOptionsMessage={() => 'No users found'}
/>
</div>
);
}
}