Skip to content

Commit 42e39ee

Browse files
author
Nick Frasser
committed
Correct types for Linkify React
1 parent 9d157b6 commit 42e39ee

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

packages/linkify-react/src/linkify-react.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as React from 'react';
22
import { tokenize, Options } from 'linkifyjs';
33

4-
// Given a string, converts to an array of valid React components
5-
// (which may include strings)
4+
/**
5+
* Given a string, converts to an array of valid React components
6+
* (which may include strings)
7+
* @param {string} str
8+
* @param {any} opts
9+
* @returns {React.ReactNodeArray}
10+
*/
611
function stringToElements(str, opts) {
712

813
const tokens = tokenize(str);
@@ -52,6 +57,14 @@ function stringToElements(str, opts) {
5257
}
5358

5459
// Recursively linkify the contents of the given React Element instance
60+
/**
61+
* @template P
62+
* @template {string | React.JSXElementConstructor<P>} T
63+
* @param {React.ReactElement<P, T>} element
64+
* @param {Object} opts
65+
* @param {number} elementId
66+
* @returns {React.ReactElement<P, T>}
67+
*/
5568
function linkifyReactElement(element, opts, elementId = 0) {
5669
if (React.Children.count(element.props.children) === 0) {
5770
// No need to clone if the element had no children
@@ -90,22 +103,24 @@ function linkifyReactElement(element, opts, elementId = 0) {
90103
}
91104

92105
/**
93-
* @param {Object} props
94-
* @param {Object} [props.options] Linkify options
95-
* @param {string | React.Element} [props.tagName] element in which to wrap all Linkified content (default React.Fragment)
106+
* @template P
107+
* @template {string | React.JSXElementConstructor<P>} T
108+
* @param {P & { tagName?: T, options?: any, children?: React.ReactNode}} props
109+
* @returns {React.ReactElement<P, T>}
96110
*/
97111
const Linkify = (props) => {
98112
// Copy over all non-linkify-specific props
99113
const newProps = { key: 'linkified-element-wrapper' };
100114
for (const prop in props) {
101-
if (prop !== 'options' && prop !== 'tagName') {
115+
if (prop !== 'options' && prop !== 'tagName' && prop !== 'children') {
102116
newProps[prop] = props[prop];
103117
}
104118
}
105119

106120
const opts = new Options(props.options);
107121
const tagName = props.tagName || React.Fragment || 'span';
108-
const element = React.createElement(tagName, newProps);
122+
const children = props.children;
123+
const element = React.createElement(tagName, newProps, children);
109124

110125
return linkifyReactElement(element, opts, 0);
111126
};

0 commit comments

Comments
 (0)