Skip to content
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

fix(demo/text): render 0 as number (#813) #814

Merged
merged 4 commits into from
Oct 8, 2020
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
9 changes: 4 additions & 5 deletions packages/visx-text/src/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ class Text extends React.Component<TextProps, TextState> {
// Only perform calculations if using features that require them (multiline, scaleToFit)
if (props.width || props.scaleToFit) {
if (needCalculate) {
const words: string[] = props.children
? props.children.toString().split(/(?:(?!\u00A0+)\s+)/)
: [];

const words: string[] =
props.children == null ? [] : props.children.toString().split(/(?:(?!\u00A0+)\s+)/);
this.wordsWithWidth = words.map(word => ({
word,
width: getStringWidth(word, props.style) || 0,
Expand All @@ -138,7 +136,8 @@ class Text extends React.Component<TextProps, TextState> {
}

updateWordsWithoutCalculate(props: TextProps) {
const words = props.children ? props.children.toString().split(/(?:(?!\u00A0+)\s+)/) : [];
const words =
props.children == null ? [] : props.children.toString().split(/(?:(?!\u00A0+)\s+)/);
this.setState({ wordsByLines: [{ words }] });
}

Expand Down
12 changes: 10 additions & 2 deletions packages/visx-text/test/Text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ describe('getStringWidth()', () => {
});
});

// TODO: Fix tests (jsdom does not support getComputedTextLength() or getBoundingClientRect()). Maybe use puppeteer

describe('<Text />', () => {
beforeEach(addMock);
afterEach(removeMock);
Expand Down Expand Up @@ -102,6 +100,16 @@ describe('<Text />', () => {
expect(wrapperNan.text()).not.toContain('anything');
});

it('Render text when children 0 is a number', () => {
const wrapper = mount(
<Text x={0} y={0}>
{0}
</Text>,
);

expect(wrapper.text()).toContain('0');
});

it('Recalculates text when children are updated', () => {
const wrapper = shallow<Text>(
<Text width={200} style={{ fontFamily: 'Courier' }}>
Expand Down