@mui/x-charts
Version:
The community edition of MUI X Charts components.
88 lines • 3.28 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { warnOnce } from '@mui/x-internals/warning';
import { useAssertModelConsistency } from '@mui/x-internals/useAssertModelConsistency';
import useEventCallback from '@mui/utils/useEventCallback';
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
import { fastObjectShallowCompare } from '@mui/x-internals/fastObjectShallowCompare';
import { createIdentifierWithType } from "../../corePlugins/useChartSeries/useChartSeries.mjs";
export const useChartHighlight = ({
store,
params,
instance
}) => {
useAssertModelConsistency({
warningPrefix: 'MUI X Charts',
componentName: 'Chart',
propName: 'highlightedItem',
controlled: params.highlightedItem,
defaultValue: null
});
useEnhancedEffect(() => {
if (store.state.highlight.item !== params.highlightedItem) {
if (params.highlightedItem === null) {
store.set('highlight', _extends({}, store.state.highlight, {
item: null
}));
return;
}
const cleanItem = instance.identifierWithType(params.highlightedItem, 'highlightItem');
const item = instance.cleanIdentifier(cleanItem, 'highlightItem');
store.set('highlight', _extends({}, store.state.highlight, {
item
}));
}
if (process.env.NODE_ENV !== 'production') {
if (params.highlightedItem !== undefined && !store.state.highlight.isControlled) {
warnOnce(['MUI X Charts: The `highlightedItem` switched between controlled and uncontrolled state.', 'To remove the highlight when using controlled state, you must provide `null` to the `highlightedItem` prop instead of `undefined`.'].join('\n'));
}
}
}, [store, params.highlightedItem, instance]);
const clearHighlight = useEventCallback(() => {
params.onHighlightChange?.(null);
const prevHighlight = store.state.highlight;
if (prevHighlight.item === null || prevHighlight.isControlled) {
return;
}
store.set('highlight', {
item: null,
lastUpdate: 'pointer',
isControlled: false
});
});
const setHighlight = useEventCallback(newItem => {
const prevHighlight = store.state.highlight;
const identifierWithType = instance.identifierWithType(newItem, 'highlightItem');
const cleanedIdentifier = instance.cleanIdentifier(identifierWithType, 'highlightItem');
if (fastObjectShallowCompare(prevHighlight.item, cleanedIdentifier)) {
return;
}
params.onHighlightChange?.(cleanedIdentifier);
if (prevHighlight.isControlled) {
return;
}
store.set('highlight', {
item: cleanedIdentifier,
lastUpdate: 'pointer',
isControlled: false
});
});
return {
instance: {
clearHighlight,
setHighlight
}
};
};
useChartHighlight.getInitialState = (params, currentState) => ({
highlight: {
item: params.highlightedItem == null ? params.highlightedItem : createIdentifierWithType(currentState)(
// Need some as because the generic SeriesType can't be propagated to plugins methods.
params.highlightedItem),
lastUpdate: 'pointer',
isControlled: params.highlightedItem !== undefined
}
});
useChartHighlight.params = {
highlightedItem: true,
onHighlightChange: true
};