diff --git a/packages/grafana-ui/src/components/FormField/FormField.test.tsx b/packages/grafana-ui/src/components/FormField/FormField.test.tsx index 3c89a347e86..95ccb57b658 100644 --- a/packages/grafana-ui/src/components/FormField/FormField.test.tsx +++ b/packages/grafana-ui/src/components/FormField/FormField.test.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { shallow } from 'enzyme'; import { FormField, Props } from './FormField'; -const setup = (propOverrides?: object) => { +const setup = (propOverrides?: Partial) => { const props: Props = { label: 'Test', labelWidth: 11, @@ -15,10 +15,23 @@ const setup = (propOverrides?: object) => { return shallow(); }; -describe('Render', () => { - it('should render component', () => { +describe('FormField', () => { + it('should render component with default inputEl', () => { const wrapper = setup(); expect(wrapper).toMatchSnapshot(); }); + + it('should render component with custom inputEl', () => { + const wrapper = setup({ + inputEl: ( + <> + Input + + + ), + }); + + expect(wrapper).toMatchSnapshot(); + }); }); diff --git a/packages/grafana-ui/src/components/FormField/FormField.tsx b/packages/grafana-ui/src/components/FormField/FormField.tsx index 89e879ccc64..a8adeee35d1 100644 --- a/packages/grafana-ui/src/components/FormField/FormField.tsx +++ b/packages/grafana-ui/src/components/FormField/FormField.tsx @@ -1,10 +1,12 @@ import React, { InputHTMLAttributes, FunctionComponent } from 'react'; +// import React, { InputHTMLAttributes } from 'react'; import { FormLabel } from '../FormLabel/FormLabel'; export interface Props extends InputHTMLAttributes { label: string; labelWidth?: number; inputWidth?: number; + inputEl?: React.ReactNode; } const defaultProps = { @@ -12,14 +14,24 @@ const defaultProps = { inputWidth: 12, }; -const FormField: FunctionComponent = ({ label, labelWidth, inputWidth, ...inputProps }) => { +/** + * Default form field including label used in Grafana UI. Default input element is simple . You can also pass + * custom inputEl if required in which case inputWidth and inputProps are ignored. + * @param label + * @param labelWidth + * @param inputWidth + * @param inputEl + * @param inputProps + * @constructor + */ +export const FormField: FunctionComponent = ({ label, labelWidth, inputWidth, inputEl, ...inputProps }) => { return (
{label} - + {inputEl || }
); }; +FormField.displayName = 'FormField'; FormField.defaultProps = defaultProps; -export { FormField }; diff --git a/packages/grafana-ui/src/components/FormField/__snapshots__/FormField.test.tsx.snap b/packages/grafana-ui/src/components/FormField/__snapshots__/FormField.test.tsx.snap index 99eb0803149..049d927b50e 100644 --- a/packages/grafana-ui/src/components/FormField/__snapshots__/FormField.test.tsx.snap +++ b/packages/grafana-ui/src/components/FormField/__snapshots__/FormField.test.tsx.snap @@ -1,6 +1,24 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Render should render component 1`] = ` +exports[`FormField should render component with custom inputEl 1`] = ` +
+ + Test + + + Input + + +
+`; + +exports[`FormField should render component with default inputEl 1`] = `