Skip to content

Commit

Permalink
feat: TET-901 remove onChange
Browse files Browse the repository at this point in the history
  • Loading branch information
golas-m committed May 13, 2024
1 parent bc8b473 commit 7c18472
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 41 deletions.
1 change: 0 additions & 1 deletion src/components/SelectablePill/SelectablePill.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export type SelectablePillProps = {
tabIndex?: number;
custom?: SelectablePillConfig;
beforeComponent?: BeforeComponentProps;
onChange?: (state: boolean) => void;
} & Omit<HTMLAttributes<HTMLSpanElement>, 'color'>;

type BeforeComponentProps =
Expand Down
26 changes: 1 addition & 25 deletions src/components/SelectablePill/SelectablePill.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { vi } from 'vitest';

import { SelectablePill } from './SelectablePill';
import { SelectablePillState } from './SelectablePillState.type';
import { render, screen, fireEvent } from '../../tests/render';
import { render, screen } from '../../tests/render';

describe('SelectablePill', () => {
const states: SelectablePillState[] = ['default', 'disabled'];
Expand Down Expand Up @@ -98,28 +96,6 @@ describe('SelectablePill', () => {

selected.forEach((isSelected) => {
describe(`isSelected ${isSelected}`, () => {
it('should handle onChange properly when clicked', () => {
const onChangeMock = vi.fn();
render(
<SelectablePill
text="Value"
state={state}
isSelected={isSelected}
onChange={onChangeMock}
/>,
);

const pill = screen.getByTestId(pillPointer);
expect(pill).toBeInTheDocument();
fireEvent.click(pill);
if (state !== 'disabled') {
expect(onChangeMock).toHaveBeenCalled();
expect(onChangeMock).toBeCalledWith(!isSelected);
} else {
expect(onChangeMock).not.toHaveBeenCalled();
}
});

it('should correctly render the checkmark', () => {
render(
<SelectablePill
Expand Down
10 changes: 1 addition & 9 deletions src/components/SelectablePill/SelectablePill.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Icon } from '@virtuslab/tetrisly-icons';
import { MouseEventHandler, useCallback, useMemo, type FC } from 'react';
import { useMemo, type FC } from 'react';

import { SelectablePillProps } from './SelectablePill.props';
import { stylesBuilder } from './stylesBuilder';
Expand All @@ -16,7 +16,6 @@ export const SelectablePill: FC<SelectablePillProps> = ({
text,
prefix,
custom,
onChange,
...rest
}) => {
const styles = useMemo(
Expand Down Expand Up @@ -54,17 +53,10 @@ export const SelectablePill: FC<SelectablePillProps> = ({
[beforeComponent],
);

const handleOnClick: MouseEventHandler<HTMLSpanElement> = useCallback(() => {
if (state !== 'disabled') {
onChange?.(!isSelected);
}
}, [onChange, state, isSelected]);

return (
<tet.span
tabIndex={tabIndex}
data-state={state}
onClick={handleOnClick}
data-testid="selectable-pill"
{...styles.container}
{...rest}
Expand Down
21 changes: 15 additions & 6 deletions src/docs-components/SelectablePillDocs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ const appearances = [false, true] as const;
const selected = [false, true] as const;

const props = [
{ text: 'Value' } as const,
{ text: 'Value', prefix: 'Prefix' } as const,
{ text: 'Value', beforeComponent: { icon: '20-tree' } } as const,
{ id: '0', text: 'Value' } as const,
{ id: '1', text: 'Value', prefix: 'Prefix' } as const,
{ id: '2', text: 'Value', beforeComponent: { icon: '20-tree' } } as const,
{
id: '3',
text: 'Value',
prefix: 'Prefix',
beforeComponent: { icon: '20-tree' },
} as const,
{
id: '4',
text: 'Value',
beforeComponent: { avatar: { initials: 'M' } },
} as const,
{
id: '5',
text: 'Value',
beforeComponent: {
avatar: { image: 'https://thispersondoesnotexist.com/' },
},
} as const,
{
id: '6',
text: 'Value',
prefix: 'Prefix',
beforeComponent: {
Expand All @@ -55,7 +59,7 @@ export const SelectablePillDocs: FC = () => (
</SectionHeader>
{appearances.map((appearance) => (
<tet.div
key={String(appearance)}
key={`${state}-${appearance}`}
display="flex"
flexDirection="column"
bg={appearance ? '$color-background-neutral-subtle' : undefined}
Expand All @@ -71,7 +75,11 @@ export const SelectablePillDocs: FC = () => (
</SectionHeader>

{selected.map((select) => (
<tet.div px="$dimension-1000" pb="$dimension-500">
<tet.div
px="$dimension-1000"
pb="$dimension-500"
key={`${state}-${appearance}-${select}`}
>
<SectionHeader variant="H3" as="h4" pt="$dimension-500">
Selected: {String(select)}
</SectionHeader>
Expand All @@ -88,8 +96,9 @@ export const SelectablePillDocs: FC = () => (
gap="$dimension-300"
py="$dimension-500"
>
{props.map((prop) => (
{props.map(({ id, ...prop }) => (
<SelectablePill
key={id}
state={state}
isInverted={appearance}
isSelected={select}
Expand Down

0 comments on commit 7c18472

Please sign in to comment.