Skip to content

Commit

Permalink
Migrate victory-shared-events to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
KenanYusuf committed Jan 23, 2024
1 parent 1856276 commit 329836d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 94 deletions.
6 changes: 3 additions & 3 deletions packages/victory-core/src/victory-util/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComponentEvent>) => {
Expand Down
23 changes: 0 additions & 23 deletions packages/victory-shared-events/src/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/victory-shared-events/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/victory-shared-events/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { VictorySharedEvents } from "./victory-shared-events";
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, StringOrNumberOrCallback>[];
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<VictorySharedEventsProps> {
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: <g />,
};

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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
});
}
Expand Down

0 comments on commit 329836d

Please sign in to comment.