Skip to content

Commit 8ef6201

Browse files
committedOct 30, 2023
refactor(domToReact): refactoring multiple types
Release-As: 5.0.2
1 parent 810d13b commit 8ef6201

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed
 

‎src/attributes-to-props.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ import { PRESERVE_CUSTOM_ATTRIBUTES, setStyleProp } from './utilities';
99

1010
// https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components
1111
// https://developer.mozilla.org/docs/Web/HTML/Attributes
12-
const UNCONTROLLED_COMPONENT_ATTRIBUTES = ['checked', 'value'];
13-
const UNCONTROLLED_COMPONENT_NAMES = ['input', 'select', 'textarea'];
12+
const UNCONTROLLED_COMPONENT_ATTRIBUTES = ['checked', 'value'] as const;
13+
const UNCONTROLLED_COMPONENT_NAMES = ['input', 'select', 'textarea'] as const;
14+
15+
type UncontrolledComponentAttributes =
16+
(typeof UNCONTROLLED_COMPONENT_ATTRIBUTES)[number];
17+
18+
type UncontrolledComponentNames = (typeof UNCONTROLLED_COMPONENT_NAMES)[number];
1419

1520
const valueOnlyInputs = {
1621
reset: true,
@@ -63,8 +68,12 @@ export default function attributesToProps(
6368

6469
// convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`)
6570
if (
66-
UNCONTROLLED_COMPONENT_ATTRIBUTES.indexOf(propName) !== -1 &&
67-
UNCONTROLLED_COMPONENT_NAMES.indexOf(nodeName!) !== -1 &&
71+
UNCONTROLLED_COMPONENT_ATTRIBUTES.indexOf(
72+
propName as UncontrolledComponentAttributes,
73+
) !== -1 &&
74+
UNCONTROLLED_COMPONENT_NAMES.indexOf(
75+
nodeName! as UncontrolledComponentNames,
76+
) !== -1 &&
6877
!isInputValueOnly
6978
) {
7079
propName = getPropName('default' + attributeNameLowerCased);

‎src/dom-to-react.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ export default function domToReact(
3737
const { cloneElement, createElement, isValidElement } =
3838
options?.library || React;
3939

40-
let index = 0;
4140
const nodesLength = nodes.length;
4241

43-
for (; index < nodesLength; index++) {
42+
for (let index = 0; index < nodesLength; index++) {
4443
const node = nodes[index];
4544

4645
// replace with custom React element (if present)

‎src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const domParserOptions = { lowerCaseAttributeNames: false } as const;
2121
export default function HTMLReactParser(
2222
html: string,
2323
options?: HTMLReactParserOptions,
24-
): JSX.Element | JSX.Element[] | string {
24+
): ReturnType<typeof domToReact> {
2525
if (typeof html !== 'string') {
2626
throw new TypeError('First argument must be a string');
2727
}

0 commit comments

Comments
 (0)
Please sign in to comment.