From d3f691b65f0317d7e27d9d065d2ac3a7f3bdf5a0 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 4 Nov 2024 08:45:36 +0200 Subject: [PATCH] feat: add custom loader to react component --- components/react/src/iconify.ts | 18 ++++++++++++- .../react/tests/iconify/10-basic.test.tsx | 27 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/components/react/src/iconify.ts b/components/react/src/iconify.ts index c6a7055..abc16b4 100644 --- a/components/react/src/iconify.ts +++ b/components/react/src/iconify.ts @@ -27,6 +27,10 @@ import { calculateSize } from '@iconify/utils/lib/svg/size'; import type { IconifyIconBuildResult } from '@iconify/utils/lib/svg/build'; // API +import type { + IconifyCustomIconLoader, + IconifyCustomIconsLoader, +} from '@iconify/core/lib/api/types'; import type { IconifyAPIFunctions, IconifyAPIInternalFunctions, @@ -59,6 +63,10 @@ import type { IconifyIconLoaderAbort, } from '@iconify/core/lib/api/icons'; import { loadIcons, loadIcon } from '@iconify/core/lib/api/icons'; +import { + setCustomIconLoader, + setCustomIconsLoader, +} from '@iconify/core/lib/api/loaders'; import { sendAPIQuery } from '@iconify/core/lib/api/query'; // Cache @@ -120,6 +128,8 @@ export { PartialIconifyAPIConfig, IconifyAPIQueryParams, IconifyAPICustomQueryParams, + IconifyCustomIconLoader, + IconifyCustomIconsLoader, }; // Builder functions @@ -394,7 +404,13 @@ const _api: IconifyAPIInternalFunctions = { export { _api }; // IconifyAPIFunctions -export { addAPIProvider, loadIcons, loadIcon }; +export { + addAPIProvider, + loadIcons, + loadIcon, + setCustomIconLoader, + setCustomIconsLoader, +}; // IconifyStorageFunctions export { diff --git a/components/react/tests/iconify/10-basic.test.tsx b/components/react/tests/iconify/10-basic.test.tsx index 7de652c..be1a8c9 100644 --- a/components/react/tests/iconify/10-basic.test.tsx +++ b/components/react/tests/iconify/10-basic.test.tsx @@ -1,5 +1,10 @@ import React from 'react'; -import { Icon, InlineIcon } from '../../dist/iconify'; +import { + Icon, + InlineIcon, + setCustomIconLoader, + loadIcon, +} from '../../dist/iconify'; import { describe, test, expect } from 'vitest'; import { render } from '@testing-library/react'; @@ -32,4 +37,24 @@ describe('Creating component using object', () => { '' ); }); + + test('custom loader', async () => { + const prefix = 'customLoader'; + const name = 'TestIcon'; + + // Set custom loader and load icon data + setCustomIconLoader(() => { + return iconData; + }, prefix); + await loadIcon(`${prefix}:${name}`); + + // Create component + const renderResult = render( + + ); + + expect(renderResult.container.innerHTML).toEqual( + `` + ); + }); });