From 6a49fff47161d8e8d31c2face604251b809d9c6f Mon Sep 17 00:00:00 2001 From: Marcin Lewandowski Date: Wed, 24 Jan 2024 20:06:10 +0100 Subject: [PATCH] fix(icon-helpers): fix TypeScript type issues (#15542) --- packages/icon-helpers/src/getAttributes.ts | 16 ++++------------ packages/icon-helpers/src/{toSVG.js => toSVG.ts} | 5 +++-- .../src/{toString.js => toString.ts} | 7 ++++--- packages/icon-helpers/src/types.ts | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 17 deletions(-) rename packages/icon-helpers/src/{toSVG.js => toSVG.ts} (81%) rename packages/icon-helpers/src/{toString.js => toString.ts} (77%) create mode 100644 packages/icon-helpers/src/types.ts diff --git a/packages/icon-helpers/src/getAttributes.ts b/packages/icon-helpers/src/getAttributes.ts index c35c2fb4fc10..a0a85ea97a2c 100644 --- a/packages/icon-helpers/src/getAttributes.ts +++ b/packages/icon-helpers/src/getAttributes.ts @@ -1,19 +1,11 @@ /** - * Copyright IBM Corp. 2018, 2023 + * Copyright IBM Corp. 2018, 2024 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ -import React from 'react'; -interface IconAttributes - extends Omit, 'tabIndex'> { - tabindex?: string | number | undefined; - - title?: string | undefined; -} - -export const defaultAttributes: IconAttributes = { +export const defaultAttributes = { // Reference: // https://github.com/IBM/carbon-components-react/issues/1392 // https://github.com/PolymerElements/iron-iconset-svg/pull/47 @@ -31,9 +23,9 @@ export default function getAttributes({ height, viewBox = `0 0 ${width} ${height}`, ...attributes -}: IconAttributes = {}): IconAttributes { +}: Record = {}): Record { const { tabindex, ...rest } = attributes; - const iconAttributes: IconAttributes = { + const iconAttributes: Record = { ...defaultAttributes, ...rest, width, diff --git a/packages/icon-helpers/src/toSVG.js b/packages/icon-helpers/src/toSVG.ts similarity index 81% rename from packages/icon-helpers/src/toSVG.js rename to packages/icon-helpers/src/toSVG.ts index 12ec12230c4a..82d283730d3e 100644 --- a/packages/icon-helpers/src/toSVG.js +++ b/packages/icon-helpers/src/toSVG.ts @@ -1,16 +1,17 @@ /** - * Copyright IBM Corp. 2018, 2023 + * Copyright IBM Corp. 2018, 2024 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import getAttributes from './getAttributes'; +import IconDescriptor from './types'; /** * Convert an icon descriptor to a DOM node. */ -export default function toSVG(descriptor) { +export default function toSVG(descriptor: IconDescriptor): SVGElement { const { elem = 'svg', attrs = {}, content = [] } = descriptor; const node = document.createElementNS('http://www.w3.org/2000/svg', elem); const attributes = elem !== 'svg' ? attrs : getAttributes(attrs); diff --git a/packages/icon-helpers/src/toString.js b/packages/icon-helpers/src/toString.ts similarity index 77% rename from packages/icon-helpers/src/toString.js rename to packages/icon-helpers/src/toString.ts index 444878b522b9..d63ee086cb33 100644 --- a/packages/icon-helpers/src/toString.js +++ b/packages/icon-helpers/src/toString.ts @@ -1,16 +1,17 @@ /** - * Copyright IBM Corp. 2018, 2023 + * Copyright IBM Corp. 2018, 2024 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import getAttributes from './getAttributes'; +import IconDescriptor from './types'; /** * Convert an icon descriptor to a String */ -export default function toString(descriptor) { +export default function toString(descriptor: IconDescriptor): string { const { elem = 'svg', attrs = {}, content = [] } = descriptor; const children = content.map(toString).join(''); if (elem !== 'svg') { @@ -21,7 +22,7 @@ export default function toString(descriptor) { )}>${children}`; } -export function formatAttributes(attrs) { +export function formatAttributes(attrs: Record): string { return Object.keys(attrs).reduce((acc, key, index) => { const attribute = `${key}="${attrs[key]}"`; if (index === 0) { diff --git a/packages/icon-helpers/src/types.ts b/packages/icon-helpers/src/types.ts new file mode 100644 index 000000000000..9006c3dcd068 --- /dev/null +++ b/packages/icon-helpers/src/types.ts @@ -0,0 +1,14 @@ +/** + * Copyright IBM Corp. 2024 + * + * This source code is licensed under the Apache-2.0 license found in the + * LICENSE file in the root directory of this source tree. + */ + +export default interface IconDescriptor { + elem?: string; + + attrs?: Record; + + content?: Array; +}