-
Notifications
You must be signed in to change notification settings - Fork 3
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: Radio group implementation #73
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { fireEvent, render } from '@testing-library/react' | ||
import React from 'react' | ||
|
||
import { Radio, RadioGroup } from '../radio-group' | ||
|
||
describe('RadioGroup', () => { | ||
const radioItems = [ | ||
{ label: 'Option 1', value: 'option1', checked: true }, | ||
{ label: 'Option 2', value: 'option2' }, | ||
] | ||
|
||
it('should have the `flex-row` class when the `inline` variant is passed', () => { | ||
const { queryByRole } = render( | ||
<RadioGroup variant="inline" items={radioItems} name="radioName" />, | ||
) | ||
|
||
expect(queryByRole('tabpanel')).toBeInTheDocument() | ||
expect(queryByRole('tabpanel')).toHaveClass('flex-row') | ||
}) | ||
|
||
it('renders radio group correctly with items', () => { | ||
const { getByRole } = render(<RadioGroup items={radioItems} name="radioGroup" />) | ||
|
||
const radioGroup = getByRole('tabpanel') | ||
expect(radioGroup).toBeInTheDocument() | ||
expect(radioGroup.childNodes.length).toBe(2) // Ensure two radio buttons are rendered | ||
}) | ||
|
||
it('renders radio group with no element checked by default', () => { | ||
const radioItemsNotChecked = [ | ||
{ label: 'Option 1', value: 'option1' }, | ||
{ label: 'Option 2', value: 'option2' }, | ||
] | ||
const { getByLabelText } = render(<RadioGroup items={radioItemsNotChecked} name="radioGroup" />) | ||
|
||
const firstRadioButton = getByLabelText('Option 1') | ||
const secondRadioButton = getByLabelText('Option 2') | ||
|
||
expect(firstRadioButton).not.toBeChecked() | ||
expect(secondRadioButton).not.toBeChecked() | ||
}) | ||
|
||
it('handles keyboard navigation', () => { | ||
const { getByLabelText } = render(<RadioGroup items={radioItems} name="radioGroup" />) | ||
|
||
const radioGroup = getByLabelText('Option 1') | ||
fireEvent.keyDown(radioGroup, { key: 'ArrowRight', code: 'ArrowRight' }) | ||
let secondRadioButton = getByLabelText('Option 2') | ||
expect(secondRadioButton).toBeChecked() | ||
|
||
fireEvent.keyDown(radioGroup, { key: 'ArrowLeft', code: 'ArrowLeft' }) | ||
let firstRadioButton = getByLabelText('Option 1') | ||
expect(firstRadioButton).toBeChecked() | ||
|
||
fireEvent.keyDown(radioGroup, { key: 'ArrowUp', code: 'ArrowUp' }) | ||
secondRadioButton = getByLabelText('Option 2') | ||
expect(secondRadioButton).toBeChecked() | ||
|
||
fireEvent.keyDown(radioGroup, { key: 'ArrowDown', code: 'ArrowDown' }) | ||
firstRadioButton = getByLabelText('Option 1') | ||
expect(firstRadioButton).toBeChecked() | ||
}) | ||
|
||
it('changes selection on arrow keys', () => { | ||
const { getByLabelText } = render(<RadioGroup items={radioItems} name="radioGroup" />) | ||
|
||
const radioGroup = getByLabelText('Option 1') | ||
fireEvent.keyDown(radioGroup, { key: 'ArrowRight', code: 'ArrowRight' }) | ||
fireEvent.keyDown(radioGroup, { key: 'Enter', code: 'Enter' }) | ||
const secondRadioButton = getByLabelText('Option 2') | ||
expect(secondRadioButton).toBeChecked() | ||
}) | ||
|
||
it('changes selection on clicking radio buttons', () => { | ||
const { getByLabelText } = render(<RadioGroup items={radioItems} name="radioGroup" />) | ||
|
||
const secondRadioButton = getByLabelText('Option 2') | ||
fireEvent.click(secondRadioButton) | ||
expect(secondRadioButton).toBeChecked() | ||
}) | ||
}) | ||
|
||
describe('Radio', () => { | ||
it('renders radio button correctly with label', () => { | ||
const { getByLabelText } = render(<Radio label="Option 1" value="option1" name="radioGroup" />) | ||
|
||
const radioButton = getByLabelText('Option 1') | ||
expect(radioButton).toBeInTheDocument() | ||
expect(radioButton).toHaveAttribute('type', 'radio') | ||
expect(radioButton).not.toBeChecked() | ||
|
||
fireEvent.click(radioButton) | ||
expect(radioButton).toBeChecked() | ||
}) | ||
|
||
it('renders disabled radio button', () => { | ||
const { getByLabelText } = render( | ||
<Radio label="Option 1" value="option1" name="radioGroup" disabled />, | ||
) | ||
|
||
const radioButton = getByLabelText('Option 1') | ||
expect(radioButton).toBeDisabled() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { type VariantProps, cva } from 'class-variance-authority' | ||
import React, { useEffect, useMemo, useState } from 'react' | ||
|
||
import { cn } from '@/utils/cn' | ||
|
||
export interface RadioProps { | ||
checked?: boolean | ||
label?: string | ||
value: string | ||
name: string | ||
id?: string | ||
disabled?: boolean | ||
onChange?: any | ||
} | ||
|
||
export const Radio = ({ | ||
label, | ||
id, | ||
name, | ||
value, | ||
disabled, | ||
onChange, | ||
checked, | ||
}: RadioProps): JSX.Element => { | ||
const inputId = id || `id-${name}-${value}` | ||
|
||
return ( | ||
<div className="flex items-center"> | ||
<input | ||
id={inputId} | ||
type="radio" | ||
disabled={disabled} | ||
value={value} | ||
name={name} | ||
className="hidden" | ||
onChange={onChange} | ||
checked={checked} | ||
/> | ||
|
||
<label htmlFor={inputId} className="flex items-center"> | ||
<span className="w-6 h-6 inline-block rounded-full border-2 border-base" /> | ||
{label ? <p className="text-base text-medium leading-6 ms-2">{label}</p> : ''} | ||
</label> | ||
</div> | ||
) | ||
} | ||
|
||
const radioGroupVariants = cva(['flex gap-3'], { | ||
variants: { | ||
variant: { | ||
default: 'flex-col', | ||
inline: 'flex-row', | ||
}, | ||
fullWidth: { | ||
true: 'w-full', | ||
}, | ||
}, | ||
defaultVariants: { | ||
variant: 'default', | ||
}, | ||
}) | ||
|
||
export interface RadioGroupProps | ||
extends VariantProps<typeof radioGroupVariants>, | ||
React.InputHTMLAttributes<HTMLInputElement> { | ||
disabled?: boolean | ||
items: Omit<RadioProps, 'name'>[] | ||
name: string | ||
} | ||
|
||
export const RadioGroup = ({ | ||
items, | ||
variant, | ||
name, | ||
fullWidth, | ||
disabled, | ||
className, | ||
}: RadioGroupProps) => { | ||
const checkedItem = useMemo(() => items.findIndex(item => item.checked), [items]) | ||
const [selected, setSelected] = useState(checkedItem) | ||
|
||
const handleKeyDown = (event: any) => { | ||
if (event.code === 'ArrowRight' || event.code === 'ArrowDown') { | ||
event.preventDefault() | ||
|
||
const nextIndex = (selected + 1) % items.length | ||
setSelected(nextIndex) | ||
} else if (event.code === 'ArrowLeft' || event.code === 'ArrowUp') { | ||
event.preventDefault() | ||
|
||
const prevIndex = selected > 0 ? selected - 1 : items.length - 1 | ||
setSelected(prevIndex) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
const handleKeyPress = (event: any) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto above. Let's type the event. |
||
if (event.target.type === 'radio' && event.key === 'Enter') { | ||
setSelected(Number(event.target.value)) | ||
} | ||
} | ||
|
||
document.addEventListener('keypress', handleKeyPress) | ||
return () => { | ||
document.removeEventListener('keypress', handleKeyPress) | ||
} | ||
}, []) | ||
raducristianpopa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ( | ||
//eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions | ||
<div | ||
tabIndex={0} | ||
className={cn(radioGroupVariants({ variant, fullWidth }), className, 'outline-none')} | ||
dianafulga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
onKeyDown={handleKeyDown} | ||
dianafulga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
role="tabpanel"> | ||
dianafulga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{items.map((item, index) => ( | ||
<Radio | ||
key={`key-${name}-${item.value}`} | ||
{...item} | ||
name={name} | ||
disabled={disabled} | ||
checked={selected === index} | ||
onChange={() => setSelected(index)} | ||
/> | ||
))} | ||
</div> | ||
) | ||
} |
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,33 +1,27 @@ | ||
{ | ||
"name": "__MSG_appName__", | ||
"version": "1.0.1", | ||
"manifest_version": 2, | ||
"description": "__MSG_appDescription__", | ||
"icons": { | ||
"34": "assets/icons/icon-34.png", | ||
"128": "assets/icons/icon-128.png" | ||
}, | ||
"default_locale": "en", | ||
"content_scripts": [ | ||
{ | ||
"matches": ["http://*/*", "https://*/*", "<all_urls>"], | ||
"js": ["content/content.js"] | ||
} | ||
], | ||
"background": { | ||
"scripts": ["background/background.js"] | ||
}, | ||
"permissions": ["tabs", "storage"], | ||
"browser_action": { | ||
"default_icon": "assets/icons/icon-34.png", | ||
"default_title": "Web Monetization", | ||
"default_popup": "popup/index.html" | ||
}, | ||
"web_accessible_resources": [ | ||
"assets/*", | ||
"content/*", | ||
"options/*", | ||
"popup/*", | ||
"background/*" | ||
] | ||
"name": "__MSG_appName__", | ||
"version": "1.0.1", | ||
"manifest_version": 2, | ||
"description": "__MSG_appDescription__", | ||
"icons": { | ||
"34": "assets/icons/icon-34.png", | ||
"128": "assets/icons/icon-128.png" | ||
}, | ||
"default_locale": "en", | ||
"content_scripts": [ | ||
{ | ||
"matches": ["http://*/*", "https://*/*", "<all_urls>"], | ||
"js": ["content/content.js"] | ||
} | ||
], | ||
"background": { | ||
"scripts": ["background/background.js"] | ||
}, | ||
"permissions": ["tabs", "storage"], | ||
"browser_action": { | ||
"default_icon": "assets/icons/icon-34.png", | ||
"default_title": "Web Monetization", | ||
"default_popup": "popup/index.html" | ||
}, | ||
"web_accessible_resources": ["assets/*", "content/*", "options/*", "popup/*", "background/*"] | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's get rid of the
any
.