From c611dd4056bf805432e36de03ffbe0394302d17a Mon Sep 17 00:00:00 2001 From: Temidayo32 Date: Tue, 24 Oct 2023 17:12:13 +0100 Subject: [PATCH] Ensure the Icon react component can support custom icon paths #11122 - When provided with children (e.g. custom paths), render these instead of the `use` symbol reference - Allow any valid SVG attribute to be passed to the component to render on the `svg` element - Clean up rendering of className to avoid extra whitespace - Clean up ordering of the props to be alphabetically sorted - Update unit tests to be focused more on testing and less on snapshots --- client/src/components/Icon/Icon.test.js | 52 ++++++++++++++++--- client/src/components/Icon/Icon.tsx | 20 +++++-- .../Icon/__snapshots__/Icon.test.js.snap | 41 +++------------ .../__snapshots__/FieldBlock.test.js.snap | 2 +- .../__snapshots__/StreamBlock.test.js.snap | 4 +- 5 files changed, 69 insertions(+), 50 deletions(-) diff --git a/client/src/components/Icon/Icon.test.js b/client/src/components/Icon/Icon.test.js index a4d4a6ee9e..f3820ac91c 100644 --- a/client/src/components/Icon/Icon.test.js +++ b/client/src/components/Icon/Icon.test.js @@ -1,5 +1,5 @@ import React from 'react'; -import { shallow } from 'enzyme'; +import { shallow, mount } from 'enzyme'; import Icon from './Icon'; describe('Icon', () => { @@ -7,15 +7,53 @@ describe('Icon', () => { expect(Icon).toBeDefined(); }); - it('#name', () => { - expect(shallow()).toMatchSnapshot(); + it('should support icons with a name', () => { + const wrapper = mount(); + + expect(wrapper.find('.icon.icon-test')).toHaveLength(1); + expect(wrapper.find('use[href="#icon-test"]')).toHaveLength(1); + + expect(wrapper).toMatchSnapshot(); }); - it('#className', () => { - expect(shallow()).toMatchSnapshot(); + it('should support children in place of the icon use#name', () => { + const wrapper = shallow( + + + , + ); + + expect(wrapper.find('use')).toHaveLength(0); + expect(wrapper.find('rect')).toHaveLength(1); }); - it('#title', () => { - expect(shallow()).toMatchSnapshot(); + it('should support a className prop', () => { + const wrapper = mount(); + + expect(wrapper.find('.icon.u-test')).toHaveLength(1); + }); + + it('should support other svg attributes', () => { + const wrapper = mount(); + + expect(wrapper.find('svg').prop('viewBox')).toBe('0 0 1024 1024'); + }); + + it('should support a title that is output as a sibling of the title', () => { + const wrapper = mount(); + + const title = wrapper.find('svg.icon ~ span'); + expect(title).toHaveLength(1); + + expect(title.text()).toBe('Test title'); + expect(title.hasClass('w-sr-only')).toBe(true); }); }); diff --git a/client/src/components/Icon/Icon.tsx b/client/src/components/Icon/Icon.tsx index bf9e87f8dd..9ff672a007 100644 --- a/client/src/components/Icon/Icon.tsx +++ b/client/src/components/Icon/Icon.tsx @@ -1,8 +1,10 @@ import * as React from 'react'; -export interface IconProps { - name: string; +export interface IconProps extends React.SVGProps { + /** Optional svg `path` instead of the `use` based on the icon name. */ + children?: React.ReactNode; className?: string; + name: string; title?: string; } @@ -10,13 +12,21 @@ export interface IconProps { * Provide a `title` as an accessible label intended for screen readers. */ const Icon: React.FunctionComponent = ({ - name, + children, className, + name, title, + ...props }) => ( <> - {title}} diff --git a/client/src/components/Icon/__snapshots__/Icon.test.js.snap b/client/src/components/Icon/__snapshots__/Icon.test.js.snap index 9796195804..8cd3a07ac4 100644 --- a/client/src/components/Icon/__snapshots__/Icon.test.js.snap +++ b/client/src/components/Icon/__snapshots__/Icon.test.js.snap @@ -1,45 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Icon #className 1`] = ` - +exports[`Icon should support icons with a name 1`] = ` + - -`; - -exports[`Icon #name 1`] = ` - - - -`; - -exports[`Icon #title 1`] = ` - - - - Test title - - + `; diff --git a/client/src/components/StreamField/blocks/__snapshots__/FieldBlock.test.js.snap b/client/src/components/StreamField/blocks/__snapshots__/FieldBlock.test.js.snap index 4d1377d0b0..d324f94507 100644 --- a/client/src/components/StreamField/blocks/__snapshots__/FieldBlock.test.js.snap +++ b/client/src/components/StreamField/blocks/__snapshots__/FieldBlock.test.js.snap @@ -51,7 +51,7 @@ exports[`telepath: wagtail.blocks.FieldBlock with comments enabled it renders co

drink more water

The widget

-
+ " `; diff --git a/client/src/components/StreamField/blocks/__snapshots__/StreamBlock.test.js.snap b/client/src/components/StreamField/blocks/__snapshots__/StreamBlock.test.js.snap index 15f1029f00..67e9c29336 100644 --- a/client/src/components/StreamField/blocks/__snapshots__/StreamBlock.test.js.snap +++ b/client/src/components/StreamField/blocks/__snapshots__/StreamBlock.test.js.snap @@ -711,7 +711,7 @@ exports[`telepath: wagtail.blocks.StreamBlock it renders menus on opening 1`] = -
+
@@ -922,7 +922,7 @@ exports[`telepath: wagtail.blocks.StreamBlock setError renders error messages 1`
" `; -exports[`telepath: wagtail.blocks.StreamBlock with labels that need escaping it renders correctly 1`] = `"
Test Block <A>
Test Block <B>
"`; +exports[`telepath: wagtail.blocks.StreamBlock with labels that need escaping it renders correctly 1`] = `"
Test Block <A>
Test Block <B>
"`; exports[`telepath: wagtail.blocks.StreamBlock with unique block type it can add block 1`] = ` "