From 329836d971e2900b9250fd98c3624fb72092fc3b Mon Sep 17 00:00:00 2001 From: Kenan Date: Tue, 23 Jan 2024 16:42:10 +0000 Subject: [PATCH] Migrate victory-shared-events to TypeScript --- .../victory-core/src/victory-util/events.ts | 6 +- packages/victory-shared-events/src/index.d.ts | 23 ---- packages/victory-shared-events/src/index.js | 1 - packages/victory-shared-events/src/index.ts | 1 + ...ed-events.js => victory-shared-events.tsx} | 100 ++++++------------ 5 files changed, 37 insertions(+), 94 deletions(-) delete mode 100644 packages/victory-shared-events/src/index.d.ts delete mode 100644 packages/victory-shared-events/src/index.js create mode 100644 packages/victory-shared-events/src/index.ts rename packages/victory-shared-events/src/{victory-shared-events.js => victory-shared-events.tsx} (82%) diff --git a/packages/victory-core/src/victory-util/events.ts b/packages/victory-core/src/victory-util/events.ts index fee5c9047..434ff8e95 100644 --- a/packages/victory-core/src/victory-util/events.ts +++ b/packages/victory-core/src/victory-util/events.ts @@ -48,10 +48,10 @@ interface ComponentWithEvents extends EventMixinCalculatedValues { export function getEvents( this: ComponentWithEvents, props, - target, - eventKey, + target?, + eventKey?, // eslint-disable-next-line no-shadow - getScopedEvents, + getScopedEvents?, ) { // Returns all events that apply to a particular target element const getEventsByTarget = (events: Array) => { diff --git a/packages/victory-shared-events/src/index.d.ts b/packages/victory-shared-events/src/index.d.ts deleted file mode 100644 index a52a12fb6..000000000 --- a/packages/victory-shared-events/src/index.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -import * as React from "react"; -import { - EventCallbackInterface, - EventPropTypeInterface, - StringOrNumberOrCallback, -} from "victory-core"; - -export type VictorySharedEventsProps = { - children?: React.ReactElement | React.ReactElement[]; - container?: React.ReactElement; - groupComponent?: React.ReactElement; - events?: EventPropTypeInterface[]; - eventKey?: StringOrNumberOrCallback; - externalEventMutations?: EventCallbackInterface< - string | string[], - string | number | (string | number)[] - >[]; -}; - -export class VictorySharedEvents extends React.Component< - VictorySharedEventsProps, - any -> {} diff --git a/packages/victory-shared-events/src/index.js b/packages/victory-shared-events/src/index.js deleted file mode 100644 index 451c001e6..000000000 --- a/packages/victory-shared-events/src/index.js +++ /dev/null @@ -1 +0,0 @@ -export { default as VictorySharedEvents } from "./victory-shared-events"; diff --git a/packages/victory-shared-events/src/index.ts b/packages/victory-shared-events/src/index.ts new file mode 100644 index 000000000..65741c217 --- /dev/null +++ b/packages/victory-shared-events/src/index.ts @@ -0,0 +1 @@ +export { VictorySharedEvents } from "./victory-shared-events"; diff --git a/packages/victory-shared-events/src/victory-shared-events.js b/packages/victory-shared-events/src/victory-shared-events.tsx similarity index 82% rename from packages/victory-shared-events/src/victory-shared-events.js rename to packages/victory-shared-events/src/victory-shared-events.tsx index b6bbdf02d..33b8ddca6 100644 --- a/packages/victory-shared-events/src/victory-shared-events.js +++ b/packages/victory-shared-events/src/victory-shared-events.tsx @@ -8,92 +8,58 @@ import { difference, } from "lodash"; import React from "react"; -import PropTypes from "prop-types"; import { - PropTypes as CustomPropTypes, + EventCallbackInterface, + EventPropTypeInterface, Events, Helpers, + StringOrNumberOrCallback, TimerContext, } from "victory-core"; import isEqual from "react-fast-compare"; import stringify from "json-stringify-safe"; +export type VictorySharedEventsProps = { + children?: React.ReactElement | React.ReactElement[]; + container?: React.ReactElement; + groupComponent?: React.ReactElement; + events?: EventPropTypeInterface[]; + eventKey?: StringOrNumberOrCallback; + externalEventMutations?: EventCallbackInterface< + string | string[], + string | number | (string | number)[] + >[]; +}; + // DISCLAIMER: // This file is not currently tested, and it is first on the list of files // to refactor in our current refactoring effort. Please do not make changes // to this file without manual testing and/or refactoring and adding tests. -export default class VictorySharedEvents extends React.Component { +export class VictorySharedEvents extends React.Component { static displayName = "VictorySharedEvents"; - static role = "shared-event-wrapper"; - - static propTypes = { - children: PropTypes.oneOfType([ - PropTypes.arrayOf(PropTypes.node), - PropTypes.node, - ]), - container: PropTypes.node, - eventKey: PropTypes.oneOfType([ - PropTypes.array, - PropTypes.func, - CustomPropTypes.allOfType([ - CustomPropTypes.integer, - CustomPropTypes.nonNegative, - ]), - PropTypes.string, - ]), - events: PropTypes.arrayOf( - PropTypes.shape({ - childName: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), - eventHandlers: PropTypes.object, - eventKey: PropTypes.oneOfType([ - PropTypes.array, - PropTypes.func, - CustomPropTypes.allOfType([ - CustomPropTypes.integer, - CustomPropTypes.nonNegative, - ]), - PropTypes.string, - ]), - target: PropTypes.string, - }), - ), - externalEventMutations: PropTypes.arrayOf( - PropTypes.shape({ - callback: PropTypes.func, - childName: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), - eventKey: PropTypes.oneOfType([ - PropTypes.array, - CustomPropTypes.allOfType([ - CustomPropTypes.integer, - CustomPropTypes.nonNegative, - ]), - PropTypes.string, - ]), - mutation: PropTypes.func, - target: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), - }), - ), - groupComponent: PropTypes.node, - }; - - static defaultProps = { - groupComponent: , - }; - static contextType = TimerContext; - constructor(props) { + getScopedEvents; + getEventState; + baseProps; + sharedEventsCache; + globalEvents; + prevGlobalEventKeys; + boundGlobalEvents; + + constructor(props: VictorySharedEventsProps) { super(props); - this.state = this.state || {}; + this.getScopedEvents = Events.getScopedEvents.bind(this); this.getEventState = Events.getEventState.bind(this); - this.baseProps = this.getBaseProps(props); + this.state = this.state || {}; this.sharedEventsCache = {}; this.globalEvents = {}; this.prevGlobalEventKeys = []; this.boundGlobalEvents = {}; + this.baseProps = this.getBaseProps(props); } shouldComponentUpdate(nextProps) { @@ -298,9 +264,9 @@ export default class VictorySharedEvents extends React.Component { getContainer(props, baseProps, events) { const children = this.getNewChildren(props, baseProps); - const parents = - Array.isArray(events) && - events.filter((event) => event.target === "parent"); + const parents = Array.isArray(events) + ? events.filter((event) => event.target === "parent") + : []; const sharedEvents = parents.length > 0 @@ -340,12 +306,12 @@ export default class VictorySharedEvents extends React.Component { : React.cloneElement(container, localEvents, children); } - render() { + render(): React.ReactElement { const events = this.getAllEvents(this.props); if (events) { return this.getContainer(this.props, this.baseProps, events); } - return React.cloneElement(this.props.container, { + return React.cloneElement(this.props.container as React.ReactElement, { children: this.props.children, }); }