rangeField.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as React from 'react';
  2. import RangeSlider from 'app/views/settings/components/forms/controls/rangeSlider';
  3. import InputField, {onEvent} from 'app/views/settings/components/forms/inputField';
  4. type DefaultProps = {
  5. formatMessageValue?: false | Function;
  6. };
  7. type DisabledFunction = (props: Omit<Props, 'formatMessageValue'>) => boolean;
  8. type PlaceholderFunction = (props: any) => React.ReactNode;
  9. type Props = DefaultProps &
  10. Omit<React.ComponentProps<typeof RangeSlider>, 'value' | 'disabled' | 'placeholder'> &
  11. Omit<InputField['props'], 'disabled' | 'field'> & {
  12. disabled?: boolean | DisabledFunction;
  13. placeholder?: string | PlaceholderFunction;
  14. };
  15. function onChange(
  16. fieldOnChange: onEvent,
  17. value: number | '',
  18. e: React.FormEvent<HTMLInputElement>
  19. ) {
  20. fieldOnChange(value, e);
  21. }
  22. function defaultFormatMessageValue(value, props: Props) {
  23. return (typeof props.formatLabel === 'function' && props.formatLabel(value)) || value;
  24. }
  25. export default function RangeField({
  26. formatMessageValue = defaultFormatMessageValue,
  27. disabled,
  28. ...otherProps
  29. }: Props) {
  30. const resolvedDisabled =
  31. typeof disabled === 'function' ? disabled(otherProps) : disabled;
  32. const props: InputField['props'] = {
  33. ...otherProps,
  34. disabled: resolvedDisabled,
  35. formatMessageValue,
  36. };
  37. return (
  38. <InputField
  39. {...props}
  40. field={({onChange: fieldOnChange, onBlur, value, ...fieldProps}) => (
  41. <RangeSlider
  42. {...fieldProps}
  43. value={value}
  44. onBlur={onBlur}
  45. onChange={(val, event) => onChange(fieldOnChange, val, event)}
  46. />
  47. )}
  48. />
  49. );
  50. }