From 862f2e4821f28123f8c630b67cc6def58a85c447 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Tue, 8 Oct 2019 20:54:00 +0200 Subject: [PATCH] React group by segment poc (#19436) * Add simple group by component * Make segment generic * Refactoring segments. Add support for lazy loading * Use base props * Add example with grouped options * Move examples to storybook * Fixes according to pr feedback * Cleanup * added className * Fixes according to feed back * Add query string to api so that search can be imlemented in the future --- .../src/components/Segment/Segment.story.tsx | 112 +++++++++++++++++ .../src/components/Segment/Segment.tsx | 34 ++++++ .../components/Segment/SegmentAsync.story.tsx | 113 ++++++++++++++++++ .../src/components/Segment/SegmentAsync.tsx | 54 +++++++++ .../src/components/Segment/SegmentSelect.tsx | 45 +++++++ .../src/components/Segment/index.ts | 5 + .../src/components/Segment/types.ts | 8 ++ .../components/Segment/useExpandableLabel.tsx | 26 ++++ packages/grafana-ui/src/components/index.ts | 3 + 9 files changed, 400 insertions(+) create mode 100644 packages/grafana-ui/src/components/Segment/Segment.story.tsx create mode 100644 packages/grafana-ui/src/components/Segment/Segment.tsx create mode 100644 packages/grafana-ui/src/components/Segment/SegmentAsync.story.tsx create mode 100644 packages/grafana-ui/src/components/Segment/SegmentAsync.tsx create mode 100644 packages/grafana-ui/src/components/Segment/SegmentSelect.tsx create mode 100644 packages/grafana-ui/src/components/Segment/index.ts create mode 100644 packages/grafana-ui/src/components/Segment/types.ts create mode 100644 packages/grafana-ui/src/components/Segment/useExpandableLabel.tsx 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