-
-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(component): Allow `Label` as self-closing tag, resolves #173 Instead of relying on`children`, allow the use of prop `value`. * test(component): Add unit test for `<Label value="">`
- Loading branch information
1 parent
2f9f345
commit 17f5e8e
Showing
2 changed files
with
25 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |