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(component): Allow Label as self-closing tag, resolves #173 #207

Merged
merged 2 commits into from
Jun 8, 2022
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
21 changes: 14 additions & 7 deletions src/lib/components/FormControls/Label.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { render } from '@testing-library/react';
import { HiGlobe, HiLockClosed } from 'react-icons/hi';
import { describe, expect, it } from 'vitest';

import { Button } from '../Button';
import { Checkbox } from './Checkbox';
import { FileInput } from './FileInput';
Expand All @@ -13,14 +12,8 @@ import { TextInput } from './TextInput';
import { ToggleSwitch } from './ToggleSwitch';

describe.concurrent('Components / Form controls / Label', () => {
it('should render', () => {
render(<Label>Hello</Label>);
});

describe('A11y', () => {
it('should provide accessible name to any form control associated by `htmlFor`', () => {
const { getByLabelText } = render(<TestForm />);

const inputLabels = [
'Your email',
'Your password',
Expand All @@ -31,9 +24,23 @@ describe.concurrent('Components / Form controls / Label', () => {
'Your message',
];

const { getByLabelText } = render(<TestForm />);

inputLabels.forEach((label) => expect(getByLabelText(label)).toHaveAccessibleName(label));
});
});

describe('Rendering', () => {
it('should render', () => {
render(<Label>Hello</Label>);
});

describe('`value=".."`', () => {
it('should render', () => {
render(<Label value="Hello" />);
});
});
});
});

const TestForm = (): JSX.Element => (
Expand Down
19 changes: 11 additions & 8 deletions src/lib/components/FormControls/Label.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import type { ComponentProps, FC } from 'react';
import classNames from 'classnames';
import type { ComponentProps, FC, PropsWithChildren } from 'react';

type Color = 'gray' | 'green' | 'red';

export type LabelProps = ComponentProps<'label'> & {
export interface LabelProps extends PropsWithChildren<ComponentProps<'label'>> {
color?: Color;
};
value?: string;
}

const colorClasses: Record<Color, string> = {
gray: 'text-gray-900 dark:text-gray-300',
green: 'text-green-700 dark:text-green-500',
red: 'text-red-700 dark:text-red-500',
};

export const Label: FC<LabelProps> = ({ children, color = 'gray', className, ...props }) => (
<label className={classNames('text-sm font-medium', colorClasses[color], className)} {...props}>
{children}
</label>
);
export const Label: FC<LabelProps> = ({ children, color = 'gray', className, value, ...props }): JSX.Element => {
return (
<label className={classNames('text-sm font-medium', colorClasses[color], className)} {...props}>
{value ?? children ?? ''}
</label>
);
};