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

ensureElement wraps React Fragment #2499

Merged
merged 2 commits into from
May 14, 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
13 changes: 8 additions & 5 deletions packages/core/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ export function isFunction(value: any): value is Function {
}

/**
* Converts a React child to an element: non-empty strings or numbers are wrapped in `<span>`;
* empty strings are discarded.
* Converts a React child to an element: non-empty string or number or
* `React.Fragment` (React 16.3+) is wrapped in given tag name; empty strings
* are discarded.
*/
export function ensureElement(child: React.ReactChild | undefined, tagName: keyof JSX.IntrinsicElements = "span") {
// wrap text in a <span> so children are always elements
if (typeof child === "string") {
if (child == null) {
return undefined;
} else if (typeof child === "string") {
// cull whitespace strings
return child.trim().length > 0 ? React.createElement(tagName, {}, child) : undefined;
} else if (typeof child === "number") {
} else if (typeof child === "number" || typeof child.type === "symbol") {
// React.Fragment has a symbol type
return React.createElement(tagName, {}, child);
} else {
return child;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,39 @@ describe("Utils", () => {
// TODO: not sure how to test this. perhaps with the help of https://github.com/alexreardon/raf-stub?
it.skip("throttleEvent");

describe("ensureElement", () => {
it("handles undefined/null", () => {
assert.isUndefined(Utils.ensureElement(undefined));
assert.isUndefined(Utils.ensureElement(null));
});

it("wraps strings & numbers", () => {
assert.strictEqual(Utils.ensureElement("foo").type, "span");
assert.strictEqual(Utils.ensureElement(1234).type, "span");
});

it("returns undefined for whitespace strings", () => {
assert.isUndefined(Utils.ensureElement(" "));
});

it("passes through JSX elements", () => {
const el = <div>my element</div>;
assert.strictEqual(Utils.ensureElement(el), el);
});

// React 16 only
if (React.Fragment !== undefined) {
it("wraps JSX fragments in element", () => {
const el = Utils.ensureElement(
<>
one <em>two</em> three
</>,
);
assert.strictEqual(el.type, "span");
});
}
});

describe("throttleReactEventCallback", () => {
let callback: SinonSpy;
let fakeEvent: any; // cast as `any` to avoid having to set every required property on the event
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import "./callout/calloutTests";
import "./card/cardTests";
import "./collapse/collapseTests";
import "./collapsible-list/collapsibleListTests";
import "./common/propsTests.ts";
import "./common/utils/compareUtilsTests.ts";
import "./common/utilsTests.ts";
import "./common/propsTests";
import "./common/utils/compareUtilsTests";
import "./common/utilsTests";
import "./context-menu/contextMenuTests";
import "./controls/controlsTests";
import "./controls/inputGroupTests";
Expand Down