Skip to content

Commit

Permalink
feature/CC 7 radio (#17)
Browse files Browse the repository at this point in the history
* Radio, Radio List, Checkbox update

* Try import on provider

* Remove try
  • Loading branch information
joaltoroc authored Jan 11, 2024
1 parent d615d5d commit 213bbb2
Show file tree
Hide file tree
Showing 25 changed files with 844 additions and 30 deletions.
13 changes: 13 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
<!-- markdownlint-configure-file { "MD024": { "allow_different_nesting": true } } -->
# Changelog

## v0.3.0 - Radio (Jan 10 - 2024)

### 👾 Feature

- Radio

### 🐛 Bugs

- Checkbox update

## v0.2.0 - Checkbox (Jan 10 - 2024)

### 👾 Feature

- Checkbox

### 🐛 Bugs

- Fix name Dropdown
- Fix build chromatic
- New util isValidUrl
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"framework design",
"design system"
],
"version": "0.2.0",
"version": "0.3.0",
"homepage": "https://github.com/creativecodeco/ui",
"author": {
"name": "John Toro",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export * from '@/ui/components';
export * from '@/ui/forms';
export * from '@/ui/provider';

export * from '@/types';
export type * from '@/types';

export * from '@/utils';
1 change: 1 addition & 0 deletions src/theme/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

@import './avatar.css';
@import './checkbox.css';
@import './radio.css';
@import './text-box.css';
47 changes: 47 additions & 0 deletions src/theme/radio.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.radio-color {
&-primary {
@apply radio-primary;
}

&-secondary {
@apply radio-secondary;
}

&-accent {
@apply radio-accent;
}

&-success {
@apply radio-success;
}

&-warning {
@apply radio-warning;
}

&-info {
@apply radio-info;
}

&-error {
@apply radio-error;
}
}

.radio-size {
&-xs {
@apply radio-xs;
}

&-sm {
@apply radio-sm;
}

&-md {
@apply radio-md;
}

&-lg {
@apply radio-lg;
}
}
9 changes: 9 additions & 0 deletions src/types/ui/base/constants.types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export type SizeType = 'xs' | 'sm' | 'md' | 'lg';
export type ColorType =
| 'primary'
| 'secondary'
| 'accent'
| 'success'
| 'warning'
| 'info'
| 'error';
export type PositionType = 'left' | 'right';
2 changes: 1 addition & 1 deletion src/types/ui/components/avatar.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SizeType } from '@/types';
import type { SizeType } from '@/types';

