[@wordpress/element] add new definitions (#35877)

This commit is contained in:
Derek Sifford 2019-06-05 19:42:55 -04:00 committed by Andrew Casey
parent 94061c1de4
commit ab96f30acf
5 changed files with 108 additions and 0 deletions

59
types/wordpress__element/index.d.ts vendored Normal file
View File

@ -0,0 +1,59 @@
// Type definitions for @wordpress/element 2.4
// Project: https://github.com/WordPress/gutenberg/tree/master/packages/element/README.md
// Definitions by: Derek Sifford <https://github.com/dsifford>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.4
import * as R from "react";
import * as RD from "react-dom";
declare global {
namespace React {
//
// ReactDOM re-exports
//
const createPortal: typeof RD.createPortal;
const findDOMNode: typeof RD.findDOMNode;
const render: typeof RD.render;
const unmountComponentAtNode: typeof RD.unmountComponentAtNode;
/**
* Component used as equivalent of Fragment with unescaped HTML, in cases where
* it is desirable to render dangerous HTML without needing a wrapper element.
* To preserve additional props, a `div` wrapper _will_ be created if any props
* aside from `children` are passed.
*
* @param props.children - HTML to render.
*
* @return Dangerously-rendering element.
*/
function RawHTML(
props: { children: string } & HTMLProps<HTMLDivElement>
): JSX.Element;
/**
* Checks if the provided WP element is empty.
*
* @param element - WP element to check.
* @return True when an element is considered empty.
*/
function isEmptyElement(element: ReactNode): boolean;
/**
* Serializes a React element to string.
*
* @param element - Element to serialize.
* @param context - Context object.
* @param legacyContext - Legacy context object.
*
* @return Serialized element.
*/
function renderToString(
element: ReactNode,
context?: any,
legacyContext?: any
): string;
}
}
export = React;

View File

@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"csstype": "^2.2.0"
}
}

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "dom"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"paths": {
"@wordpress/element": ["wordpress__element"]
}
},
"files": ["index.d.ts", "wordpress__element-tests.ts"]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,23 @@
import * as WPElement from "@wordpress/element";
const testElement = WPElement.createElement(
"div",
{ className: "foobar" },
"hello world"
);
WPElement.createRef();
WPElement.createPortal(testElement, document.createElement("div"));
WPElement.findDOMNode(null);
WPElement.render(testElement, document.createElement("div"));
WPElement.unmountComponentAtNode(document.createElement("div"));
WPElement.RawHTML({ children: "<h1>Hello world</h1>" });
WPElement.isEmptyElement("hello world");
WPElement.renderToString(WPElement.createElement("div", {}, "hello world"));