TeamPicker: Add optional initial value support (#76353)

* TeamPicker: Add optional initial value support

* Rename value to teamId
This commit is contained in:
Alexander Zobnin 2023-10-16 17:02:12 +02:00 committed by GitHub
parent 5a2a3ab596
commit 87d697e4fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,10 +9,12 @@ import { Team } from 'app/types';
export interface Props {
onSelected: (team: SelectableValue<Team>) => void;
className?: string;
teamId?: number;
}
export interface State {
isLoading: boolean;
value?: SelectableValue<Team>;
}
export class TeamPicker extends Component<Props, State> {
@ -29,6 +31,25 @@ export class TeamPicker extends Component<Props, State> {
});
}
componentDidMount(): void {
const { teamId } = this.props;
if (!teamId) {
return;
}
getBackendSrv()
.get(`/api/teams/${teamId}`)
.then((team: Team) => {
this.setState({
value: {
value: team,
label: team.name,
imgUrl: team.avatarUrl,
},
});
});
}
search(query?: string) {
this.setState({ isLoading: true });
@ -54,13 +75,14 @@ export class TeamPicker extends Component<Props, State> {
render() {
const { onSelected, className } = this.props;
const { isLoading } = this.state;
const { isLoading, value } = this.state;
return (
<div className="user-picker" data-testid="teamPicker">
<AsyncSelect
isLoading={isLoading}
defaultOptions={true}
loadOptions={this.debouncedSearch}
value={value}
onChange={onSelected}
className={className}
placeholder="Select a team"