export interface AvatarType {
src: string;
Expand Down
23 changes: 12 additions & 11 deletions src/types/ui/forms/checkbox.types.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import type { ErrorType, SizeType } from '@/types';
import type { ColorType, ErrorType, PositionType, SizeType } from '@/types';

export interface CheckboxType extends ErrorType {
export interface CheckboxItemType {
label?: string;
checked?: boolean;
value: string | number;
}

export interface CheckboxType extends CheckboxItemType, ErrorType {
name: string;

label?: string;
checked?: boolean;

position?: 'left' | 'right';
color?:
| 'primary'
| 'secondary'
| 'accent'
| 'success'
| 'warning'
| 'info'
| 'error';
position?: PositionType;
color?: ColorType;

size?: SizeType;

disabled?: boolean;

onChange?: (value: CheckboxItemType) => void;
}

export type CheckboxRef = HTMLInputElement;
1 change: 1 addition & 0 deletions src/types/ui/forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type * from './checkbox.types';
export type * from './dropdown.types';
export type * from './radio.types';
export type * from './text-box.types';
29 changes: 29 additions & 0 deletions src/types/ui/forms/radio.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ColorType, ErrorType, PositionType, SizeType } from '@/types';

export interface RadioItemType {
label?: string;
checked?: boolean;
value?: string | number;
}

export interface RadioType extends RadioItemType, ErrorType {
name: string;

label?: string;
checked?: boolean;

position?: PositionType;
color?: ColorType;

size?: SizeType;

disabled?: boolean;

onChange?: (value: RadioItemType) => void;
}

export type RadioRef = HTMLInputElement;

export interface RadioListType extends RadioType {
options: Omit<RadioType, 'name'>[];
}
17 changes: 14 additions & 3 deletions src/ui/forms/checkbox/checkbox.component.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
import React, { forwardRef } from 'react';
import cls from 'classnames';

import type { CheckboxRef, CheckboxType } from '@/types';
import type { CheckboxItemType, CheckboxRef, CheckboxType } from '@/types';

const Checkbox = forwardRef<CheckboxRef, CheckboxType>(
(
{
name,
label,
isError,
checked,
disabled,
error,
position = 'left',
color,
size = 'md',
checked,
value,
onChange,
...otherProps
},
ref
) => {
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
onChange?.({
isChecked: e.target.checked,
value,
label
} as CheckboxItemType);
};

const checkbox = () => (
<input
id={name}
name={name}
data-testid={name}
ref={ref}
type='checkbox'
checked={checked}
className={cls('checkbox', {
[`checkbox-color-${color}`]: color,
[`checkbox-size-${size}`]: size
})}
disabled={disabled}
checked={checked}
onChange={handleChange}
{...otherProps}
/>
);
Expand Down
38 changes: 29 additions & 9 deletions src/ui/forms/checkbox/checkbox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { render } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';

import type { CheckboxType } from '@/types';

import Checkbox from './checkbox.component';

const baseProps: CheckboxType = {
name: 'test',
value: 'test'
};

describe('<Checkbox />', () => {
it('snapshot', () => {
const { container } = render(<Checkbox name='test' />);
const { container } = render(<Checkbox {...baseProps} />);

expect(container).toMatchSnapshot();
});

it('snapshot full props', () => {
const { container } = render(
<Checkbox
name='test'
{...baseProps}
checked
color='primary'
size='lg'
Expand All @@ -28,48 +36,60 @@ describe('<Checkbox />', () => {

it('label', () => {
const label = 'Hello Word';
const { getByText } = render(<Checkbox name='test' label={label} />);
const { getByText } = render(<Checkbox {...baseProps} label={label} />);

expect(getByText(label)).toBeInTheDocument();
});

it('with error', () => {
const error = 'Error message';
const { getByText } = render(
<Checkbox name='test' isError error={error} />
<Checkbox {...baseProps} isError error={error} />
);

expect(getByText(error)).toBeInTheDocument();
});

it('disabled', () => {
const { getByTestId } = render(<Checkbox name='test' disabled />);
const { getByTestId } = render(<Checkbox {...baseProps} disabled />);

expect(getByTestId('test')).toHaveProperty('disabled', true);
});

it('position right', () => {
const label = 'Hello Word';
const { getByText } = render(
<Checkbox name='test' label={label} position='right' />
<Checkbox {...baseProps} label={label} position='right' />
);

expect(getByText(label)).toBeInTheDocument();
});

it('color', () => {
const { container } = render(<Checkbox name='test' color='primary' />);
const { container } = render(<Checkbox {...baseProps} color='primary' />);

expect(container.querySelector('input')?.className).toEqual(
'checkbox checkbox-color-primary checkbox-size-md'
);
});

it('size', () => {
const { container } = render(<Checkbox name='test' size='lg' />);
const { container } = render(<Checkbox {...baseProps} size='lg' />);

expect(container.querySelector('input')?.className).toEqual(
'checkbox checkbox-size-lg'
);
});

it('onChange', () => {
const onChange = jest.fn();

const { getByRole } = render(
<Checkbox {...baseProps} size='lg' onChange={onChange} />
);

fireEvent.click(getByRole('checkbox'));

expect(onChange).toHaveBeenCalledTimes(1);
});
});
4 changes: 2 additions & 2 deletions src/ui/forms/dropdown/dropdown.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { fireEvent, render, screen } from '@testing-library/react';

import Dropdown from './dropdown.component';

import type { DropdownOption } from '@/types';

import Dropdown from './dropdown.component';

const options = Array.from({ length: 5 }).map<DropdownOption>((_, index) => ({
value: index,
label: `Option ${index}`
Expand Down
2 changes: 2 additions & 0 deletions src/ui/forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from '@/ui/forms/checkbox';
export * from '@/ui/forms/dropdown';
export * from '@/ui/forms/radio';
export * from '@/ui/forms/radio-list';
export * from '@/ui/forms/text-box';
Loading

0 comments on commit 213bbb2

Please sign in to comment.