Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL/ResourceSelector: Allow to set resources #12

Merged
merged 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/sql/ResourceSelector.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { ResourceSelector, ResourceSelectorProps } from './ResourceSelector';
import React from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm used to having React as first, any reason why you moved it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now using Typescript Hero to automatically add, order and remove imports. Since it's automatic, it uses the import path to order them (alphabetically). It also differentiate between packages and relative imports.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see!
I would have thought it would put default before named imports, but I don't see anything in the eslint or prettier config for it so good to go 👍

import { select } from 'react-select-event';

import { ResourceSelector, ResourceSelectorProps } from './ResourceSelector';
import { defaultKey } from './types';

const props: ResourceSelectorProps = {
Expand Down Expand Up @@ -30,4 +31,16 @@ describe('ResourceSelector', () => {
expect(fetch).toHaveBeenCalled();
expect(onChange).toHaveBeenCalledWith({ label: 'bar', value: 'bar' });
});

it('should select pre-loaded resource', async () => {
const onChange = jest.fn();
const resources = ['foo', 'bar'];
render(<ResourceSelector {...props} fetch={undefined} onChange={onChange} resources={resources} />);

const selectEl = screen.getByLabelText(props.label);
expect(selectEl).toBeInTheDocument();

await select(selectEl, 'bar', { container: document.body });
expect(onChange).toHaveBeenCalledWith({ label: 'bar', value: 'bar' });
});
});
17 changes: 12 additions & 5 deletions src/sql/ResourceSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useState, useEffect, useMemo } from 'react';
import { SelectableValue } from '@grafana/data';
import { InlineField, Select } from '@grafana/ui';
import { isEqual } from 'lodash';
import React, { useEffect, useMemo, useState } from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem import order


import { defaultKey } from './types';

export type ResourceSelectorProps = {
value: string | null;
fetch: () => Promise<Array<string | SelectableValue<string>>>;
onChange: (e: SelectableValue<string> | null) => void;
dependencies?: Array<string | null | undefined>;
tooltip?: string;
Expand All @@ -21,6 +21,9 @@ export type ResourceSelectorProps = {
labelWidth?: number;
className?: string;
saveOptions?: () => Promise<void>;
// Either set a way of fetching resources or the resource list
fetch?: () => Promise<Array<string | SelectableValue<string>>>;
resources?: string[];
};

export function ResourceSelector(props: ResourceSelectorProps) {
Expand All @@ -44,8 +47,13 @@ export function ResourceSelector(props: ResourceSelectorProps) {
}, [props.default, props.value]);
const [options, setOptions] = useState<Array<SelectableValue<string>>>(props.default ? defaultOpts : []);
useEffect(() => {
if (props.resources !== undefined) {
setResources(props.resources);
}
}, [props.resources]);
useEffect(() => {
const newOptions: Array<SelectableValue<string>> = props.default ? defaultOpts : [];
if (resources.length) {
const newOptions: Array<SelectableValue<string>> = props.default ? defaultOpts : [];
resources.forEach((r) => {
const value = typeof r === 'string' ? r : r.value;
if (!newOptions.find((o) => o.value === value)) {
Expand All @@ -62,7 +70,6 @@ export function ResourceSelector(props: ResourceSelectorProps) {
// A change in the dependencies cause a state clean-up
if (!isEqual(props.dependencies, dependencies)) {
setFetched(false);
setResources([]);
setResource(null);
props.onChange(null);
setDependencies(props.dependencies);
Expand Down Expand Up @@ -110,7 +117,7 @@ export function ResourceSelector(props: ResourceSelectorProps) {
isLoading={isLoading}
className={props.className || 'min-width-6'}
disabled={props.disabled}
onOpenMenu={onClick}
onOpenMenu={() => props.fetch && onClick()}
/>
</div>
</InlineField>
Expand Down