Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(icon-helpers): fix TypeScript type issues #15542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 4 additions & 12 deletions 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<React.SVGProps<React.ReactSVGElement>, '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
Expand All @@ -31,9 +23,9 @@ export default function getAttributes({
height,
viewBox = `0 0 ${width} ${height}`,
...attributes
}: IconAttributes = {}): IconAttributes {
}: Record<string, unknown> = {}): Record<string, unknown> {
const { tabindex, ...rest } = attributes;
const iconAttributes: IconAttributes = {
const iconAttributes: Record<string, unknown> = {
...defaultAttributes,
...rest,
width,
Expand Down
@@ -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);
Expand Down
@@ -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') {
Expand All @@ -21,7 +22,7 @@ export default function toString(descriptor) {
)}>${children}</${elem}>`;
}

export function formatAttributes(attrs) {
export function formatAttributes(attrs: Record<string, unknown>): string {
return Object.keys(attrs).reduce((acc, key, index) => {
const attribute = `${key}="${attrs[key]}"`;
if (index === 0) {
Expand Down
14 changes: 14 additions & 0 deletions 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<string, string>;

content?: Array<IconDescriptor>;
}