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

feat(core): Implements chip component and chip delete component #38

Merged
merged 15 commits into from
Nov 25, 2024
Merged
4 changes: 3 additions & 1 deletion apps/tester/src/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type Fixture = {
alterColors?: UIColor[];
alterStates?: UIState[];
waitTime?: number;
maxDiffPixels?: number;
renderJoyElement: ElementRenderer;
renderTjElement: ElementRenderer;
};
Expand Down Expand Up @@ -65,6 +66,7 @@ export function testEach(
alterColors,
alterStates,
waitTime,
maxDiffPixels,
renderJoyElement,
renderTjElement,
} of fixtures) {
Expand Down Expand Up @@ -174,7 +176,7 @@ export function testEach(
await page.getByTestId(containerTestId),
).toHaveScreenshot(screenshotName, {
animations: 'disabled',
maxDiffPixels: 1,
maxDiffPixels,
});
if (state === 'active') {
await sleep(100);
Expand Down
107 changes: 107 additions & 0 deletions apps/tester/tests/23-chip.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import SunIcon from '@mui/icons-material/LightMode';
import { MdLightMode } from 'react-icons/md';
import { Chip as JoyChip } from '@mui/joy';
import { Chip as TJChip } from 'tailwind-joy/components';

import type { Fixture } from '@/settings';
import { testEach } from '@/settings';

const containerClassName =
'flex h-[100px] w-[200px] items-center justify-center p-2';

const fixtures: Fixture[] = [
{
title: 'basics',
renderJoyElement({ testId, size, variant, color }) {
return (
<JoyChip
data-testid={testId}
size={size}
variant={variant}
color={color}
>
This is a chip
</JoyChip>
);
},
renderTjElement({ testId, size, variant, color }) {
return (
<TJChip
data-testid={testId}
size={size}
variant={variant}
color={color}
>
This is a chip
</TJChip>
);
},
},
{
title: 'decorators',
maxDiffPixels: 2,
renderJoyElement({ testId, size, variant, color }) {
return (
<JoyChip
data-testid={testId}
size={size}
variant={variant}
color={color}
startDecorator={<SunIcon />}
>
This is a chip
</JoyChip>
);
},
renderTjElement({ testId, size, variant, color, iconClassName }) {
return (
<TJChip
data-testid={testId}
size={size}
variant={variant}
color={color}
startDecorator={<MdLightMode className={iconClassName} />}
>
This is a chip
</TJChip>
);
},
},
{
title: 'clickable',
alterStates: ['default', 'hover', 'focus-visible', 'active', 'disabled'],
renderJoyElement({ testId, size, variant, color, state }) {
return (
<JoyChip
data-testid={testId}
size={size}
variant={variant}
color={color}
disabled={state === 'disabled'}
onClick={() => {}}
>
This is a chip
</JoyChip>
);
},
renderTjElement({ testId, size, variant, color, state }) {
return (
<TJChip
data-testid={testId}
size={size}
variant={variant}
color={color}
disabled={state === 'disabled'}
onClick={() => {}}
>
This is a chip
</TJChip>
);
},
},
];

testEach(fixtures, {
containerClassName,
viewport: { width: 500, height: 500 },
});
51 changes: 51 additions & 0 deletions apps/tester/tests/24-chip-delete.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ChipDelete as JoyChipDelete, Chip as JoyChip } from '@mui/joy';
import {
ChipDelete as TJChipDelete,
Chip as TJChip,
} from 'tailwind-joy/components';

import type { Fixture } from '@/settings';
import { testEach } from '@/settings';

const containerClassName =
'flex h-[100px] w-[200px] items-center justify-center p-2';

const fixtures: Fixture[] = [
{
title: 'basics',
alterStates: ['default', 'hover', 'focus-visible', 'active', 'disabled'],
renderJoyElement({ testId, size, variant, color, state }) {
return (
<JoyChip
data-testid={testId}
size={size}
variant={variant}
color={color}
disabled={state === 'disabled'}
endDecorator={<JoyChipDelete onDelete={() => {}} />}
>
This is a chip
</JoyChip>
);
},
renderTjElement({ testId, size, variant, color, state }) {
return (
<TJChip
data-testid={testId}
size={size}
variant={variant}
color={color}
disabled={state === 'disabled'}
endDecorator={<TJChipDelete onDelete={() => {}} />}
>
This is a chip
</TJChip>
);
},
},
];

testEach(fixtures, {
containerClassName,
viewport: { width: 500, height: 500 },
});
1 change: 1 addition & 0 deletions apps/tester/tests/70-switch.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const fixtures: Fixture[] = [
{
title: 'decorators',
alterStates: ['default', 'hover', 'focus-visible', 'active', 'disabled'],
maxDiffPixels: 1,
renderJoyElement({ testId, size, variant, color, state }) {
return (
<JoySwitch
Expand Down
110 changes: 110 additions & 0 deletions apps/website/docs/apis/23-chip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
sidebar_position: 23
title: <Chip />
---

# Chip API

<AvailableFrom version="0.5.0" />

API reference docs for the React Chip component.
Learn about the props of this exported module.

## Demos

:::tip

For examples and details on the usage of this React component, visit the component demo pages:

- [Chip](../components/chip)

:::

## Import

```tsx
import { Chip } from 'tailwind-joy/components';
```

## Props

:::info

The `ref` is forwarded to the root element.

:::

### `className`

Class name applied to the root element.

- Type: `string`

### `color`

The color of the component.

- Type: `'primary' | 'neutral' | 'danger' | 'success' | 'warning'`
- Default: `'neutral'`

### `component`

The component used for the root node.

- Type: `keyof JSX.IntrinsicElements`
- Default: `'div'`

### `disabled`

If `true`, the component is disabled.

- Type: `boolean`
- Default: `false`

### `endDecorator`

Element placed after the children.

- Type: `ReactNode`

### `onClick`

Element action click handler.

- Type: `MouseEventHandler`

### `size`

The size of the component.

- Type: `'sm' | 'md' | 'lg'`
- Default: `'md'`

### `slotProps`

The props used for each slot inside.

- Type:
```tsx
{
root?: ComponentProps<'div'>;
label?: ComponentProps<'span'>;
action?: ComponentProps<'button'>;
startDecorator?: ComponentProps<'span'>;
endDecorator?: ComponentProps<'span'>;
}
```
- Default: `{}`

### `startDecorator`

Element placed before the children.

- Type: `ReactNode`

### `variant`

The variant of the component.

- Type: `'solid' | 'soft' | 'outlined' | 'plain'`
- Default: `'soft'`
90 changes: 90 additions & 0 deletions apps/website/docs/apis/24-chip-delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
sidebar_position: 24
title: <ChipDelete />
---

# ChipDelete API

<AvailableFrom version="0.5.0" />

API reference docs for the React ChipDelete component.
Learn about the props of this exported module.

## Demos

:::tip

For examples and details on the usage of this React component, visit the component demo pages:

- [Chip](../components/chip)

:::

## Import

```tsx
import { ChipDelete } from 'tailwind-joy/components';
```

## Props

:::info

The `ref` is forwarded to the root element.

:::

### `className`

Class name applied to the root element.

- Type: `string`

### `color`

The color of the component.

- Type: `'primary' | 'neutral' | 'danger' | 'success' | 'warning'`
- Default: `'neutral'`

### `component`

The component used for the root node.

- Type: `keyof JSX.IntrinsicElements`
- Default: `'button'`

### `disabled`

If `true`, the component is disabled.
If `undefined`, the value inherits from the parent chip.

- Type: `boolean`

### `onDelete`

Callback fired when the component is not disabled and either:

- `Backspace`, `Enter` or `Delete` is pressed.
- The component is clicked.

* Type: `(event: KeyboardEvent | MouseEvent) => void`

### `slotProps`

The props used for each slot inside.

- Type:
```tsx
{
root?: ComponentProps<'button'>;
}
```
- Default: `{}`

### `variant`

The variant of the component.

- Type: `'solid' | 'soft' | 'outlined' | 'plain'`
- Default: `'plain'`
Loading