inputField.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import FormField, {FormFieldProps} from 'sentry/components/forms/formField';
  2. import Input, {InputProps} from 'sentry/components/input';
  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. /**
  44. * InputField should be thought of as a "base" field, and generally not used
  45. * within the Form itself.
  46. */
  47. function InputField({field = defaultField, ...props}: InputFieldProps) {
  48. return (
  49. <FormField {...props}>
  50. {({children: _children, ...otherFieldProps}) => field(otherFieldProps)}
  51. </FormField>
  52. );
  53. }
  54. export default InputField;