-
Notifications
You must be signed in to change notification settings - Fork 525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace addEvents
HOC with custom hook
#2007
Changes from 9 commits
465ed0a
dbb16f4
4cd6398
85c2c3a
35409f9
cea1757
a1969b5
f80f94b
8dc57cc
4dde45e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable react/no-multi-comp */ | ||
import PropTypes from "prop-types"; | ||
import React from "react"; | ||
import { getBaseProps } from "./helper-methods"; | ||
|
@@ -8,9 +9,10 @@ import { | |
VictoryContainer, | ||
VictoryTheme, | ||
CommonProps, | ||
addEvents, | ||
useEvents, | ||
Data, | ||
Domain | ||
Domain, | ||
VictoryTransition | ||
} from "victory-core"; | ||
|
||
const fallbackProps = { | ||
|
@@ -26,99 +28,130 @@ const defaultData = [ | |
{ x: 4, y: 4 } | ||
]; | ||
|
||
class VictoryBar extends React.Component { | ||
static animationWhitelist = [ | ||
"data", | ||
"domain", | ||
"height", | ||
"padding", | ||
"style", | ||
"width" | ||
]; | ||
|
||
static displayName = "VictoryBar"; | ||
|
||
static role = "bar"; | ||
|
||
static defaultTransitions = { | ||
onLoad: { | ||
duration: 2000, | ||
before: () => ({ _y: 0, _y1: 0, _y0: 0 }), | ||
after: (datum) => ({ _y: datum._y, _y1: datum._y1, _y0: datum._y0 }) | ||
}, | ||
onExit: { | ||
duration: 500, | ||
before: () => ({ _y: 0, yOffset: 0 }) | ||
}, | ||
onEnter: { | ||
duration: 500, | ||
before: () => ({ _y: 0, _y1: 0, _y0: 0 }), | ||
after: (datum) => ({ _y: datum._y, _y1: datum._y1, _y0: datum._y0 }) | ||
} | ||
}; | ||
|
||
static propTypes = { | ||
...CommonProps.baseProps, | ||
...CommonProps.dataProps, | ||
alignment: PropTypes.oneOf(["start", "middle", "end"]), | ||
barRatio: PropTypes.number, | ||
barWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
cornerRadius: PropTypes.oneOfType([ | ||
PropTypes.number, | ||
PropTypes.func, | ||
PropTypes.shape({ | ||
top: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
topLeft: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
topRight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
bottom: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
bottomLeft: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
bottomRight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]) | ||
}) | ||
]), | ||
getPath: PropTypes.func, | ||
horizontal: PropTypes.bool | ||
}; | ||
|
||
static defaultProps = { | ||
containerComponent: <VictoryContainer />, | ||
data: defaultData, | ||
dataComponent: <Bar />, | ||
groupComponent: <g role="presentation" />, | ||
labelComponent: <VictoryLabel />, | ||
samples: 50, | ||
sortOrder: "ascending", | ||
standalone: true, | ||
theme: VictoryTheme.grayscale | ||
}; | ||
|
||
static getDomain = Domain.getDomainWithZero; | ||
static getData = Data.getData; | ||
static getBaseProps = (props) => getBaseProps(props, fallbackProps); | ||
static expectedComponents = [ | ||
"dataComponent", | ||
"labelComponent", | ||
"groupComponent", | ||
"containerComponent" | ||
]; | ||
|
||
// Overridden in native versions | ||
shouldAnimate() { | ||
return !!this.props.animate; | ||
const VictoryBar = (props) => { | ||
const modifiedProps = React.useMemo( | ||
() => Helpers.modifyProps(props, fallbackProps, "bar"), | ||
[props] | ||
); | ||
|
||
const { renderedData, renderContainer } = useEvents(modifiedProps, { | ||
expectedComponents: VictoryBar.expectedComponents, | ||
getBaseProps: VictoryBar.getBaseProps, | ||
role: "bar", | ||
animationWhitelist: VictoryBar.animationWhitelist | ||
}); | ||
|
||
return props.standalone | ||
? renderContainer(props.containerComponent, renderedData) | ||
: renderedData; | ||
}; | ||
|
||
VictoryBar.animationWhitelist = [ | ||
"data", | ||
"domain", | ||
"height", | ||
"padding", | ||
"style", | ||
"width" | ||
]; | ||
|
||
VictoryBar.displayName = "VictoryBar"; | ||
|
||
VictoryBar.role = "bar"; | ||
|
||
VictoryBar.defaultTransitions = { | ||
onLoad: { | ||
duration: 2000, | ||
before: () => ({ _y: 0, _y1: 0, _y0: 0 }), | ||
after: (datum) => ({ _y: datum._y, _y1: datum._y1, _y0: datum._y0 }) | ||
}, | ||
onExit: { | ||
duration: 500, | ||
before: () => ({ _y: 0, yOffset: 0 }) | ||
}, | ||
onEnter: { | ||
duration: 500, | ||
before: () => ({ _y: 0, _y1: 0, _y0: 0 }), | ||
after: (datum) => ({ _y: datum._y, _y1: datum._y1, _y0: datum._y0 }) | ||
}, | ||
onMove: { | ||
duration: 500 | ||
} | ||
}; | ||
|
||
render() { | ||
const { animationWhitelist, role } = VictoryBar; | ||
const props = Helpers.modifyProps(this.props, fallbackProps, role); | ||
VictoryBar.propTypes = { | ||
...CommonProps.baseProps, | ||
...CommonProps.dataProps, | ||
alignment: PropTypes.oneOf(["start", "middle", "end"]), | ||
barRatio: PropTypes.number, | ||
barWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
cornerRadius: PropTypes.oneOfType([ | ||
PropTypes.number, | ||
PropTypes.func, | ||
PropTypes.shape({ | ||
top: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
topLeft: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
topRight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
bottom: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
bottomLeft: PropTypes.oneOfType([PropTypes.number, PropTypes.func]), | ||
bottomRight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]) | ||
}) | ||
]), | ||
getPath: PropTypes.func, | ||
horizontal: PropTypes.bool | ||
}; | ||
|
||
VictoryBar.defaultProps = { | ||
containerComponent: <VictoryContainer />, | ||
data: defaultData, | ||
dataComponent: <Bar />, | ||
groupComponent: <g role="presentation" />, | ||
labelComponent: <VictoryLabel />, | ||
samples: 50, | ||
sortOrder: "ascending", | ||
standalone: true, | ||
theme: VictoryTheme.grayscale | ||
}; | ||
|
||
VictoryBar.getDomain = Domain.getDomainWithZero; | ||
VictoryBar.getData = Data.getData; | ||
VictoryBar.getBaseProps = (props) => getBaseProps(props, fallbackProps); | ||
VictoryBar.expectedComponents = [ | ||
"dataComponent", | ||
"labelComponent", | ||
"groupComponent", | ||
"containerComponent" | ||
]; | ||
|
||
if (this.shouldAnimate()) { | ||
return this.animateComponent(props, animationWhitelist); | ||
} | ||
// This component is at the top level so VictoryBar has access to the animation props | ||
const Wrapper = (props) => { | ||
// TODO: Figure out how to override this in victory-native | ||
const shouldAnimate = React.useMemo(() => { | ||
return !!props.animate; | ||
}, [props]); | ||
|
||
const children = this.renderData(props); | ||
return props.standalone | ||
? this.renderContainer(props.containerComponent, children) | ||
: children; | ||
const animationWhitelist = React.useMemo(() => { | ||
return props.animate && props.animate.animationWhitelist | ||
? props.animate.animationWhitelist | ||
: VictoryBar.animationWhitelist; | ||
}, [props]); | ||
|
||
if (shouldAnimate) { | ||
return ( | ||
<VictoryTransition | ||
animate={props.animate} | ||
animationWhitelist={animationWhitelist} | ||
> | ||
<VictoryBar {...props} /> | ||
</VictoryTransition> | ||
); | ||
} | ||
} | ||
|
||
export default addEvents(VictoryBar); | ||
return <VictoryBar {...props} />; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure whether this is the right solution, but we still needed some sort of wrapper component in order to access the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it stays, it probably needs a better name. |
||
|
||
Wrapper.propTypes = VictoryBar.propTypes; | ||
Wrapper.role = VictoryBar.role; | ||
Wrapper.getBaseProps = VictoryBar.getBaseProps; | ||
|
||
export default Wrapper; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still getting warnings due to this plugin - I will either need to fix webpack to resolve these warnings or take out this plugin.