UNPKG

html-react-parser

Version:
74 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = attributesToProps; var react_property_1 = require("react-property"); var utilities_1 = require("./utilities"); // https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components // https://developer.mozilla.org/docs/Web/HTML/Attributes var UNCONTROLLED_COMPONENT_ATTRIBUTES = ['checked', 'value']; var UNCONTROLLED_COMPONENT_NAMES = ['input', 'select', 'textarea']; var valueOnlyInputs = { reset: true, submit: true, }; /** * Converts HTML/SVG DOM attributes to React props. * * @param attributes - HTML/SVG DOM attributes. * @param nodeName - DOM node name. * @returns - React props. */ function attributesToProps(attributes, nodeName) { if (attributes === void 0) { attributes = {}; } var props = {}; var isInputValueOnly = Boolean(attributes.type && valueOnlyInputs[attributes.type]); for (var attributeName in attributes) { var attributeValue = attributes[attributeName]; // ARIA (aria-*) or custom data (data-*) attribute if ((0, react_property_1.isCustomAttribute)(attributeName)) { props[attributeName] = attributeValue; continue; } // convert HTML/SVG attribute to React prop var attributeNameLowerCased = attributeName.toLowerCase(); var propName = getPropName(attributeNameLowerCased); if (propName) { var propertyInfo = (0, react_property_1.getPropertyInfo)(propName); // convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`) if (UNCONTROLLED_COMPONENT_ATTRIBUTES.includes(propName) && UNCONTROLLED_COMPONENT_NAMES.includes(nodeName) && !isInputValueOnly) { propName = getPropName('default' + attributeNameLowerCased); } props[propName] = attributeValue; switch (propertyInfo && propertyInfo.type) { case react_property_1.BOOLEAN: props[propName] = true; break; case react_property_1.OVERLOADED_BOOLEAN: if (attributeValue === '') { props[propName] = true; } break; } continue; } // preserve custom attribute if React >=16 if (utilities_1.PRESERVE_CUSTOM_ATTRIBUTES) { props[attributeName] = attributeValue; } } // transform inline style to object (0, utilities_1.setStyleProp)(attributes.style, props); return props; } /** * Gets prop name from lowercased attribute name. * * @param attributeName - Lowercased attribute name. * @returns - Prop name. */ function getPropName(attributeName) { return react_property_1.possibleStandardNames[attributeName]; } //# sourceMappingURL=attributes-to-props.js.map