inputField.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import * as React from 'react';
  2. import omit from 'lodash/omit';
  3. import Input from 'app/views/settings/components/forms/controls/input';
  4. import FormField from 'app/views/settings/components/forms/formField';
  5. type Props = {
  6. // TODO(ts) Add base types for this. Each input field
  7. // has different props, but we could use have a base type that contains
  8. // the common properties.
  9. field?: (props) => React.ReactNode;
  10. value?: any;
  11. } & Omit<FormField['props'], 'children'> &
  12. Omit<
  13. React.ComponentPropsWithoutRef<'input'>,
  14. 'value' | 'placeholder' | 'disabled' | 'onBlur' | 'onKeyDown' | 'onChange'
  15. >;
  16. export type onEvent = (value, event?: React.FormEvent<HTMLInputElement>) => void;
  17. export default class InputField extends React.Component<Props> {
  18. static defaultProps = {
  19. field: ({
  20. onChange,
  21. onBlur,
  22. onKeyDown,
  23. ...props
  24. }: {
  25. onChange: onEvent;
  26. onBlur: onEvent;
  27. onKeyDown: onEvent;
  28. }) => (
  29. <Input
  30. {...props}
  31. onBlur={e => onBlur(e.target.value, e)}
  32. onKeyDown={e => onKeyDown((e.target as any).value, e)}
  33. onChange={e => onChange(e.target.value, e)}
  34. />
  35. ),
  36. };
  37. render() {
  38. const {className, field} = this.props;
  39. return (
  40. <FormField className={className} {...this.props}>
  41. {formFieldProps => field && field(omit(formFieldProps, 'children'))}
  42. </FormField>
  43. );
  44. }
  45. }