FormField.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import React, { InputHTMLAttributes, FunctionComponent } from 'react';
  2. // import React, { InputHTMLAttributes } from 'react';
  3. import { FormLabel } from '../FormLabel/FormLabel';
  4. export interface Props extends InputHTMLAttributes<HTMLInputElement> {
  5. label: string;
  6. labelWidth?: number;
  7. inputWidth?: number;
  8. inputEl?: React.ReactNode;
  9. }
  10. const defaultProps = {
  11. labelWidth: 6,
  12. inputWidth: 12,
  13. };
  14. /**
  15. * Default form field including label used in Grafana UI. Default input element is simple <input />. You can also pass
  16. * custom inputEl if required in which case inputWidth and inputProps are ignored.
  17. * @param label
  18. * @param labelWidth
  19. * @param inputWidth
  20. * @param inputEl
  21. * @param inputProps
  22. * @constructor
  23. */
  24. export const FormField: FunctionComponent<Props> = ({ label, labelWidth, inputWidth, inputEl, ...inputProps }) => {
  25. return (
  26. <div className="form-field">
  27. <FormLabel width={labelWidth}>{label}</FormLabel>
  28. {inputEl || <input type="text" className={`gf-form-input width-${inputWidth}`} {...inputProps} />}
  29. </div>
  30. );
  31. };
  32. FormField.displayName = 'FormField';
  33. FormField.defaultProps = defaultProps;