forked from grafana/grafana
-
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.
Field config API: add slider editor (grafana#28007)
* Field config: implementation slider editor (grafana#27592) * PR-28007 feedback fixed * Field config: implementation slider editor (grafana#27592) * PR-28007 feedback fixed * processed review PR-28007 * Field config: implementation slider editor (grafana#27592) * PR-28007 feedback fixed * Field config: implementation slider editor (grafana#27592) * processed review PR-28007 * fixing leftover number[] bugs * RichHistoryQueriesTab.test fix + slider vertical feat fixed * fixed Slider.test.tsx expectation * Added @docs to prevent build-frontend-docs from failing Co-authored-by: Isa Ozler <[email protected]>
- Loading branch information
1 parent
1bff2fd
commit 85a0479
Showing
19 changed files
with
423 additions
and
181 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React, { useCallback } from 'react'; | ||
import { FieldConfigEditorProps, SliderFieldConfigSettings } from '@grafana/data'; | ||
import { Slider } from '../Slider/Slider'; | ||
|
||
export const SliderValueEditor: React.FC<FieldConfigEditorProps<number, SliderFieldConfigSettings>> = ({ | ||
value, | ||
onChange, | ||
item, | ||
}) => { | ||
const { settings } = item; | ||
const onValueAfterChange = useCallback( | ||
(value?: number) => { | ||
onChange(value); | ||
}, | ||
[onChange] | ||
); | ||
const initialValue = typeof value === 'number' ? value : typeof value === 'string' ? +value : 0; | ||
return ( | ||
<Slider | ||
value={initialValue} | ||
min={settings?.min || 0} | ||
max={settings?.max || 100} | ||
step={settings?.step} | ||
onAfterChange={onValueAfterChange} | ||
/> | ||
); | ||
}; |
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,12 @@ | ||
import { Meta, Props } from '@storybook/addon-docs/blocks'; | ||
import { RangeSliderProps } from './types'; | ||
|
||
<Meta title="MDX|RangeSlider" /> | ||
|
||
# Range-slider | ||
|
||
The `Range-slider` component is an input element where users can manipulate two values on a one-dimensional axis. | ||
|
||
`Range-slider` can be implemented in horizontal or vertical orientation. You can set the default starting values for the slider with the `value` prop. | ||
|
||
<Props of={RangeSliderProps} /> |
30 changes: 30 additions & 0 deletions
30
packages/grafana-ui/src/components/Slider/RangeSlider.story.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,30 @@ | ||
import React from 'react'; | ||
import { RangeSlider } from '@grafana/ui'; | ||
import { select, number, boolean } from '@storybook/addon-knobs'; | ||
|
||
export default { | ||
title: 'Forms/Slider/Range', | ||
component: RangeSlider, | ||
}; | ||
|
||
const getKnobs = () => { | ||
return { | ||
min: number('min', 0), | ||
max: number('max', 100), | ||
step: boolean('enable step', false), | ||
orientation: select('orientation', ['horizontal', 'vertical'], 'horizontal'), | ||
reverse: boolean('reverse', false), | ||
}; | ||
}; | ||
|
||
const SliderWrapper = () => { | ||
const { min, max, orientation, reverse, step } = getKnobs(); | ||
const stepValue = step ? 10 : undefined; | ||
return ( | ||
<div style={{ width: '200px', height: '200px' }}> | ||
<RangeSlider min={min} max={max} step={stepValue} orientation={orientation} value={[10, 20]} reverse={reverse} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const basic = () => <SliderWrapper />; |
17 changes: 17 additions & 0 deletions
17
packages/grafana-ui/src/components/Slider/RangeSlider.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,17 @@ | ||
import React from 'react'; | ||
import { RangeSlider } from './RangeSlider'; | ||
import { RangeSliderProps } from './types'; | ||
import { render } from '@testing-library/react'; | ||
|
||
const sliderProps: RangeSliderProps = { | ||
min: 10, | ||
max: 20, | ||
}; | ||
|
||
describe('RangeSlider', () => { | ||
it('renders without error', () => { | ||
expect(() => { | ||
render(<RangeSlider {...sliderProps} />); | ||
}); | ||
}); | ||
}); |
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,53 @@ | ||
import React, { FunctionComponent } from 'react'; | ||
import { Range as RangeComponent, createSliderWithTooltip } from 'rc-slider'; | ||
import { cx } from 'emotion'; | ||
import { Global } from '@emotion/core'; | ||
import { useTheme } from '../../themes/ThemeContext'; | ||
import { getStyles } from './styles'; | ||
import { RangeSliderProps } from './types'; | ||
|
||
/** | ||
* @public | ||
* | ||
* RichHistoryQueriesTab uses this Range Component | ||
*/ | ||
export const RangeSlider: FunctionComponent<RangeSliderProps> = ({ | ||
min, | ||
max, | ||
onChange, | ||
onAfterChange, | ||
orientation = 'horizontal', | ||
reverse, | ||
step, | ||
formatTooltipResult, | ||
value, | ||
tooltipAlwaysVisible = true, | ||
}) => { | ||
const isHorizontal = orientation === 'horizontal'; | ||
const theme = useTheme(); | ||
const styles = getStyles(theme, isHorizontal); | ||
const RangeWithTooltip = createSliderWithTooltip(RangeComponent); | ||
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} /> | ||
<RangeWithTooltip | ||
tipProps={{ | ||
visible: tooltipAlwaysVisible, | ||
placement: isHorizontal ? 'top' : 'right', | ||
}} | ||
min={min} | ||
max={max} | ||
step={step} | ||
defaultValue={value} | ||
tipFormatter={(value: number) => (formatTooltipResult ? formatTooltipResult(value) : value)} | ||
onChange={onChange} | ||
onAfterChange={onAfterChange} | ||
vertical={!isHorizontal} | ||
reverse={reverse} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
RangeSlider.displayName = 'Range'; |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { Meta, Props } from '@storybook/addon-docs/blocks'; | ||
import { Slider } from './Slider'; | ||
import { SliderProps } from './types'; | ||
|
||
<Meta title="MDX|Slider" /> | ||
|
||
# Slider | ||
|
||
The `Slider` component is an input element where users can manipulate one or two values on a one-dimensional axis. | ||
The `Slider` component is an input element where users can manipulate one value on a one-dimensional axis. | ||
|
||
`Slider` can be implemented in horizontal or vertical orientation. You can set the default starting value(s) for the slider with the `value` prop. | ||
`Slider` can be implemented in horizontal or vertical orientation. You can set the default starting value(s) for the slider with the `value` prop. | ||
|
||
<Props of={Slider} /> | ||
<Props of={SliderProps} /> |
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
Oops, something went wrong.