2020-10-28 15:00:31 +01:00
|
|
|
import React, { useState, useCallback, ChangeEvent, FunctionComponent } from 'react';
|
|
|
|
|
import SliderComponent from 'rc-slider';
|
2021-04-01 14:15:23 +02:00
|
|
|
import { cx } from '@emotion/css';
|
|
|
|
|
import { Global } from '@emotion/react';
|
2021-04-21 14:25:43 +02:00
|
|
|
import { useTheme2 } from '../../themes/ThemeContext';
|
2020-10-28 15:00:31 +01:00
|
|
|
import { getStyles } from './styles';
|
|
|
|
|
import { SliderProps } from './types';
|
2020-12-14 11:34:12 +01:00
|
|
|
import { Input } from '../Input/Input';
|
2020-02-28 11:02:31 +01:00
|
|
|
|
2020-10-28 15:00:31 +01:00
|
|
|
/**
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
export const Slider: FunctionComponent<SliderProps> = ({
|
2020-02-28 11:02:31 +01:00
|
|
|
min,
|
|
|
|
|
max,
|
|
|
|
|
onChange,
|
Explore: Rich History (#22570)
* Explore: Refactor active buttons css
* Explore: Add query history button
* WIP: Creating drawer
* WIP: Create custom drawer (for now)
* Revert changes to Drawer.tsx
* WIP: Layout finished
* Rich History: Set up boilerplate for Settings
* WIP: Query history cards
* Refactor, split components
* Add resizability, interactivity
* Save history to local storage
* Visualise queries from queryhistory local storage
* Set up query history settings
* Refactor
* Create link, un-refactored verison
* Copyable url
* WIP: Add slider
* Commenting feature
* Slider filtration
* Add headings
* Hide Rich history behind feature toggle
* Cleaning up, refactors
* Update tests
* Implement getQueryDisplayText
* Update lockfile for new dependencies
* Update heading based on sorting
* Fix typescript strinctNullCheck errors
* Fix Forms, new forms
* Fixes based on provided feedback
* Fixes, splitting component into two
* Add tooltips, add delete history button
* Clicking on card adds queries to query rows
* Delete history, width of drawers
* UI/UX changes, updates
* Add number of queries to headings, box shadows
* Fix slider, remove feature toggle
* Fix typo in the beta announcement
* Fix how the rich history state is rendered when initialization
* Move updateFilters when activeDatasourceFilter onlyto RichHistory, remove duplicated code
* Fix typescript strictnull errors, not used variables errors
2020-03-10 15:08:15 +01:00
|
|
|
onAfterChange,
|
2020-02-28 11:02:31 +01:00
|
|
|
orientation = 'horizontal',
|
|
|
|
|
reverse,
|
2020-09-08 09:00:16 -07:00
|
|
|
step,
|
2020-02-28 11:02:31 +01:00
|
|
|
value,
|
2021-10-12 13:26:01 +01:00
|
|
|
ariaLabelForHandle,
|
2020-02-28 11:02:31 +01:00
|
|
|
}) => {
|
|
|
|
|
const isHorizontal = orientation === 'horizontal';
|
2021-04-21 14:25:43 +02:00
|
|
|
const theme = useTheme2();
|
2020-02-28 11:02:31 +01:00
|
|
|
const styles = getStyles(theme, isHorizontal);
|
2020-10-28 15:00:31 +01:00
|
|
|
const SliderWithTooltip = SliderComponent;
|
2021-02-24 17:21:47 +01:00
|
|
|
const [sliderValue, setSliderValue] = useState<number>(value || min);
|
2020-10-28 15:00:31 +01:00
|
|
|
|
2020-12-14 11:34:12 +01:00
|
|
|
const onSliderChange = useCallback(
|
|
|
|
|
(v: number) => {
|
|
|
|
|
setSliderValue(v);
|
2020-10-28 15:00:31 +01:00
|
|
|
|
2020-12-14 11:34:12 +01:00
|
|
|
if (onChange) {
|
|
|
|
|
onChange(v);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[setSliderValue, onChange]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onSliderInputChange = useCallback(
|
|
|
|
|
(e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
let v = +e.target.value;
|
|
|
|
|
|
|
|
|
|
if (Number.isNaN(v)) {
|
|
|
|
|
v = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v > max && (v = max);
|
|
|
|
|
v < min && (v = min);
|
2020-10-28 15:00:31 +01:00
|
|
|
|
2020-12-14 11:34:12 +01:00
|
|
|
setSliderValue(v);
|
2020-10-28 15:00:31 +01:00
|
|
|
|
2020-12-14 11:34:12 +01:00
|
|
|
if (onChange) {
|
|
|
|
|
onChange(v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (onAfterChange) {
|
|
|
|
|
onAfterChange(v);
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-02-24 17:21:47 +01:00
|
|
|
[max, min, onChange, onAfterChange]
|
2020-12-14 11:34:12 +01:00
|
|
|
);
|
2020-10-28 15:00:31 +01:00
|
|
|
|
|
|
|
|
const sliderInputClassNames = !isHorizontal ? [styles.sliderInputVertical] : [];
|
|
|
|
|
const sliderInputFieldClassNames = !isHorizontal ? [styles.sliderInputFieldVertical] : [];
|
2020-12-14 11:34:12 +01:00
|
|
|
|
2020-02-28 11:02:31 +01:00
|
|
|
return (
|
|
|
|
|
<div className={cx(styles.container, styles.slider)}>
|
|
|
|
|
{/** Slider tooltip's parent component is body and therefore we need Global component to do css overrides for it. */}
|
|
|
|
|
<Global styles={styles.tooltip} />
|
2020-10-28 15:00:31 +01:00
|
|
|
<label className={cx(styles.sliderInput, ...sliderInputClassNames)}>
|
|
|
|
|
<SliderWithTooltip
|
|
|
|
|
min={min}
|
|
|
|
|
max={max}
|
|
|
|
|
step={step}
|
|
|
|
|
defaultValue={value}
|
2021-02-24 17:21:47 +01:00
|
|
|
value={sliderValue}
|
2020-10-28 15:00:31 +01:00
|
|
|
onChange={onSliderChange}
|
|
|
|
|
onAfterChange={onAfterChange}
|
|
|
|
|
vertical={!isHorizontal}
|
|
|
|
|
reverse={reverse}
|
2021-10-12 13:26:01 +01:00
|
|
|
ariaLabelForHandle={ariaLabelForHandle}
|
2020-10-28 15:00:31 +01:00
|
|
|
/>
|
2020-12-14 11:34:12 +01:00
|
|
|
{/* Uses text input so that the number spinners are not shown */}
|
|
|
|
|
<Input
|
|
|
|
|
type="text"
|
2020-10-28 15:00:31 +01:00
|
|
|
className={cx(styles.sliderInputField, ...sliderInputFieldClassNames)}
|
2021-02-24 17:21:47 +01:00
|
|
|
value={`${sliderValue}`} // to fix the react leading zero issue
|
2020-10-28 15:00:31 +01:00
|
|
|
onChange={onSliderInputChange}
|
|
|
|
|
min={min}
|
|
|
|
|
max={max}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
2020-02-28 11:02:31 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Slider.displayName = 'Slider';
|