Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

check for content and return null rather than trying to render a label #365

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/victory-label/victory-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,16 @@ export default class VictoryLabel extends React.Component {

getContent(props) {
if (props.text === undefined || props.text === null) {
return [" "];
return undefined;
}
const datum = props.datum || props.data;
if (Array.isArray(props.text)) {
return props.text.map((line) => Helpers.evaluateProp(line, datum, props.active));
}
const child = Helpers.evaluateProp(props.text, datum, props.active);
if (child === undefined || child === null) {
return undefined;
}
return `${child}`.split("\n");
}

Expand Down Expand Up @@ -205,13 +208,12 @@ export default class VictoryLabel extends React.Component {
return defaultStyles.fontSize;
}

renderElements(props) {
renderElements(props, content) {
const { datum, active, inline, className, title, desc, events } = props;
const style = this.getStyles(props);
const lineHeight = this.getHeight(props, "lineHeight");
const textAnchor = props.textAnchor ?
Helpers.evaluateProp(props.textAnchor, datum, active) : "start";
const content = this.getContent(props);
const dx = props.dx ? Helpers.evaluateProp(props.dx, datum, active) : 0;
const dy = this.getDy(props, style, content, lineHeight);
const transform = this.getTransform(props, style);
Expand Down Expand Up @@ -245,7 +247,11 @@ export default class VictoryLabel extends React.Component {
}

render() {
const label = this.renderElements(this.props);
const content = this.getContent(this.props);
if (content === null || content === undefined) {
return null;
}
const label = this.renderElements(this.props, content);
return this.props.renderInPortal ? <VictoryPortal>{label}</VictoryPortal> : label;
}
}
3 changes: 1 addition & 2 deletions test/client/spec/victory-label/victory-label.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe("components/victory-label", () => {
it("attaches an to the parent object", () => {
const clickHandler = sinon.spy();
const wrapper = mount(
<VictoryLabel events={{ onClick: clickHandler }}/>
<VictoryLabel text="hi" events={{ onClick: clickHandler }}/>
);
wrapper.find(Text).simulate("click");
expect(clickHandler.called).to.equal(true);
Expand Down Expand Up @@ -148,7 +148,6 @@ describe("components/victory-label", () => {
const wrapper = shallow(
<VictoryLabel
text={["lineHeight", "array", "testing"]}
// eslint-disable-next-line no-magic-numbers
lineHeight={lineHeight}
/>
);
Expand Down