UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

80 lines (79 loc) 3.56 kB
import createClass from 'create-react-class'; import _ from 'lodash'; import React, { useCallback } from 'react'; import { DraggableLineChart, TextFieldValidated } from '../../../index'; const initialCustomSpendDataPoints = [ { x: '12 AM', y: 0, ref: React.createRef() }, { x: '1 AM', y: 0, ref: React.createRef() }, { x: '2 AM', y: 0, ref: React.createRef() }, { x: '3 AM', y: 0, ref: React.createRef() }, { x: '4 AM', y: 0, ref: React.createRef() }, { x: '5 AM', y: 0, ref: React.createRef() }, { x: '6 AM', y: 0, ref: React.createRef() }, { x: '7 AM', y: 0, ref: React.createRef() }, { x: '8 AM', y: 0, ref: React.createRef() }, { x: '9 AM', y: 0, ref: React.createRef() }, { x: '10 AM', y: 0, ref: React.createRef() }, { x: '11 AM', y: 0, ref: React.createRef() }, ]; const style = { paddingTop: '4rem', }; const DataInput = ({ xValue, yValue, myRef, changeHandler, }) => { const onChange = useCallback(newYValue => { changeHandler(newYValue, xValue); }, [changeHandler, xValue]); return (React.createElement("div", { style: { width: '70%', margin: 'auto' } }, React.createElement(TextFieldValidated, { value: yValue || 0, onBlur: onChange, tabIndex: 0, ref: myRef }), React.createElement("div", { style: { margin: 'auto', textAlign: 'center', width: '95%', marginTop: '15px', } }, xValue))); }; export default createClass({ getInitialState() { return { customSpendDataPoints: initialCustomSpendDataPoints, }; }, onDragHandler(newYValue, xValue, fromOnChangeHandler) { const cleanedYValue = fromOnChangeHandler ? newYValue : +Number(newYValue).toFixed(0); const newCustomSpendDataPoints = _.map(this.state.customSpendDataPoints, dataPoint => dataPoint.x === xValue ? { ...dataPoint, y: cleanedYValue } : dataPoint); this.setState({ customSpendDataPoints: newCustomSpendDataPoints }); return newCustomSpendDataPoints; }, onPreselectHandler(data) { this.setState({ customSpendDataPoints: data }); }, onChangeHandler(newYValue, xValue) { const currentIndex = _.findIndex(this.state.customSpendDataPoints, [ 'x', xValue, ]); const currentYValue = this.state.customSpendDataPoints[currentIndex].y; const nextValue = +Number(newYValue).toFixed(0); if (currentYValue !== nextValue) { const newCustomSpendDataPoints = this.onDragHandler(newYValue, xValue, true); const nextIndex = currentIndex >= newCustomSpendDataPoints.length - 1 ? 0 : currentIndex + 1; const myRef = newCustomSpendDataPoints[nextIndex].ref; setTimeout(() => myRef.current.focus(), 1); } }, getRenderProp({ onChangeHandler, }, { x, y, ref }) { return (React.createElement(DataInput, { xValue: x, yValue: y, myRef: ref, changeHandler: onChangeHandler })); }, render() { const { customSpendDataPoints } = this.state; const renderProp = _.partial(this.getRenderProp, { onChangeHandler: this.onChangeHandler, }); return (React.createElement("div", { style: style }, React.createElement(DraggableLineChart, { data: customSpendDataPoints, width: 900, dataIsCentered: true, onDragEnd: this.onDragHandler, xAxisRenderProp: renderProp, onPreselect: this.onPreselectHandler }))); }, });