diff --git a/packages/grafana-ui/src/components/Segment/Segment.story.tsx b/packages/grafana-ui/src/components/Segment/Segment.story.tsx index 3da2a4b5edd..55d215ef8c5 100644 --- a/packages/grafana-ui/src/components/Segment/Segment.story.tsx +++ b/packages/grafana-ui/src/components/Segment/Segment.story.tsx @@ -131,3 +131,21 @@ export const CustomLabelField = () => { ); }; + +export const HtmlAttributes = () => { + const [value, setValue] = useState(options[0]); + return ( + + { + setValue(value); + action('Segment value changed')(value); + }} + /> + + ); +}; diff --git a/packages/grafana-ui/src/components/Segment/Segment.tsx b/packages/grafana-ui/src/components/Segment/Segment.tsx index 7c0c77c8f33..c3a0fdc40af 100644 --- a/packages/grafana-ui/src/components/Segment/Segment.tsx +++ b/packages/grafana-ui/src/components/Segment/Segment.tsx @@ -1,10 +1,10 @@ -import React from 'react'; +import React, { HTMLProps } from 'react'; import { cx } from 'emotion'; import _ from 'lodash'; import { SelectableValue } from '@grafana/data'; import { SegmentSelect, useExpandableLabel, SegmentProps } from './'; -export interface SegmentSyncProps extends SegmentProps { +export interface SegmentSyncProps extends SegmentProps, Omit, 'value' | 'onChange'> { value?: T | SelectableValue; onChange: (item: SelectableValue) => void; options: Array>; @@ -18,6 +18,7 @@ export function Segment({ className, allowCustomValue, placeholder, + ...rest }: React.PropsWithChildren>) { const [Label, width, expanded, setExpanded] = useExpandableLabel(false); @@ -38,6 +39,7 @@ export function Segment({ return ( { ); }; + +export const HtmlAttributes = () => { + const [value, setValue] = useState(options[0]); + return ( + loadOptions(options)}> + loadOptions(options)} + onChange={item => { + setValue(item); + action('Segment value changed')(item.value); + }} + /> + + ); +}; diff --git a/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx b/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx index 6e09ce1a990..ed6cf2af569 100644 --- a/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx +++ b/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { HTMLProps } from 'react'; import { cx } from 'emotion'; import _ from 'lodash'; import { SegmentSelect } from './SegmentSelect'; @@ -7,7 +7,7 @@ import { useExpandableLabel, SegmentProps } from '.'; import { useAsyncFn } from 'react-use'; import { AsyncState } from 'react-use/lib/useAsync'; -export interface SegmentAsyncProps extends SegmentProps { +export interface SegmentAsyncProps extends SegmentProps, Omit, 'value' | 'onChange'> { value?: T | SelectableValue; loadOptions: (query?: string) => Promise>>; onChange: (item: SelectableValue) => void; @@ -21,6 +21,7 @@ export function SegmentAsync({ className, allowCustomValue, placeholder, + ...rest }: React.PropsWithChildren>) { const [state, fetchOptions] = useAsyncFn(loadOptions, [loadOptions]); const [Label, width, expanded, setExpanded] = useExpandableLabel(false); @@ -43,6 +44,7 @@ export function SegmentAsync({ return ( { ); }; +export const BasicInputWithHtmlAttributes = () => { + const [value, setValue] = useState('some text'); + return ( + + { + setValue(text as string); + action('Segment value changed')(text); + }} + /> + + ); +}; + const InputComponent = ({ initialValue }: any) => { const [value, setValue] = useState(initialValue); return ( diff --git a/packages/grafana-ui/src/components/Segment/SegmentInput.tsx b/packages/grafana-ui/src/components/Segment/SegmentInput.tsx index 054cecf1455..93516b4e823 100644 --- a/packages/grafana-ui/src/components/Segment/SegmentInput.tsx +++ b/packages/grafana-ui/src/components/Segment/SegmentInput.tsx @@ -1,10 +1,10 @@ -import React, { useRef, useState } from 'react'; +import React, { HTMLProps, useRef, useState } from 'react'; import { cx, css } from 'emotion'; import useClickAway from 'react-use/lib/useClickAway'; import { measureText } from '../../utils/measureText'; import { useExpandableLabel, SegmentProps } from '.'; -export interface SegmentInputProps extends SegmentProps { +export interface SegmentInputProps extends SegmentProps, Omit, 'value' | 'onChange'> { value: string | number; onChange: (text: string | number) => void; autofocus?: boolean; @@ -19,6 +19,7 @@ export function SegmentInput({ className, placeholder, autofocus = false, + ...rest }: React.PropsWithChildren>) { const ref = useRef(null); const [value, setValue] = useState(initialValue); @@ -50,6 +51,7 @@ export function SegmentInput({ return ( { +export interface Props extends Omit, 'value' | 'onChange'> { value?: SelectableValue; options: Array>; onChange: (item: SelectableValue) => void; @@ -22,6 +22,7 @@ export function SegmentSelect({ width, noOptionsMessage = '', allowCustomValue = false, + ...rest }: React.PropsWithChildren>) { const ref = useRef(null); @@ -39,7 +40,7 @@ export function SegmentSelect({ }); return ( -
+