inputField.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import Input, {InputProps} from 'sentry/components/forms/controls/input';
  2. import FormField, {FormFieldProps} from 'sentry/components/forms/formField';
  3. export interface InputFieldProps
  4. extends Omit<FormFieldProps, 'children'>,
  5. Omit<
  6. InputProps,
  7. | 'value'
  8. | 'placeholder'
  9. | 'disabled'
  10. | 'onBlur'
  11. | 'onKeyDown'
  12. | 'onChange'
  13. | 'children'
  14. | 'name'
  15. | 'defaultValue'
  16. > {
  17. // TODO(ts) Add base types for this. Each input field
  18. // has different props, but we could use have a base type that contains
  19. // the common properties.
  20. field?: (props) => React.ReactNode;
  21. value?: any;
  22. }
  23. export type onEvent = (value, event?: React.FormEvent<HTMLInputElement>) => void;
  24. function defaultField({
  25. onChange,
  26. onBlur,
  27. onKeyDown,
  28. ...rest
  29. }: {
  30. onBlur: onEvent;
  31. onChange: onEvent;
  32. onKeyDown: onEvent;
  33. }) {
  34. return (
  35. <Input
  36. onBlur={e => onBlur(e.target.value, e)}
  37. onKeyDown={e => onKeyDown((e.target as any).value, e)}
  38. onChange={e => onChange(e.target.value, e)}
  39. {...rest}
  40. />
  41. );
  42. }
  43. function InputField(props: InputFieldProps) {
  44. return (
  45. <FormField className={props.className} {...props}>
  46. {formFieldProps => {
  47. const {children: _children, ...otherFieldProps} = formFieldProps;
  48. return props.field ? props.field(otherFieldProps) : defaultField(otherFieldProps);
  49. }}
  50. </FormField>
  51. );
  52. }
  53. export default InputField;