UNPKG

@visactor/react-vchart

Version:

The react version of VChart 4.x

49 lines (43 loc) 2.62 kB
import React from "react"; import { isNil, pickWithout } from "@visactor/vutils"; import RootChartContext from "../context/chart"; import { REACT_TO_VCHART_EVENTS, findEventProps, COMMON_EVENTK_KEYS, VCHART_TO_REACT_EVENTS } from "../eventsUtils"; import { VChart } from "@visactor/vchart"; export const createSeries = (componentName, markNames, type, registers) => { registers && registers.length && VChart.useRegisters(registers); const notSpecKeys = COMMON_EVENTK_KEYS.concat([ "updateId", "componentId" ]), Comp = props => { var _a; const context = React.useContext(RootChartContext), id = null !== (_a = props.id) && void 0 !== _a ? _a : props.componentId, bindedEvents = React.useRef({}), handleEvent = e => { const markIds = markNames.map((markName => `${id}-${markName}`)); (null == e ? void 0 : e.mark) && markIds.includes(e.mark.getUserId()) && props[VCHART_TO_REACT_EVENTS[e.event.type]](e); }, addMarkEvent = events => { events && context.chart && (bindedEvents.current && Object.keys(bindedEvents.current).forEach((eventKey => { context.chart.off(REACT_TO_VCHART_EVENTS[eventKey], bindedEvents.current[eventKey]), bindedEvents.current[eventKey] = null; })), events && Object.keys(events).forEach((eventKey => { var _a; (null === (_a = bindedEvents.current) || void 0 === _a ? void 0 : _a[eventKey]) || (context.chart.on(REACT_TO_VCHART_EVENTS[eventKey], handleEvent), bindedEvents.current || (bindedEvents.current = {}), bindedEvents.current[eventKey] = handleEvent); }))); }; return addMarkEvent(findEventProps(props)), React.useEffect((() => () => { addMarkEvent({}), bindedEvents.current = {}; }), []), null; }; return Comp.displayName = componentName, Comp.parseSpec = compProps => { var _a; const newSeriesSpec = pickWithout(compProps, notSpecKeys); var spec, seriesId; return spec = newSeriesSpec, seriesId = null !== (_a = compProps.id) && void 0 !== _a ? _a : compProps.componentId, markNames.forEach((markName => { const defaultMarkId = `${seriesId}-${markName}`; isNil(spec[markName]) ? spec[markName] = { id: defaultMarkId } : isNil(spec[markName].id) && (spec[markName].id = defaultMarkId); })), isNil(type) || (newSeriesSpec.type = type), { spec: newSeriesSpec, specName: "series" }; }, Comp; }; //# sourceMappingURL=BaseSeries.js.map