From ce9af8aac31ab3ca015230bf0c12de601e8c3768 Mon Sep 17 00:00:00 2001 From: Nidhi Kumari Date: Thu, 10 Nov 2022 06:01:51 +0530 Subject: [PATCH] Added Picker Network Component (#16340) * added file structure for picker-network * updated accessory prop with props * added tests and documentation to picker network * updated picker network classnames * updated custom tests for picker network * updated css in picker network * updated readme and stories * added snapshot testing * changed behaviour to button * updated snapshot for button * updated label as per src --- .../component-library-components.scss | 1 + .../picker-network/README.mdx | 47 +++++++++++ .../__snapshots__/picker-network.test.js.snap | 25 ++++++ .../component-library/picker-network/index.js | 1 + .../picker-network/picker-network.js | 84 +++++++++++++++++++ .../picker-network/picker-network.scss | 6 ++ .../picker-network/picker-network.stories.js | 56 +++++++++++++ .../picker-network/picker-network.test.js | 57 +++++++++++++ 8 files changed, 277 insertions(+) create mode 100644 ui/components/component-library/picker-network/README.mdx create mode 100644 ui/components/component-library/picker-network/__snapshots__/picker-network.test.js.snap create mode 100644 ui/components/component-library/picker-network/index.js create mode 100644 ui/components/component-library/picker-network/picker-network.js create mode 100644 ui/components/component-library/picker-network/picker-network.scss create mode 100644 ui/components/component-library/picker-network/picker-network.stories.js create mode 100644 ui/components/component-library/picker-network/picker-network.test.js diff --git a/ui/components/component-library/component-library-components.scss b/ui/components/component-library/component-library-components.scss index 8b8ad55e6..33a83b7c3 100644 --- a/ui/components/component-library/component-library-components.scss +++ b/ui/components/component-library/component-library-components.scss @@ -12,6 +12,7 @@ @import 'button-secondary/button-secondary'; @import 'icon/icon'; @import 'label/label'; +@import 'picker-network/picker-network'; @import 'tag/tag'; @import 'text/text'; @import 'text-field/text-field'; diff --git a/ui/components/component-library/picker-network/README.mdx b/ui/components/component-library/picker-network/README.mdx new file mode 100644 index 000000000..19e90d02f --- /dev/null +++ b/ui/components/component-library/picker-network/README.mdx @@ -0,0 +1,47 @@ +import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'; + +import { PickerNetwork } from './picker-network'; + +# PickerNetwork + +The `PickerNetwork` is used for the action of changing a network. + + + + + +## Props + +The `PickerNetwork` accepts all props below as well as all [Box](/docs/ui-components-ui-box-box-stories-js--default-story#props) component props. + + + +### Src + +Use the `src` prop with an image url to render the `AvatarNetwork`. + + + + + +```jsx +import { PickerNetwork } from '../../ui/component-library/picker-network'; + + + +``` + +### Label + +Use the `label` prop for the text content of the `PickerNetwork` component + + + + + +```jsx +import { PickerNetwork } from '../../ui/component-library/picker-network'; + + + +``` \ No newline at end of file diff --git a/ui/components/component-library/picker-network/__snapshots__/picker-network.test.js.snap b/ui/components/component-library/picker-network/__snapshots__/picker-network.test.js.snap new file mode 100644 index 000000000..5991eb25e --- /dev/null +++ b/ui/components/component-library/picker-network/__snapshots__/picker-network.test.js.snap @@ -0,0 +1,25 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`PickerNetwork should render the label inside the PickerNetwork 1`] = ` +
+ +
+`; diff --git a/ui/components/component-library/picker-network/index.js b/ui/components/component-library/picker-network/index.js new file mode 100644 index 000000000..469bf6962 --- /dev/null +++ b/ui/components/component-library/picker-network/index.js @@ -0,0 +1 @@ +export { PickerNetwork } from './picker-network'; diff --git a/ui/components/component-library/picker-network/picker-network.js b/ui/components/component-library/picker-network/picker-network.js new file mode 100644 index 000000000..1719630c7 --- /dev/null +++ b/ui/components/component-library/picker-network/picker-network.js @@ -0,0 +1,84 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import classnames from 'classnames'; +import { AvatarNetwork } from '../avatar-network'; +import { Icon, ICON_NAMES } from '../icon'; +import { Text } from '../text'; +import Box from '../../ui/box'; +import { + ALIGN_ITEMS, + COLORS, + DISPLAY, + SIZES, + BORDER_RADIUS, + TEXT, +} from '../../../helpers/constants/design-system'; + +export const PickerNetwork = ({ + className, + avatarNetworkProps, + iconProps, + label, + src, + ...props +}) => { + return ( + + + {label} + + + ); +}; + +PickerNetwork.propTypes = { + /** + * The src accepts the string of the image to be rendered + */ + src: PropTypes.string, + /** + * An additional className to apply to the PickerNetwork. + */ + className: PropTypes.string, + /** + * It accepts all the props from AvatarNetwork + */ + avatarNetworkProps: PropTypes.shape(AvatarNetwork.PropTypes), + /** + * It accepts all the props from Icon + */ + iconProps: PropTypes.shape(Icon.PropTypes), + /** + * The text content of the PickerNetwork component + */ + label: PropTypes.string.isRequired, + /** + * PickerNetwork accepts all the props from Box + */ + ...Box.propTypes, +}; diff --git a/ui/components/component-library/picker-network/picker-network.scss b/ui/components/component-library/picker-network/picker-network.scss new file mode 100644 index 000000000..f96edf782 --- /dev/null +++ b/ui/components/component-library/picker-network/picker-network.scss @@ -0,0 +1,6 @@ +.mm-picker-network { + --picker-network-height: 32px; + + max-width: fit-content; + height: var(--picker-network-height); +} diff --git a/ui/components/component-library/picker-network/picker-network.stories.js b/ui/components/component-library/picker-network/picker-network.stories.js new file mode 100644 index 000000000..eb5b25c7e --- /dev/null +++ b/ui/components/component-library/picker-network/picker-network.stories.js @@ -0,0 +1,56 @@ +import React from 'react'; +import { + DISPLAY, + FLEX_DIRECTION, +} from '../../../helpers/constants/design-system'; + +import Box from '../../ui/box'; +import README from './README.mdx'; +import { PickerNetwork } from './picker-network'; + +export default { + title: 'Components/ComponentLibrary/PickerNetwork', + id: __filename, + component: PickerNetwork, + parameters: { + docs: { + page: README, + }, + }, + argTypes: { + label: { + control: 'text', + }, + src: { + control: 'text', + }, + }, + args: { + label: 'Avalanche C-Chain', + src: './images/avax-token.png', + }, +}; + +export const DefaultStory = (args) => ; + +export const Label = (args) => ( + + + + + +); + +export const Src = (args) => ( + + + + + +); + +DefaultStory.storyName = 'Default'; diff --git a/ui/components/component-library/picker-network/picker-network.test.js b/ui/components/component-library/picker-network/picker-network.test.js new file mode 100644 index 000000000..878b5b968 --- /dev/null +++ b/ui/components/component-library/picker-network/picker-network.test.js @@ -0,0 +1,57 @@ +import { render, screen } from '@testing-library/react'; +import React from 'react'; + +import { PickerNetwork } from './picker-network'; + +describe('PickerNetwork', () => { + it('should render the label inside the PickerNetwork', () => { + const { getByTestId, container } = render( + , + ); + expect(getByTestId('picker-network')).toBeDefined(); + expect(getByTestId('picker-network')).toHaveTextContent('Imported'); + expect(container).toMatchSnapshot(); + }); + it('should render correct Avatar inside Picker Network', () => { + render( + , + ); + const image = screen.getByRole('img'); + expect(image).toBeDefined(); + expect(image).toHaveAttribute('src', './images/matic-token.png'); + }); + it('should render avatar network inside the PickerNetwork with custom props', () => { + const container = ( + + ); + + expect(container.props.avatarNetworkProps.name).toStrictEqual( + 'matic network', + ); + }); + it('should render down arrow icon inside the PickerNetwork with custom props', () => { + const container = ( + + ); + + expect(container.props.iconProps.name).toStrictEqual( + 'down-arrow-picker-icon', + ); + }); +});