rangeField.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import * as React from 'react';
  2. import RangeSlider from 'sentry/components/forms/controls/rangeSlider';
  3. import InputField, {InputFieldProps, onEvent} from 'sentry/components/forms/inputField';
  4. interface DefaultProps {
  5. formatMessageValue?: false | Function;
  6. }
  7. type DisabledFunction = (props: Omit<RangeFieldProps, 'formatMessageValue'>) => boolean;
  8. type PlaceholderFunction = (props: any) => React.ReactNode;
  9. export interface RangeFieldProps
  10. extends DefaultProps,
  11. Omit<
  12. React.ComponentProps<typeof RangeSlider>,
  13. 'value' | 'disabled' | 'placeholder' | 'css'
  14. >,
  15. Omit<
  16. InputFieldProps,
  17. | 'disabled'
  18. | 'field'
  19. | 'step'
  20. | 'onChange'
  21. | 'max'
  22. | 'min'
  23. | 'onBlur'
  24. | 'css'
  25. | 'formatMessageValue'
  26. > {
  27. disabled?: boolean | DisabledFunction;
  28. placeholder?: string | PlaceholderFunction;
  29. }
  30. function onChange(
  31. fieldOnChange: onEvent,
  32. value: number | '',
  33. e: React.FormEvent<HTMLInputElement>
  34. ) {
  35. fieldOnChange(value, e);
  36. }
  37. function defaultFormatMessageValue(value, props: RangeFieldProps) {
  38. return (typeof props.formatLabel === 'function' && props.formatLabel(value)) || value;
  39. }
  40. export default function RangeField({
  41. formatMessageValue = defaultFormatMessageValue,
  42. disabled,
  43. ...otherProps
  44. }: RangeFieldProps) {
  45. const resolvedDisabled =
  46. typeof disabled === 'function' ? disabled(otherProps) : disabled;
  47. const props: InputFieldProps = {
  48. ...otherProps,
  49. disabled: resolvedDisabled,
  50. formatMessageValue,
  51. };
  52. return (
  53. <InputField
  54. {...props}
  55. field={({onChange: fieldOnChange, onBlur, value, ...fieldProps}) => (
  56. <RangeSlider
  57. {...fieldProps}
  58. value={value}
  59. onBlur={onBlur}
  60. onChange={(val, event) => onChange(fieldOnChange, val, event)}
  61. />
  62. )}
  63. />
  64. );
  65. }