Skip to content

Commit

Permalink
Update tag chip to make it a bit cleaner (#2068)
Browse files Browse the repository at this point in the history
- changes the property name to tagName from tag to make it clearer
- make onClick type accurate
- make onSelect an optional type
  • Loading branch information
belcherj authored May 12, 2020
1 parent 6a0fc6a commit ec5bd6e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
14 changes: 9 additions & 5 deletions lib/components/tag-chip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import React, { FunctionComponent } from 'react';
import classNames from 'classnames';

type OwnProps = {
onSelect: () => any;
onSelect?: (event: React.MouseEvent<HTMLDivElement>) => any;
selected: boolean;
tag: string;
tagName: string;
};

const TagChip: FunctionComponent<OwnProps> = ({ onSelect, selected, tag }) => (
const TagChip: FunctionComponent<OwnProps> = ({
onSelect,
selected,
tagName
}) => (
<div
className={classNames('tag-chip', { selected })}
data-tag-name={tag}
data-tag-name={tagName}
onClick={onSelect}
>
{tag}
{tagName}
</div>
);

Expand Down
10 changes: 5 additions & 5 deletions lib/components/tag-chip/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ describe('TagChip', () => {
it('should select tag when clicked', () => {
const selectIt = jest.fn();

const chip = mount(<TagChip tag="spline" onSelect={selectIt} />);
const chip = mount(<TagChip tagName="spline" onSelect={selectIt} />);

expect(selectIt).not.toHaveBeenCalled();
chip.simulate('click');
expect(selectIt).toHaveBeenCalled();
});

it('should not include the `selected` class by default', () => {
const chip = shallow(<TagChip tag="spline" />);
const chip = shallow(<TagChip tagName="spline" />);

expect(chip.hasClass('selected')).toBe(false);
});

it('should include the `selected` class when selected', () => {
const chip = shallow(<TagChip tag="spline" selected />);
const chip = shallow(<TagChip tagName="spline" selected />);

expect(chip.hasClass('selected')).toBe(true);
});

it('should toggle the `selected` class with prop changes', () => {
const chip = shallow(<TagChip tag="spline" />);
const chip = shallow(<TagChip tagName="spline" />);

expect(chip.hasClass('selected')).toBe(false);

Expand All @@ -42,7 +42,7 @@ describe('TagChip', () => {
});

it('should not introduce visual regressions', () => {
const component = renderer.create(<TagChip tag="spline" />).toJSON();
const component = renderer.create(<TagChip tagName="spline" />).toJSON();

expect(component).toMatchSnapshot();
});
Expand Down
2 changes: 1 addition & 1 deletion lib/tag-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class TagField extends Component<Props, OwnState> {
{tags.filter(negate(isEmailTag)).map((tag) => (
<TagChip
key={tag}
tag={tag}
tagName={tag}
selected={tag === selectedTag}
onSelect={this.selectTag}
/>
Expand Down

0 comments on commit ec5bd6e

Please sign in to comment.