// @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) => mixed, |}; type UnControlledProps = {| ...PropsForAll, +defaultValue: number, |}; type Props = ControlledProps | UnControlledProps; type State = {| internalValue: number, |}; class FormRatio extends React.PureComponent { state = { internalValue: !this.props.onChange ? this.props.defaultValue : 0, }; handleOnChange = (e: SyntheticInputEvent): 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 ( ); } } FormRatio.displayName = "Form.Ratio"; export default FormRatio;