diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/DateHistogramSettingsEditor.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/DateHistogramSettingsEditor.tsx index f4eac46858f..7b0fe750a8c 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/DateHistogramSettingsEditor.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/DateHistogramSettingsEditor.tsx @@ -1,4 +1,4 @@ -import React, { ComponentProps } from 'react'; +import React from 'react'; import { InlineField, Input, Select } from '@grafana/ui'; import { DateHistogram } from '../aggregations'; import { bucketAggregationConfig } from '../utils'; @@ -9,9 +9,7 @@ import { inlineFieldProps } from '.'; import { uniqueId } from 'lodash'; import { useCreatableSelectPersistedBehaviour } from '../../../hooks/useCreatableSelectPersistedBehaviour'; -type IntervalOption = Required, 'label' | 'value'>>; - -const defaultIntervalOptions: IntervalOption[] = [ +const defaultIntervalOptions: Array> = [ { label: 'auto', value: 'auto' }, { label: '10s', value: '10s' }, { label: '1m', value: '1m' }, @@ -22,12 +20,12 @@ const defaultIntervalOptions: IntervalOption[] = [ { label: '1d', value: '1d' }, ]; -const hasValue = (searchValue: IntervalOption['value']) => ({ value }: IntervalOption) => value === searchValue; +const hasValue = (searchValue: string) => ({ value }: SelectableValue) => value === searchValue; -const isValidNewOption: ComponentProps['isValidNewOption'] = ( - inputValue, - _, - options: IntervalOption[] +const isValidNewOption = ( + inputValue: string, + _: SelectableValue | null, + options: Readonly>> ) => { // TODO: would be extremely nice here to allow only template variables and values that are // valid date histogram's Interval options @@ -36,7 +34,7 @@ const isValidNewOption: ComponentProps['isValidNewOption'] = ( return !valueExists && inputValue.trim().length > 0; }; -const optionStartsWithValue: ComponentProps['filterOption'] = (option: IntervalOption, value) => +const optionStartsWithValue = (option: SelectableValue, value: string) => option.value?.startsWith(value) || false; interface Props { @@ -46,8 +44,8 @@ interface Props { export const DateHistogramSettingsEditor = ({ bucketAgg }: Props) => { const dispatch = useDispatch(); - const handleIntervalChange = (newValue: string) => - dispatch(changeBucketAggregationSetting({ bucketAgg, settingName: 'interval', newValue })); + const handleIntervalChange = ({ value }: SelectableValue) => + dispatch(changeBucketAggregationSetting({ bucketAgg, settingName: 'interval', newValue: value })); return ( <> diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx index b13e020d20f..558c819ed8c 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx @@ -48,8 +48,8 @@ export const TermsSettingsEditor = ({ bucketAgg }: Props) => { {...useCreatableSelectPersistedBehaviour({ options: sizeOptions, value: bucketAgg.settings?.size || bucketAggregationConfig.terms.defaultSettings?.size, - onChange(newValue) { - dispatch(changeBucketAggregationSetting({ bucketAgg, settingName: 'size', newValue })); + onChange({ value }) { + dispatch(changeBucketAggregationSetting({ bucketAgg, settingName: 'size', newValue: value })); }, })} /> diff --git a/public/app/plugins/datasource/elasticsearch/components/hooks/useCreatableSelectPersistedBehaviour.test.tsx b/public/app/plugins/datasource/elasticsearch/components/hooks/useCreatableSelectPersistedBehaviour.test.tsx index 737ada9d3b7..26aea76ad40 100644 --- a/public/app/plugins/datasource/elasticsearch/components/hooks/useCreatableSelectPersistedBehaviour.test.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/hooks/useCreatableSelectPersistedBehaviour.test.tsx @@ -74,7 +74,7 @@ describe('useCreatableSelectPersistedBehaviour', () => { // Should call onChange when selecting an already existing option userEvent.click(option1); - expect(onChange).toHaveBeenCalledWith('Option 1'); + expect(onChange).toHaveBeenLastCalledWith({ value: 'Option 1', label: 'Option 1' }); userEvent.click(input); @@ -82,7 +82,7 @@ describe('useCreatableSelectPersistedBehaviour', () => { userEvent.type(input, 'Option 2'); userEvent.click(screen.getByLabelText('Select option')); - expect(onChange).toHaveBeenCalledWith('Option 2'); + expect(onChange).toHaveBeenLastCalledWith({ value: 'Option 2' }); }); it('Should create an option for value if value is not in options', () => { diff --git a/public/app/plugins/datasource/elasticsearch/components/hooks/useCreatableSelectPersistedBehaviour.ts b/public/app/plugins/datasource/elasticsearch/components/hooks/useCreatableSelectPersistedBehaviour.ts index 6228bacee79..2524e183057 100644 --- a/public/app/plugins/datasource/elasticsearch/components/hooks/useCreatableSelectPersistedBehaviour.ts +++ b/public/app/plugins/datasource/elasticsearch/components/hooks/useCreatableSelectPersistedBehaviour.ts @@ -1,50 +1,43 @@ import { SelectableValue } from '@grafana/data'; -import { Select } from '@grafana/ui'; -import { ComponentProps, useState } from 'react'; +import { useState } from 'react'; const hasValue = (searchValue: T['value']) => ({ value }: T) => value === searchValue; -const getInitialState = (initialOptions: T[], initialValue?: T['value']): T[] => { +const getInitialState = (initialOptions: SelectableValue[], initialValue?: string): SelectableValue[] => { if (initialValue === undefined || initialOptions.some(hasValue(initialValue))) { return initialOptions; } - return initialOptions.concat({ - value: initialValue, - label: initialValue, - } as T); + return [ + ...initialOptions, + { + value: initialValue, + label: initialValue, + }, + ]; }; -interface Params { - options: T[]; - value?: T['value']; - onChange: (value: T['value']) => void; +interface Params { + options: SelectableValue[]; + value?: string; + onChange: (s: SelectableValue) => void; } /** * Creates the Props needed by Select to handle custom values and handles custom value creation * and the initial value when it is not present in the option array. */ -export const useCreatableSelectPersistedBehaviour = ({ - options: initialOptions, - value, - onChange, -}: Params): Pick< - ComponentProps, - 'onChange' | 'onCreateOption' | 'options' | 'allowCustomValue' | 'value' -> => { - const [options, setOptions] = useState(getInitialState(initialOptions, value)); +export const useCreatableSelectPersistedBehaviour = ({ options: initialOptions, value, onChange }: Params) => { + const [options, setOptions] = useState(getInitialState(initialOptions, value)); - const addOption = (newValue: T['value']) => setOptions([...options, { value: newValue, label: newValue } as T]); + const addOption = (newValue: string) => setOptions([...options, { value: newValue, label: newValue }]); return { - onCreateOption: (value) => { + onCreateOption: (value: string) => { addOption(value); - onChange(value); - }, - onChange: (e) => { - onChange(e.value); + onChange({ value }); }, + onChange, allowCustomValue: true, options, value, diff --git a/scripts/ci-check-strict.sh b/scripts/ci-check-strict.sh index b5cc40e8418..d1a92df923d 100755 --- a/scripts/ci-check-strict.sh +++ b/scripts/ci-check-strict.sh @@ -3,7 +3,7 @@ set -e echo -e "Collecting code stats (typescript errors & more)" -ERROR_COUNT_LIMIT=19 +ERROR_COUNT_LIMIT=17 ERROR_COUNT="$(./node_modules/.bin/tsc --project tsconfig.json --noEmit --strict true | grep -oP 'Found \K(\d+)')" if [ "$ERROR_COUNT" -gt $ERROR_COUNT_LIMIT ]; then