-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add Checkbox and Label components #100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
578260f
feat: Add Checkbox and Label components
kylengn 4702279
refactor: Enhance Checkbox and Label components with improved ID gene…
kylengn f6df4ba
chore(changeset): Add checkbox and label components
kylengn 74bfb21
Merge branch 'main' into feat/add-checkbox-and-label
kylengn 80f4a80
Merge remote-tracking branch 'origin' into feat/add-checkbox-and-label
kylengn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@shipfox/react-ui": minor | ||
| --- | ||
|
|
||
| Add checkbox and label components |
This file contains hidden or 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
125 changes: 125 additions & 0 deletions
125
libs/react/ui/src/components/checkbox/checkbox-label.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import {useId} from 'react'; | ||
| import {cn} from 'utils/cn'; | ||
| import {Icon} from '../icon/icon'; | ||
| import {Label} from '../label/label'; | ||
| import {Checkbox, type CheckboxProps} from './checkbox'; | ||
|
|
||
| export type CheckboxLabelProps = Omit<CheckboxProps, 'id'> & { | ||
| id?: string; | ||
| label: string; | ||
| optional?: boolean; | ||
| description?: string; | ||
| showInfoIcon?: boolean; | ||
| border?: boolean; | ||
| className?: string; | ||
| labelClassName?: string; | ||
| descriptionClassName?: string; | ||
| }; | ||
|
|
||
| export function CheckboxLabel({ | ||
| id, | ||
| label, | ||
| optional = false, | ||
| description, | ||
| showInfoIcon = false, | ||
| border = false, | ||
| className, | ||
| labelClassName, | ||
| descriptionClassName, | ||
| ...checkboxProps | ||
| }: CheckboxLabelProps) { | ||
| const generateId = useId(); | ||
| const checkboxId = id || generateId; | ||
| const isDisabled = checkboxProps.disabled ?? false; | ||
|
|
||
| const renderContent = (checkboxId: string) => ( | ||
| <div className="flex flex-col gap-4 flex-1 min-w-0"> | ||
| <div className="flex gap-4 items-center"> | ||
| <Label | ||
| className={cn( | ||
| 'text-sm leading-20 overflow-hidden text-ellipsis whitespace-nowrap', | ||
| isDisabled | ||
| ? 'font-normal text-foreground-neutral-subtle' | ||
| : 'font-medium text-foreground-neutral-base', | ||
| labelClassName, | ||
| )} | ||
| htmlFor={checkboxId} | ||
| > | ||
| {label} | ||
| </Label> | ||
| {optional && ( | ||
| <span className="text-sm leading-20 font-regular text-foreground-neutral-muted whitespace-nowrap"> | ||
| (Optional) | ||
| </span> | ||
| )} | ||
| {showInfoIcon && ( | ||
| <Icon | ||
| name="info" | ||
| className="size-16 text-foreground-neutral-muted shrink-0" | ||
| aria-hidden="true" | ||
| /> | ||
| )} | ||
| </div> | ||
| {description && ( | ||
| <p | ||
| className={cn( | ||
| 'text-sm leading-20', | ||
| isDisabled ? 'text-foreground-neutral-disabled' : 'text-foreground-neutral-subtle', | ||
| descriptionClassName, | ||
| )} | ||
| > | ||
| {description} | ||
| </p> | ||
| )} | ||
| </div> | ||
| ); | ||
|
|
||
| if (border) { | ||
| return ( | ||
| <Label | ||
| htmlFor={checkboxId} | ||
| className={cn( | ||
| // Base container styles with border | ||
| 'flex items-start gap-10 rounded-8 p-8 transition-all duration-100', | ||
| // Unchecked state - default | ||
| 'bg-checkbox-unchecked-bg shadow-checkbox-unchecked', | ||
| // Unchecked state - hover | ||
| 'hover:bg-checkbox-unchecked-bg-hover', | ||
| // Unchecked state - focus | ||
| 'has-data-[state=unchecked]:focus-visible:shadow-border-interactive-with-active', | ||
| // Checked state - default | ||
| 'has-data-[state=checked]:bg-background-neutral-base has-data-[state=checked]:shadow-checkbox-checked', | ||
| // Checked state - hover | ||
| 'has-data-[state=checked]:hover:bg-background-neutral-hover', | ||
| // Checked state - focus | ||
| 'has-data-[state=checked]:focus-visible:shadow-checkbox-checked-focus', | ||
| // Indeterminate state - default | ||
| 'has-data-[state=indeterminate]:bg-background-neutral-base has-data-[state=indeterminate]:shadow-checkbox-indeterminate', | ||
| // Indeterminate state - hover | ||
| 'has-data-[state=indeterminate]:hover:bg-background-neutral-hover', | ||
| // Indeterminate state - focus | ||
| 'has-data-[state=indeterminate]:focus-visible:shadow-checkbox-indeterminate-focus', | ||
| // Disabled state | ||
| isDisabled && 'opacity-50 cursor-not-allowed', | ||
| !isDisabled && 'cursor-pointer', | ||
| className, | ||
| )} | ||
| > | ||
| <span className="p-4"> | ||
| <Checkbox id={checkboxId} {...checkboxProps} /> | ||
| </span> | ||
| {renderContent(checkboxId)} | ||
| </Label> | ||
| ); | ||
| } | ||
|
|
||
| // Without border variant | ||
| return ( | ||
| <div className={cn('flex items-start gap-10', className)}> | ||
| <span className="p-2"> | ||
| <Checkbox id={checkboxId} {...checkboxProps} /> | ||
| </span> | ||
| {renderContent(checkboxId)} | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import {Label} from 'components/label'; | ||
| import {type ReactNode, useId} from 'react'; | ||
| import {cn} from 'utils/cn'; | ||
| import {Checkbox, type CheckboxProps} from './checkbox'; | ||
|
|
||
| export type CheckboxLink = { | ||
| label: string; | ||
| href?: string; | ||
| onClick?: () => void; | ||
| }; | ||
|
|
||
| export type CheckboxLinksProps = Omit<CheckboxProps, 'id'> & { | ||
| id?: string; | ||
| label: string; | ||
| links: CheckboxLink[]; | ||
| separator?: ReactNode; | ||
| className?: string; | ||
| labelClassName?: string; | ||
| linkClassName?: string; | ||
| }; | ||
|
|
||
| export function CheckboxLinks({ | ||
| id, | ||
| label, | ||
| links, | ||
| separator, | ||
| className, | ||
| labelClassName, | ||
| linkClassName, | ||
| ...checkboxProps | ||
| }: CheckboxLinksProps) { | ||
| const generateId = useId(); | ||
| const checkboxId = id || generateId; | ||
| const isDisabled = checkboxProps.disabled ?? false; | ||
| const defaultSeparator = ( | ||
| <span className="size-3 rounded-full bg-foreground-neutral-muted" aria-hidden="true" /> | ||
| ); | ||
|
|
||
| return ( | ||
| <div className={cn('flex gap-10 items-start', className)}> | ||
| <span className="p-2"> | ||
| <Checkbox id={checkboxId} {...checkboxProps} /> | ||
| </span> | ||
| <div className="flex flex-col gap-4 items-start flex-1"> | ||
| <Label | ||
| htmlFor={checkboxId} | ||
| className={cn( | ||
| 'text-sm leading-20 font-medium text-foreground-neutral-base', | ||
| isDisabled && 'cursor-not-allowed opacity-50', | ||
| labelClassName, | ||
| )} | ||
| > | ||
| {label} | ||
| </Label> | ||
kylengn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <div className="flex gap-6 items-center"> | ||
| {links.map((link, index) => ( | ||
| <div key={link.label} className="flex gap-6 items-center"> | ||
| {link.href ? ( | ||
| <a | ||
| href={link.href} | ||
| onClick={link.onClick} | ||
| className={cn( | ||
| 'text-sm leading-20 font-medium text-foreground-highlight-interactive', | ||
| 'hover:text-foreground-highlight-interactive-hover', | ||
| linkClassName, | ||
| )} | ||
| > | ||
| {link.label} | ||
| </a> | ||
| ) : ( | ||
| <button | ||
| type="button" | ||
| onClick={link.onClick} | ||
| className={cn( | ||
| 'text-sm leading-20 font-medium text-foreground-highlight-interactive', | ||
| 'hover:text-foreground-highlight-interactive-hover', | ||
| linkClassName, | ||
| )} | ||
| > | ||
| {link.label} | ||
| </button> | ||
| )} | ||
| {index < links.length - 1 && (separator ?? defaultSeparator)} | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.