Files
Josh Hunt 3c6e0e8ef8 Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

79 lines
1.9 KiB
TypeScript

import { debounce, isNil } from 'lodash';
import React, { Component } from 'react';
import { SelectableValue } from '@grafana/data';
import { getBackendSrv } from '@grafana/runtime';
import { AsyncSelect } from '@grafana/ui';
import { OrgUser } from 'app/types';
export interface Props {
onSelected: (user: SelectableValue<OrgUser['userId']>) => void;
className?: string;
inputId?: 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) {
this.setState({ isLoading: true });
if (isNil(query)) {
query = '';
}
return getBackendSrv()
.get(`/api/org/users/lookup?query=${query}&limit=100`)
.then((result: OrgUser[]) => {
return result.map((user) => ({
id: user.userId,
value: user.userId,
label: user.login,
imgUrl: user.avatarUrl,
login: user.login,
}));
})
.finally(() => {
this.setState({ isLoading: false });
});
}
render() {
const { className, onSelected, inputId } = this.props;
const { isLoading } = this.state;
return (
<div className="user-picker" data-testid="userPicker">
<AsyncSelect
menuShouldPortal
isClearable
className={className}
inputId={inputId}
isLoading={isLoading}
defaultOptions={true}
loadOptions={this.debouncedSearch}
onChange={onSelected}
placeholder="Start typing to search for user"
noOptionsMessage="No users found"
aria-label="User picker"
/>
</div>
);
}
}