@amaui/ui-react
Version:
UI for React
210 lines (207 loc) • 8.77 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["tonal", "color", "name", "values", "minSize", "maxSize", "refs", "rects", "updateDefs", "updateLegend", "minMax", "animate", "animateTimeout", "PathProps", "LegendItemProps", "className"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React from 'react';
import { is, percentageFromValueWithinRange, unique, valueFromPercentageWithinRange } from '@amaui/utils';
import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react';
import PathElement from '../Path';
import LineElement from '../Line';
import TypeElement from '../Type';
import useMediaQuery from '../useMediaQuery';
import { staticClassName, valueBreakpoints } from '../utils';
const useStyle = styleMethod(theme => ({
root: {},
legend_item: {
userSelect: 'none'
},
legend_icon: {
width: '10px',
height: '2px'
},
text: _objectSpread(_objectSpread({}, theme.typography.values.b3), {}, {
fill: 'currentColor',
textAnchor: 'middle',
alignmentBaseline: 'middle'
})
}), {
name: 'amaui-BubbleChartItem'
});
const BubbleChartItem = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiBubbleChartItem?.props?.default), props_), [props_]);
const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]);
const Path = React.useMemo(() => theme?.elements?.Path || PathElement, [theme]);
const Type = React.useMemo(() => theme?.elements?.Type || TypeElement, [theme]);
const {
tonal = true,
color = 'primary',
name,
values,
minSize,
maxSize,
refs: refs_,
rects,
updateDefs,
updateLegend,
minMax,
animate: animate_,
animateTimeout: animateTimeout_,
PathProps,
LegendItemProps,
className
} = props,
other = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const refs = {
minMax: React.useRef(undefined),
theme: React.useRef(undefined),
path: React.useRef(),
pathStyle: React.useRef({}),
animate: React.useRef(undefined),
animateTimeout: React.useRef(),
init: React.useRef(undefined)
};
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 => result.push(key));
});
return unique(result);
}, [animate_, animateTimeout_]);
const breakpoints = {};
keys.forEach(key => {
breakpoints[key] = useMediaQuery(theme.breakpoints.media[key], {
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 = React.useCallback(() => {
return /*#__PURE__*/React.createElement(Line, _extends({
gap: 1,
direction: "row",
align: "center"
}, LegendItemProps, {
className: classNames([staticClassName('BubbleChart', theme) && ['amaui-BubbleChart-legend-item'], LegendItemProps?.className, classes.legend_item])
}), /*#__PURE__*/React.createElement("span", {
className: classNames([staticClassName('BubbleChart', theme) && ['amaui-BubbleChart-legend-icon'], classes.legend_icon]),
style: {
background: !theme.palette.color[color] ? color : theme.palette.color[color]['main']
}
}), /*#__PURE__*/React.createElement(Type, {
version: "b3"
}, name));
}, [theme]);
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 [x, y, size, label] = values;
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);
const value_ = [...[values_.x, height - values_.y].map(item_ => Math.abs(item_)), size, label];
const minR = 4;
const maxR = 40;
const percentage = percentageFromValueWithinRange(value_[2], minSize, maxSize);
const r = valueFromPercentageWithinRange(percentage, minR, maxR);
const element = /*#__PURE__*/React.createElement("g", null, /*#__PURE__*/React.createElement(Path, _extends({
r: r,
cx: value_[0],
cy: value_[1],
fill: theme.methods.palette.color.colorToRgb(!theme.palette.color[color] ? color : theme.palette.color[color]['main'], 0.14),
stroke: !theme.palette.color[color] ? color : theme.palette.color[color]['main'],
strokeWidth: "2px",
Component: "circle"
}, PathProps, {
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)
})), r >= 20 && value_[3] && /*#__PURE__*/React.createElement("text", {
x: value_[0],
y: value_[1],
className: classNames([staticClassName('BubbleChartItem', theme) && ['amaui-BubbleChartItem-text'], classes.text]),
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)
}, value_[3]));
// 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__*/React.createElement(LegendItem, null)
});
}
return newValues;
});
}
// Value
setValue(element);
}
};
const initMethod = React.useCallback(() => {
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);
}
}
}, [init, animate]);
React.useEffect(() => {
make();
initMethod();
}, [values, theme, animate, init, !!refs.path.current]);
React.useEffect(() => {
make();
}, [rects?.wrapper]);
return value && /*#__PURE__*/React.cloneElement(value, _objectSpread({
className
}, other));
});
BubbleChartItem.displayName = 'amaui-BubbleChartItem';
export default BubbleChartItem;