diff --git a/packages/grafana-ui/src/components/Segment/Segment.story.tsx b/packages/grafana-ui/src/components/Segment/Segment.story.tsx new file mode 100644 index 00000000000..949e3cdadac --- /dev/null +++ b/packages/grafana-ui/src/components/Segment/Segment.story.tsx @@ -0,0 +1,112 @@ +import React from 'react'; +import { storiesOf } from '@storybook/react'; +import { action } from '@storybook/addon-actions'; + +import { SelectableValue } from '@grafana/data'; +import { Segment } from './'; +import { UseState } from '../../utils/storybook/UseState'; + +const SegmentStories = storiesOf('UI/Segment/SegmentSync', module); + +const AddButton = ( + + + +); + +const toOption = (value: any) => ({ label: value, value: value }); + +SegmentStories.add('Array Options', () => { + const options = ['Option1', 'Option2', 'OptionWithLooongLabel', 'Option4'].map(toOption); + return ( + + {(value, updateValue) => ( + <> +
+
+ Segment Name +
+ ) => { + updateValue(value); + action('Segment value changed')(value); + }} + /> + ) => action('New value added')(value)} + options={options} + /> +
+ + )} +
+ ); +}); + +const groupedOptions = [ + { label: 'Names', options: ['Jane', 'Tom', 'Lisa'].map(toOption) }, + { label: 'Prime', options: [2, 3, 5, 7, 11, 13].map(toOption) }, +]; + +SegmentStories.add('Grouped Array Options', () => { + return ( + + {(value, updateValue) => ( + <> +
+
+ Segment Name +
+ ) => { + updateValue(value); + action('Segment value changed')(value); + }} + /> + action('New value added')(value)} + options={groupedOptions} + /> +
+ + )} +
+ ); +}); + +const CustomLabelComponent = ({ value }: any) =>
custom({value})
; + +SegmentStories.add('Custom Label Field', () => { + return ( + + {(value, setValue) => ( + <> +
+
+ Segment Name +
+ } + options={groupedOptions} + onChange={(value: SelectableValue) => { + setValue(value); + action('Segment value changed')(value); + }} + /> + action('New value added')(value)} + options={groupedOptions} + /> +
+ + )} +
+ ); +}); diff --git a/packages/grafana-ui/src/components/Segment/Segment.tsx b/packages/grafana-ui/src/components/Segment/Segment.tsx new file mode 100644 index 00000000000..9f064549634 --- /dev/null +++ b/packages/grafana-ui/src/components/Segment/Segment.tsx @@ -0,0 +1,34 @@ +import React from 'react'; +import { cx } from 'emotion'; +import { SelectableValue } from '@grafana/data'; +import { SegmentSelect, useExpandableLabel, SegmentProps } from './'; + +export interface SegmentSyncProps extends SegmentProps { + options: Array>; +} + +export function Segment({ + options, + value, + onChange, + Component, + className, +}: React.PropsWithChildren>) { + const [Label, width, expanded, setExpanded] = useExpandableLabel(false); + + if (!expanded) { + return