forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Don't allow values outside of range for number of top values (e…
- Loading branch information
Showing
4 changed files
with
133 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...s/public/indexpattern_datasource/operations/definitions/terms/values_range_input.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { shallow } from 'enzyme'; | ||
import { EuiRange } from '@elastic/eui'; | ||
import { ValuesRangeInput } from './values_range_input'; | ||
|
||
jest.mock('react-use', () => ({ | ||
useDebounce: (fn: () => void) => fn(), | ||
})); | ||
|
||
describe('ValuesRangeInput', () => { | ||
it('should render EuiRange correctly', () => { | ||
const onChangeSpy = jest.fn(); | ||
const instance = shallow(<ValuesRangeInput value={5} onChange={onChangeSpy} />); | ||
|
||
expect(instance.find(EuiRange).prop('value')).toEqual('5'); | ||
}); | ||
|
||
it('should run onChange function on update', () => { | ||
const onChangeSpy = jest.fn(); | ||
const instance = shallow(<ValuesRangeInput value={5} onChange={onChangeSpy} />); | ||
act(() => { | ||
instance.find(EuiRange).prop('onChange')!( | ||
{ currentTarget: { value: '7' } } as React.ChangeEvent<HTMLInputElement>, | ||
true | ||
); | ||
}); | ||
expect(instance.find(EuiRange).prop('value')).toEqual('7'); | ||
// useDebounce runs on initialization and on change | ||
expect(onChangeSpy.mock.calls.length).toBe(2); | ||
expect(onChangeSpy.mock.calls[0][0]).toBe(5); | ||
expect(onChangeSpy.mock.calls[1][0]).toBe(7); | ||
}); | ||
it('should not run onChange function on update when value is out of 1-100 range', () => { | ||
const onChangeSpy = jest.fn(); | ||
const instance = shallow(<ValuesRangeInput value={5} onChange={onChangeSpy} />); | ||
act(() => { | ||
instance.find(EuiRange).prop('onChange')!( | ||
{ currentTarget: { value: '107' } } as React.ChangeEvent<HTMLInputElement>, | ||
true | ||
); | ||
}); | ||
instance.update(); | ||
expect(instance.find(EuiRange).prop('value')).toEqual('107'); | ||
// useDebounce only runs on initialization | ||
expect(onChangeSpy.mock.calls.length).toBe(2); | ||
expect(onChangeSpy.mock.calls[0][0]).toBe(5); | ||
expect(onChangeSpy.mock.calls[1][0]).toBe(100); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
...s/lens/public/indexpattern_datasource/operations/definitions/terms/values_range_input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import { useDebounce } from 'react-use'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiRange } from '@elastic/eui'; | ||
|
||
export const ValuesRangeInput = ({ | ||
value, | ||
onChange, | ||
}: { | ||
value: number; | ||
onChange: (value: number) => void; | ||
}) => { | ||
const MIN_NUMBER_OF_VALUES = 1; | ||
const MAX_NUMBER_OF_VALUES = 100; | ||
|
||
const [inputValue, setInputValue] = useState(String(value)); | ||
useDebounce( | ||
() => { | ||
if (inputValue === '') { | ||
return; | ||
} | ||
const inputNumber = Number(inputValue); | ||
onChange(Math.min(MAX_NUMBER_OF_VALUES, Math.max(inputNumber, MIN_NUMBER_OF_VALUES))); | ||
}, | ||
256, | ||
[inputValue] | ||
); | ||
|
||
return ( | ||
<EuiRange | ||
min={MIN_NUMBER_OF_VALUES} | ||
max={MAX_NUMBER_OF_VALUES} | ||
step={1} | ||
value={inputValue} | ||
showInput | ||
showLabels | ||
compressed | ||
onChange={({ currentTarget }) => setInputValue(currentTarget.value)} | ||
aria-label={i18n.translate('xpack.lens.indexPattern.terms.size', { | ||
defaultMessage: 'Number of values', | ||
})} | ||
/> | ||
); | ||
}; |