UNPKG

lucid-ui

Version:

A UI component library from Xandr.

421 lines 21.6 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExampleWithYAxisFormatter = exports.ExampleWithNoDataWithoutPreselect = exports.ExampleWithNoDataWithPreselect = exports.ExampleWithExternalXAxisRenderProp = exports.BasicDraggableLineChart = void 0; var lodash_1 = __importDefault(require("lodash")); var react_1 = __importStar(require("react")); var create_react_class_1 = __importDefault(require("create-react-class")); var DraggableLineChart_1 = __importDefault(require("./DraggableLineChart")); var TextFieldValidated_1 = __importDefault(require("../TextFieldValidated/TextFieldValidated")); exports.default = { title: 'Visualizations/DraggableLineChart', component: DraggableLineChart_1.default, parameters: { docs: { description: { component: DraggableLineChart_1.default.peek.description, }, }, }, args: DraggableLineChart_1.default.defaultProps, }; var BasicDraggableLineChart = function (args) { var data = [ { x: '12 AM', y: 0 }, { x: '1 AM', y: 0 }, { x: '2 AM', y: 0 }, { x: '3 AM', y: 0 }, { x: '4 AM', y: 0 }, { x: '5 AM', y: 5 }, { x: '6 AM', y: 5 }, { x: '7 AM', y: 10 }, { x: '8 AM', y: 5 }, { x: '9 AM', y: 5 }, { x: '10 AM', y: 5 }, { x: '11 AM', y: 5 }, ]; var style = { paddingTop: '4rem', }; return (react_1.default.createElement("div", { style: style }, react_1.default.createElement(DraggableLineChart_1.default, __assign({}, args, { onDragEnd: function (x, y) { return console.info({ x: x, y: y }); }, data: data, width: 900, xAxisTicksVertical: true })))); }; exports.BasicDraggableLineChart = BasicDraggableLineChart; /* Example With External X Axis Render Prop */ var ExampleWithExternalXAxisRenderProp = function (args) { var initialCustomSpendDataPoints = [ { x: '12 AM', y: 0, ref: react_1.default.createRef() }, { x: '1 AM', y: 1, ref: react_1.default.createRef() }, { x: '2 AM', y: 1.5, ref: react_1.default.createRef() }, { x: '3 AM', y: 2, ref: react_1.default.createRef() }, { x: '4 AM', y: 3.8, ref: react_1.default.createRef() }, { x: '5 AM', y: 3.66, ref: react_1.default.createRef() }, { x: '6 AM', y: 5, ref: react_1.default.createRef() }, { x: '7 AM', y: 10, ref: react_1.default.createRef() }, { x: '8 AM', y: 5, ref: react_1.default.createRef() }, { x: '9 AM', y: 5, ref: react_1.default.createRef() }, { x: '10 AM', y: 5, ref: react_1.default.createRef() }, { x: '11 AM', y: 5, ref: react_1.default.createRef() }, ]; var style = { paddingTop: '4rem', }; var DataInput = function (_a) { var xValue = _a.xValue, yValue = _a.yValue, myRef = _a.myRef, changeHandler = _a.changeHandler; var onChange = (0, react_1.useCallback)(function (newYValue) { changeHandler(newYValue, xValue); }, [changeHandler, xValue]); return (react_1.default.createElement("div", { style: { width: '70%', margin: 'auto' } }, react_1.default.createElement(TextFieldValidated_1.default, { value: yValue || 0, onBlur: onChange, tabIndex: 0, ref: myRef }), react_1.default.createElement("div", { style: { margin: 'auto', textAlign: 'center', width: '95%', marginTop: '15px', } }, xValue))); }; var Component = (0, create_react_class_1.default)({ getInitialState: function () { return { customSpendDataPoints: initialCustomSpendDataPoints, }; }, onDragHandler: function (newYValue, xValue, fromOnChangeHandler) { var cleanedYValue = fromOnChangeHandler ? newYValue : +Number(newYValue).toFixed(0); var newCustomSpendDataPoints = lodash_1.default.map(this.state.customSpendDataPoints, function (dataPoint) { return dataPoint.x === xValue ? __assign(__assign({}, dataPoint), { y: cleanedYValue }) : dataPoint; }); this.setState({ customSpendDataPoints: newCustomSpendDataPoints }); return newCustomSpendDataPoints; }, onChangeHandler: function (newYValue, xValue) { var currentIndex = lodash_1.default.findIndex(this.state.customSpendDataPoints, [ 'x', xValue, ]); var currentYValue = this.state.customSpendDataPoints[currentIndex].y; var nextValue = +Number(newYValue).toFixed(0); if (currentYValue !== nextValue) { var newCustomSpendDataPoints = this.onDragHandler(newYValue, xValue, true); var nextIndex = currentIndex >= newCustomSpendDataPoints.length - 1 ? 0 : currentIndex + 1; var myRef_1 = newCustomSpendDataPoints[nextIndex].ref; setTimeout(function () { return myRef_1.current.focus(); }, 1); } }, getRenderProp: function (_a, _b) { var onChangeHandler = _a.onChangeHandler; var x = _b.x, y = _b.y, ref = _b.ref; return (react_1.default.createElement(DataInput, { xValue: x, yValue: y, myRef: ref, changeHandler: onChangeHandler })); }, render: function () { var customSpendDataPoints = this.state.customSpendDataPoints; var renderProp = lodash_1.default.partial(this.getRenderProp, { onChangeHandler: this.onChangeHandler, }); return (react_1.default.createElement("div", { style: style }, react_1.default.createElement(DraggableLineChart_1.default, __assign({}, args, { data: customSpendDataPoints, width: 900, dataIsCentered: true, onDragEnd: this.onDragHandler, xAxisRenderProp: renderProp })))); }, }); return react_1.default.createElement(Component, null); }; exports.ExampleWithExternalXAxisRenderProp = ExampleWithExternalXAxisRenderProp; /* Example With No Data With Preselect */ var ExampleWithNoDataWithPreselect = function (args) { var initialCustomSpendDataPoints = [ { x: '12 AM', y: 0, ref: react_1.default.createRef() }, { x: '1 AM', y: 0, ref: react_1.default.createRef() }, { x: '2 AM', y: 0, ref: react_1.default.createRef() }, { x: '3 AM', y: 0, ref: react_1.default.createRef() }, { x: '4 AM', y: 0, ref: react_1.default.createRef() }, { x: '5 AM', y: 0, ref: react_1.default.createRef() }, { x: '6 AM', y: 0, ref: react_1.default.createRef() }, { x: '7 AM', y: 0, ref: react_1.default.createRef() }, { x: '8 AM', y: 0, ref: react_1.default.createRef() }, { x: '9 AM', y: 0, ref: react_1.default.createRef() }, { x: '10 AM', y: 0, ref: react_1.default.createRef() }, { x: '11 AM', y: 0, ref: react_1.default.createRef() }, ]; var style = { paddingTop: '4rem', }; var DataInput = function (_a) { var xValue = _a.xValue, yValue = _a.yValue, myRef = _a.myRef, changeHandler = _a.changeHandler; var onChange = (0, react_1.useCallback)(function (newYValue) { changeHandler(newYValue, xValue); }, [changeHandler, xValue]); return (react_1.default.createElement("div", { style: { width: '70%', margin: 'auto' } }, react_1.default.createElement(TextFieldValidated_1.default, { value: yValue || 0, onBlur: onChange, tabIndex: 0, ref: myRef }), react_1.default.createElement("div", { style: { margin: 'auto', textAlign: 'center', width: '95%', marginTop: '15px', } }, xValue))); }; var Component = (0, create_react_class_1.default)({ getInitialState: function () { return { customSpendDataPoints: initialCustomSpendDataPoints, }; }, onDragHandler: function (newYValue, xValue, fromOnChangeHandler) { var cleanedYValue = fromOnChangeHandler ? newYValue : +Number(newYValue).toFixed(0); var newCustomSpendDataPoints = lodash_1.default.map(this.state.customSpendDataPoints, function (dataPoint) { return dataPoint.x === xValue ? __assign(__assign({}, dataPoint), { y: cleanedYValue }) : dataPoint; }); this.setState({ customSpendDataPoints: newCustomSpendDataPoints }); return newCustomSpendDataPoints; }, onPreselectHandler: function (data) { var totalSelected = lodash_1.default.filter(data, ['isSelected', true]).length; var avg = Math.round((100 / totalSelected) * 10) / 10; var updatedData = lodash_1.default.map(data, function (step) { return ({ ref: step.ref, x: step.x, y: step.isSelected ? avg : step.y, }); }); this.setState({ customSpendDataPoints: updatedData }); }, onChangeHandler: function (newYValue, xValue) { var currentIndex = lodash_1.default.findIndex(this.state.customSpendDataPoints, [ 'x', xValue, ]); var currentYValue = this.state.customSpendDataPoints[currentIndex].y; var nextValue = +Number(newYValue).toFixed(0); if (currentYValue !== nextValue) { var newCustomSpendDataPoints = this.onDragHandler(newYValue, xValue, true); var nextIndex = currentIndex >= newCustomSpendDataPoints.length - 1 ? 0 : currentIndex + 1; var myRef_2 = newCustomSpendDataPoints[nextIndex].ref; setTimeout(function () { return myRef_2.current.focus(); }, 1); } }, getRenderProp: function (_a, _b) { var onChangeHandler = _a.onChangeHandler; var x = _b.x, y = _b.y, ref = _b.ref; return (react_1.default.createElement(DataInput, { xValue: x, yValue: y, myRef: ref, changeHandler: onChangeHandler })); }, render: function () { var customSpendDataPoints = this.state.customSpendDataPoints; var renderProp = lodash_1.default.partial(this.getRenderProp, { onChangeHandler: this.onChangeHandler, }); return (react_1.default.createElement("div", { style: style }, react_1.default.createElement(DraggableLineChart_1.default, __assign({}, args, { data: customSpendDataPoints, width: 900, dataIsCentered: true, onDragEnd: this.onDragHandler, xAxisRenderProp: renderProp, onPreselect: this.onPreselectHandler, preSelectText: 'Click and drag to select hours' })))); }, }); return react_1.default.createElement(Component, null); }; exports.ExampleWithNoDataWithPreselect = ExampleWithNoDataWithPreselect; /* Example With No Data Without Preselect */ var ExampleWithNoDataWithoutPreselect = function (args) { var initialCustomSpendDataPoints = [ { x: '12 AM', y: 0, ref: react_1.default.createRef() }, { x: '1 AM', y: 0, ref: react_1.default.createRef() }, { x: '2 AM', y: 0, ref: react_1.default.createRef() }, { x: '3 AM', y: 0, ref: react_1.default.createRef() }, { x: '4 AM', y: 0, ref: react_1.default.createRef() }, { x: '5 AM', y: 0, ref: react_1.default.createRef() }, { x: '6 AM', y: 0, ref: react_1.default.createRef() }, { x: '7 AM', y: 0, ref: react_1.default.createRef() }, { x: '8 AM', y: 0, ref: react_1.default.createRef() }, { x: '9 AM', y: 0, ref: react_1.default.createRef() }, { x: '10 AM', y: 0, ref: react_1.default.createRef() }, { x: '11 AM', y: 0, ref: react_1.default.createRef() }, ]; var style = { paddingTop: '4rem', }; var DataInput = function (_a) { var xValue = _a.xValue, yValue = _a.yValue, myRef = _a.myRef, changeHandler = _a.changeHandler; var onChange = (0, react_1.useCallback)(function (newYValue) { changeHandler(newYValue, xValue); }, [changeHandler, xValue]); return (react_1.default.createElement("div", { style: { width: '70%', margin: 'auto' } }, react_1.default.createElement(TextFieldValidated_1.default, { value: yValue || 0, onBlur: onChange, tabIndex: 0, ref: myRef }), react_1.default.createElement("div", { style: { margin: 'auto', textAlign: 'center', width: '95%', marginTop: '15px', } }, xValue))); }; var Component = (0, create_react_class_1.default)({ getInitialState: function () { return { customSpendDataPoints: initialCustomSpendDataPoints, }; }, onDragHandler: function (newYValue, xValue, fromOnChangeHandler) { var cleanedYValue = fromOnChangeHandler ? newYValue : +Number(newYValue).toFixed(0); var newCustomSpendDataPoints = lodash_1.default.map(this.state.customSpendDataPoints, function (dataPoint) { return dataPoint.x === xValue ? __assign(__assign({}, dataPoint), { y: cleanedYValue }) : dataPoint; }); this.setState({ customSpendDataPoints: newCustomSpendDataPoints }); return newCustomSpendDataPoints; }, onPreselectHandler: function (data) { this.setState({ customSpendDataPoints: data }); }, onChangeHandler: function (newYValue, xValue) { var currentIndex = lodash_1.default.findIndex(this.state.customSpendDataPoints, [ 'x', xValue, ]); var currentYValue = this.state.customSpendDataPoints[currentIndex].y; var nextValue = +Number(newYValue).toFixed(0); if (currentYValue !== nextValue) { var newCustomSpendDataPoints = this.onDragHandler(newYValue, xValue, true); var nextIndex = currentIndex >= newCustomSpendDataPoints.length - 1 ? 0 : currentIndex + 1; var myRef_3 = newCustomSpendDataPoints[nextIndex].ref; setTimeout(function () { return myRef_3.current.focus(); }, 1); } }, getRenderProp: function (_a, _b) { var onChangeHandler = _a.onChangeHandler; var x = _b.x, y = _b.y, ref = _b.ref; return (react_1.default.createElement(DataInput, { xValue: x, yValue: y, myRef: ref, changeHandler: onChangeHandler })); }, render: function () { var customSpendDataPoints = this.state.customSpendDataPoints; var renderProp = lodash_1.default.partial(this.getRenderProp, { onChangeHandler: this.onChangeHandler, }); return (react_1.default.createElement("div", { style: style }, react_1.default.createElement(DraggableLineChart_1.default, __assign({}, args, { data: customSpendDataPoints, width: 900, dataIsCentered: true, onDragEnd: this.onDragHandler, xAxisRenderProp: renderProp, onPreselect: this.onPreselectHandler })))); }, }); return react_1.default.createElement(Component, null); }; exports.ExampleWithNoDataWithoutPreselect = ExampleWithNoDataWithoutPreselect; /* Example With Y Axis Formatter */ var ExampleWithYAxisFormatter = function (args) { var initialCustomSpendDataPoints = [ { x: '12 AM', y: 0, ref: react_1.default.createRef() }, { x: '1 AM', y: 1, ref: react_1.default.createRef() }, { x: '2 AM', y: 1.5, ref: react_1.default.createRef() }, { x: '3 AM', y: 2, ref: react_1.default.createRef() }, { x: '4 AM', y: 3.8, ref: react_1.default.createRef() }, { x: '5 AM', y: 3.66, ref: react_1.default.createRef() }, { x: '6 AM', y: 5, ref: react_1.default.createRef() }, { x: '7 AM', y: 10, ref: react_1.default.createRef() }, { x: '8 AM', y: 5, ref: react_1.default.createRef() }, { x: '9 AM', y: 5, ref: react_1.default.createRef() }, { x: '10 AM', y: 5, ref: react_1.default.createRef() }, { x: '11 AM', y: 5, ref: react_1.default.createRef() }, ]; var style = { paddingTop: '4rem', }; var DataInput = function (_a) { var xValue = _a.xValue, yValue = _a.yValue, myRef = _a.myRef, changeHandler = _a.changeHandler; var onChange = (0, react_1.useCallback)(function (newYValue) { changeHandler(newYValue, xValue); }, [changeHandler, xValue]); return (react_1.default.createElement("div", { style: { width: '70%', margin: 'auto' } }, react_1.default.createElement(TextFieldValidated_1.default, { value: yValue || 0, onBlur: onChange, tabIndex: 0, ref: myRef }), react_1.default.createElement("div", { style: { margin: 'auto', textAlign: 'center', width: '95%', marginTop: '15px', } }, xValue))); }; var Component = (0, create_react_class_1.default)({ getInitialState: function () { return { customSpendDataPoints: initialCustomSpendDataPoints, }; }, onDragHandler: function (newYValue, xValue, fromOnChangeHandler) { var cleanedYValue = fromOnChangeHandler ? newYValue : +Number(newYValue).toFixed(0); var newCustomSpendDataPoints = lodash_1.default.map(this.state.customSpendDataPoints, function (dataPoint) { return dataPoint.x === xValue ? __assign(__assign({}, dataPoint), { y: cleanedYValue }) : dataPoint; }); this.setState({ customSpendDataPoints: newCustomSpendDataPoints }); return newCustomSpendDataPoints; }, onChangeHandler: function (newYValue, xValue) { var currentIndex = lodash_1.default.findIndex(this.state.customSpendDataPoints, [ 'x', xValue, ]); var currentYValue = this.state.customSpendDataPoints[currentIndex].y; var nextValue = +Number(newYValue).toFixed(0); if (currentYValue !== nextValue) { var newCustomSpendDataPoints = this.onDragHandler(newYValue, xValue, true); var nextIndex = currentIndex >= newCustomSpendDataPoints.length - 1 ? 0 : currentIndex + 1; var myRef_4 = newCustomSpendDataPoints[nextIndex].ref; setTimeout(function () { return myRef_4.current.focus(); }, 1); } }, getRenderProp: function (_a, _b) { var onChangeHandler = _a.onChangeHandler; var x = _b.x, y = _b.y, ref = _b.ref; return (react_1.default.createElement(DataInput, { xValue: x, yValue: y, myRef: ref, changeHandler: onChangeHandler })); }, render: function () { var customSpendDataPoints = this.state.customSpendDataPoints; var renderProp = lodash_1.default.partial(this.getRenderProp, { onChangeHandler: this.onChangeHandler, }); return (react_1.default.createElement("div", { style: style }, react_1.default.createElement(DraggableLineChart_1.default, __assign({}, args, { data: customSpendDataPoints, width: 900, dataIsCentered: true, onDragEnd: this.onDragHandler, xAxisRenderProp: renderProp, yAxisFormatter: function (value) { return "".concat(value, "%"); } })))); }, }); return react_1.default.createElement(Component, null); }; exports.ExampleWithYAxisFormatter = ExampleWithYAxisFormatter; //# sourceMappingURL=DraggableLineChart.stories.js.map