UNPKG

@onesy/ui-react

Version:
199 lines (196 loc) 7.7 kB
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["tonal", "color", "name", "values", "refs", "rects", "updateDefs", "updateLegend", "minMax", "animate", "animateTimeout", "PathProps", "BackgroundProps", "BorderProps", "LegendItemProps", "className"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React from 'react'; import { is, percentageFromValueWithinRange, unique, valueFromPercentageWithinRange } from '@onesy/utils'; import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/style-react'; import PathElement from '../Path'; import LineElement from '../Line'; import TypeElement from '../Type'; import useMediaQuery from '../useMediaQuery'; import { staticClassName, valueBreakpoints } from '../utils'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const useStyle = styleMethod(theme => ({ root: {}, legend_item: { userSelect: 'none' }, legend_icon: { width: '8px', height: '8px', borderRadius: theme.methods.shape.radius.value(40, 'px') } }), { name: 'onesy-ScatterChartItem' }); const ScatterChartItem = props_ => { const theme = useOnesyTheme(); const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyScatterChartItem?.props?.default), props_); const Line = theme?.elements?.Line || LineElement; const Path = theme?.elements?.Path || PathElement; const Type = theme?.elements?.Type || TypeElement; const { tonal = true, color = 'primary', name, values, refs: refs_, rects, updateDefs, updateLegend, minMax, animate: animate_, animateTimeout: animateTimeout_, PathProps, BackgroundProps, BorderProps, LegendItemProps, className } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const refs = { minMax: React.useRef(null), path: React.useRef(null), theme: React.useRef(null), pathStyle: React.useRef({}), animate: React.useRef(null), animateTimeout: React.useRef(null), init: React.useRef(null) }; const keys = React.useMemo(() => { const result = []; const items = [animate_, animateTimeout_]; items.forEach(item => { if (is('object', item)) Object.keys(item).filter(key => theme.breakpoints.media[key]).forEach(key_0 => result.push(key_0)); }); return unique(result); }, [animate_, animateTimeout_]); const breakpoints = {}; keys.forEach(key_1 => { breakpoints[key_1] = useMediaQuery(theme.breakpoints.media[key_1], { element: refs.path.current }); }); const animate = valueBreakpoints(animate_, true, breakpoints, theme); const animateTimeout = valueBreakpoints(animateTimeout_, 140, breakpoints, theme); const [value, setValue] = React.useState(); const [init, setInit] = React.useState(); refs.theme.current = theme; refs.animate.current = animate; refs.animateTimeout.current = animateTimeout; refs.init.current = init; const LegendItem = () => { return /*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread({ gap: 1, direction: "row", align: "center" }, LegendItemProps), {}, { className: classNames([staticClassName('ScatterChartItem', theme) && ['onesy-ScatterChartItem-legend-item'], LegendItemProps?.className, classes.legend_item]), children: [/*#__PURE__*/_jsx("span", { className: classNames([staticClassName('ScatterChartItem', theme) && ['onesy-ScatterChartItem-legend-icon'], classes.legend_icon]), style: { background: !refs.theme.current.palette.color[color] ? color : refs.theme.current.palette.color[color]['main'] } }), /*#__PURE__*/_jsx(Type, { version: "b3", children: name })] })); }; const make = () => { // Make values into x, y, coordinates // normalized in rect width, height values // invert y so 0, 0 is at bottom left if (rects?.wrapper && values) { const { width, height } = rects.wrapper; const values_ = values // Sort for x from smallest to largest .sort((a, b) => a[0] - b[0]).map(itemValue => { const [x, y] = itemValue; const values__ = { x: percentageFromValueWithinRange(x, minMax.min.x, minMax.max.x), y: percentageFromValueWithinRange(y, minMax.min.y, minMax.max.y) }; values__.x = valueFromPercentageWithinRange(values__.x, 0, width); values__.y = valueFromPercentageWithinRange(values__.y, 0, height); return [values__.x, height - values__.y].map(item_ => Math.abs(item_)); }); const element = /*#__PURE__*/_jsx("g", { ref: refs.path, children: values_.map((item_0, index) => /*#__PURE__*/_jsx(Path, _objectSpread(_objectSpread({ cx: item_0[0], cy: item_0[1], r: 4, fill: !refs.theme.current.palette.color[color] ? color : refs.theme.current.palette.color[color]['main'], stroke: "none" }, PathProps), {}, { Component: "circle", style: _objectSpread(_objectSpread(_objectSpread({}, PathProps?.style), refs.animate.current && refs.init.current !== 'animated' && { opacity: 0, transform: 'scale(0)' }), {}, { transformBox: 'fill-box', transformOrigin: 'center' }, refs.pathStyle.current) }), index)) }); // Legend item if (is('function', updateLegend)) { updateLegend(previous => { const newValues = [...(previous || [])]; if (!newValues.find(itemLegend => itemLegend.item?.name === name)) { newValues.push({ item: { name, values }, element: /*#__PURE__*/_jsx(LegendItem, {}) }); } return newValues; }); } // Value setValue(element); } }; const initMethod = () => { if (animate) { if (!init) { refs.pathStyle.current = { transition: theme.methods.transitions.make(['transform', 'opacity'], { timing_function: 'decelerated' }) }; setInit(true); setTimeout(() => { refs.pathStyle.current = _objectSpread(_objectSpread({}, refs.pathStyle.current), {}, { opacity: 1, transform: 'scale(1)' }); setInit('animated'); }, refs.animateTimeout.current); } } }; React.useEffect(() => { make(); initMethod(); }, [values, theme, animate, init]); React.useEffect(() => { make(); }, [rects?.wrapper]); return value && /*#__PURE__*/React.cloneElement(value, _objectSpread({ className }, other)); }; ScatterChartItem.displayName = 'onesy-ScatterChartItem'; export default ScatterChartItem;