-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoc.js
More file actions
31 lines (28 loc) · 1.24 KB
/
hoc.js
File metadata and controls
31 lines (28 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React, { createElement } from 'react';
import { EndpointOptions } from 'jsplumb';
/**
* Creates a higher-ordered component for attaching jsPlumb endpoint options to a component. The endpoints will be
* available to the `plumb()` function returned from the `usePlumbContainer()` hook.
*
* @param {EndpointOptions[]} endpoints
* @param {EndpointOptions} options
* @returns {React.Component} a new component with the endpoint options as a default property
*/
export function withEndpoints(endpoints, options = {}) {
return function(Component) {
function ComponentWithEndpoints(props) {
return createElement(Component, props);
}
// We apply the endpoints to the default props so that
//
// 1) the developer doesn't need to explicitly include the prop when using the component, and
// 2) while doing so, the endpoints will be visible to the `usePlumbContainer()` hook
//
// This comes with the added advantage that, if the developer needs to override endpoint settings for certain
// instances, they can by passing in an `endpoints` property to the component.
ComponentWithEndpoints.defaultProps = {
endpoints: endpoints.map(e => ({ ...options, ...e }))
};
return ComponentWithEndpoints;
};
}