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

refactor: Convert EasyPill component to typescript #250

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/EasyPill/EasyPill.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dropdownDeleteIcon {
align-self: center;
margin-right: var(--rvr-space-md);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { mount, shallow } from 'enzyme';

import Pill from '../Pill';
import EasyPill from './';
import EasyPill from '.';

describe('EasyPill', () => {
it('renders', () => {
Expand Down Expand Up @@ -35,7 +35,6 @@ describe('EasyPill', () => {
onClick: () => {},
},
]}
onClick={() => {}}
>
EasyPill 1
</EasyPill>
Expand All @@ -62,7 +61,6 @@ describe('EasyPill', () => {
},
]}
checked
onClick={() => {}}
>
EasyPill 1
</EasyPill>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
import React from 'react';
import PropTypes from 'prop-types';

import Media from '../Media';
import EasyDropdown from '../EasyDropdown';
import Icon from '../Icon';
import Pill from '../Pill';
import { PillProps } from '../Pill/Pill';
import styles from './EasyPill.module.css';

const EasyPillDropdown = ({ actions, onDelete }) => {
if (!actions.length) {
type EasyPillActions = {
children?: React.ReactNode;
label: string;
onClick: Function;
group?: string;
};

interface EasyPillProps extends PillProps {
actions?: EasyPillActions[];
children: React.ReactNode;
onDelete?: (...args) => void;
}

interface EasyPillDropdownProps {
actions: EasyPillActions[];
pixelbandito marked this conversation as resolved.
Show resolved Hide resolved
onDelete?: (...args) => void;
}

const EasyPillDropdown: React.FC<EasyPillDropdownProps> = ({
actions,
onDelete = () => {},
}) => {
if (!actions?.length) {
return null;
}

Expand All @@ -19,7 +41,7 @@ const EasyPillDropdown = ({ actions, onDelete }) => {
label: 'Delete',
children: (
<Media>
<Media.Item mr="md" alignSelf="center">
<Media.Item className={styles.dropdownDeleteIcon}>
<Icon
height={16}
name="trash"
Expand All @@ -40,6 +62,11 @@ const EasyPillDropdown = ({ actions, onDelete }) => {
menuProps={{ position: 'bottomLeft' }}
menuItems={menuItems}
defaultIsOpen={false}
disabled={false}
isOpen={false}
onToggle={() => {}}
toggleProps={{}}
className=""
>
{/* The onClick action is handled by EasyDropdown */}
<div role="button" tabIndex={0}>
Expand All @@ -49,19 +76,24 @@ const EasyPillDropdown = ({ actions, onDelete }) => {
);
};

export const EasyPill = ({ actions, children, onDelete, ...passedProps }) => {
export const EasyPill: React.FC<EasyPillProps> = ({
actions,
children,
onDelete,
...passedProps
}) => {
return (
<Pill {...passedProps}>
{children}
{passedProps.checked &&
(actions.length ? (
<Pill.Addon onClick={e => e.stopPropagation()}>
(actions?.length ? (
<Pill.Addon onClick={(e) => e.stopPropagation()}>
<EasyPillDropdown actions={actions} onDelete={onDelete} />
</Pill.Addon>
) : (
onDelete && (
<Pill.Addon
onClick={e => {
onClick={(e) => {
e.stopPropagation();
onDelete(e);
}}
Expand All @@ -74,32 +106,4 @@ export const EasyPill = ({ actions, children, onDelete, ...passedProps }) => {
);
};

EasyPill.propTypes = {
actions: PropTypes.arrayOf(
PropTypes.shape({
children: PropTypes.node,
label: PropTypes.string.isRequired,
onClick: PropTypes.func.isRequired,
})
),
checked: PropTypes.bool,
children: PropTypes.node.isRequired,
onDelete: PropTypes.func,
};

EasyPill.defaultProps = {
actions: [],
checked: false,
onDelete: null,
};

EasyPillDropdown.propTypes = {
actions: EasyPill.propTypes.actions.isRequired,
onDelete: EasyPill.propTypes.onDelete,
};

EasyPillDropdown.defaultProps = {
onDelete: EasyPill.defaultProps.onDelete,
};

export default EasyPill;
1 change: 1 addition & 0 deletions src/components/EasyPill/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './EasyPill';