| [a762898] | 1 | import { isValidElement } from 'react';
|
|---|
| 2 | import { isEventKey } from './excludeEventProps';
|
|---|
| 3 | import { isDataAttribute, isSvgElementPropKey } from './svgPropertiesNoEvents';
|
|---|
| 4 | /**
|
|---|
| 5 | * Filters an object to only include SVG properties, data attributes, and event handlers.
|
|---|
| 6 | * @param obj - The object to filter.
|
|---|
| 7 | * @returns A new object containing only valid SVG properties, data attributes, and event handlers.
|
|---|
| 8 | */
|
|---|
| 9 | export function svgPropertiesAndEvents(obj) {
|
|---|
| 10 | var result = {};
|
|---|
| 11 | // for ... in loop is 10x faster than Object.entries + filter + Object.fromEntries in Chrome
|
|---|
| 12 |
|
|---|
| 13 | for (var key in obj) {
|
|---|
| 14 | if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|---|
| 15 | if (isSvgElementPropKey(key) || isDataAttribute(key) || isEventKey(key)) {
|
|---|
| 16 | result[key] = obj[key];
|
|---|
| 17 | }
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 | return result;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Function to filter SVG properties from various input types.
|
|---|
| 25 | * The input types can be:
|
|---|
| 26 | * - A record of string keys to any values, in which case it returns a record of only SVG properties
|
|---|
| 27 | * - A React element, in which case it returns the props of the element filtered to only SVG properties
|
|---|
| 28 | * - Anything else, in which case it returns null
|
|---|
| 29 | *
|
|---|
| 30 | * This function has a wide-open return type, because it will read and filter the props of an arbitrary React element.
|
|---|
| 31 | * This can be SVG, HTML, whatnot, with arbitrary values, so we can't type it more specifically.
|
|---|
| 32 | *
|
|---|
| 33 | * If you wish to have a type-safe version, use svgPropertiesNoEvents directly with a typed object.
|
|---|
| 34 | *
|
|---|
| 35 | * @param input - The input to filter, which can be a record, a React element, or other types.
|
|---|
| 36 | * @returns A record of SVG properties if the input is a record or React element, otherwise null.
|
|---|
| 37 | */
|
|---|
| 38 | export function svgPropertiesAndEventsFromUnknown(input) {
|
|---|
| 39 | if (input == null) {
|
|---|
| 40 | return null;
|
|---|
| 41 | }
|
|---|
| 42 | if (/*#__PURE__*/isValidElement(input)) {
|
|---|
| 43 | // @ts-expect-error we can't type this better because input can be any React element
|
|---|
| 44 | return svgPropertiesAndEvents(input.props);
|
|---|
| 45 | }
|
|---|
| 46 | if (typeof input === 'object' && !Array.isArray(input)) {
|
|---|
| 47 | return svgPropertiesAndEvents(input);
|
|---|
| 48 | }
|
|---|
| 49 | return null;
|
|---|
| 50 | } |
|---|