File size: 2,408 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// @flow

import * as React from "react";
import cn from "classnames";
import { Grid } from "../";

import type { MouseEvents, FocusEvents, PointerEvents } from "../../";
// FormEvents not imported due to check on props utilization. Props typing do not use mandatory props.

type PropsForAll = {|
  ...MouseEvents,
  ...FocusEvents,
  ...PointerEvents,
  +className?: string,
  +step?: number,
  +min?: number,
  +max?: number,
|};

type ControlledProps = {|
  ...PropsForAll,
  +value: number,
  +onChange: (SyntheticInputEvent<EventTarget>) => mixed,
|};

type UnControlledProps = {|
  ...PropsForAll,
  +defaultValue: number,
|};

type Props = ControlledProps | UnControlledProps;

type State = {|
  internalValue: number,
|};

class FormRatio extends React.PureComponent<Props, State> {
  state = {
    internalValue: !this.props.onChange ? this.props.defaultValue : 0,
  };

  handleOnChange = (e: SyntheticInputEvent<EventTarget>): mixed => {
    if (this.props.onChange) {
      this.props.onChange(e);
    } else {
      const value = Number(e.target.value);
      this.setState({ internalValue: value });
    }
  };

  render(): React.Node {
    const {
      className,
      step = 1,
      min = 0,
      max = 0,
      onClick,
      onMouseEnter,
      onMouseLeave,
      onPointerEnter,
      onPointerLeave,
      onFocus,
      onBlur,
    } = this.props;
    const classes = cn(className);

    const value: number = this.props.onChange
      ? this.props.value
      : this.state.internalValue;

    return (
      <Grid.Row className={classes} alignItems="center">
        <Grid.Col>
          <input
            type="range"
            className="form-control custom-range"
            step={step}
            min={min}
            max={max}
            onChange={this.handleOnChange}
            onClick={onClick}
            onMouseEnter={onMouseEnter}
            onMouseLeave={onMouseLeave}
            onPointerEnter={onPointerEnter}
            onPointerLeave={onPointerLeave}
            value={value}
          />
        </Grid.Col>
        <Grid.Col auto>
          <input
            type="number"
            className="form-control w-8"
            value={value}
            onFocus={onFocus}
            onBlur={onBlur}
            readOnly
          />
        </Grid.Col>
      </Grid.Row>
    );
  }
}

FormRatio.displayName = "Form.Ratio";

export default FormRatio;