-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Implemented singleselectradio component
- Loading branch information
Showing
4 changed files
with
126 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useState } from 'react'; | ||
import { ComponentContainer, RadioInput, RadioLabel } from './styles'; | ||
|
||
interface Option { | ||
label: string; | ||
value: string; | ||
} | ||
|
||
interface SingleSelectRadioGroupProps { | ||
name: string; | ||
options: Option[]; | ||
defaultValue?: string; | ||
onChange?: (value: string) => void; | ||
} | ||
|
||
export default function SingleSelectRadioGroup({ | ||
name, | ||
options, | ||
defaultValue, | ||
onChange, | ||
}: SingleSelectRadioGroupProps) { | ||
const [selectedValue, setSelectedValue] = useState( | ||
defaultValue || options[0].value, | ||
); | ||
|
||
const handleChange = (value: string) => { | ||
setSelectedValue(value); | ||
if (onChange) onChange(value); | ||
}; | ||
|
||
return ( | ||
<div> | ||
{options.map(option => ( | ||
<ComponentContainer | ||
key={option.value} | ||
isSelected={selectedValue === option.value} | ||
> | ||
<RadioLabel isSelected={selectedValue === option.value}> | ||
{option.label} | ||
</RadioLabel> | ||
<RadioInput | ||
name={name} | ||
value={option.value} | ||
checked={selectedValue === option.value} | ||
onChange={() => handleChange(option.value)} | ||
/> | ||
</ComponentContainer> | ||
))} | ||
</div> | ||
); | ||
} | ||
|
||
// Example usage | ||
// import SingleSelectRadioGroup from '@/components/SingleSelectRadio'; | ||
// export default function Home() { | ||
// return ( | ||
// <Container> | ||
// <Image src={BPLogo} alt="Blueprint Logo" /> | ||
// <p>Open up app/page.tsx to get started!</p> | ||
// <SingleSelectRadioGroup | ||
// name="exampleRadioGroup" | ||
// options={[ | ||
// { label: 'School', value: 'option1' }, | ||
// { label: 'Community', value: 'option2' }, | ||
// { label: 'Individual', value: 'option3' }, | ||
// ]} | ||
// defaultValue="option2" | ||
// /> | ||
// </Container> | ||
// ); | ||
// } |
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,40 @@ | ||
import styled, { css } from 'styled-components'; | ||
import COLORS from '@/styles/colors'; | ||
|
||
interface SelectedProps { | ||
isSelected: boolean; | ||
} | ||
|
||
export const ComponentContainer = styled.div<SelectedProps>` | ||
display: flex; | ||
flex-direction: row; | ||
gap: 0.9375rem; | ||
background-color: white; | ||
border: 0.125rem solid transparent; | ||
border-radius: 8px; | ||
width: 21rem; | ||
height: 3.1875rem; | ||
align-items: center; | ||
justify-content: space-between; | ||
padding-left: 1.188rem; | ||
padding-right: 1rem; | ||
margin-bottom: 1.125rem; | ||
${({ isSelected }) => | ||
isSelected && | ||
css` | ||
border-color: ${COLORS.shrub}; // Changes border color when selected | ||
background-color: rgba(148, 181, 6, 0.1); //not added to color styles yet | ||
`} | ||
`; | ||
|
||
export const RadioInput = styled.input.attrs({ type: 'radio' })` | ||
width: 1.5625rem; | ||
height: 1.5625rem; | ||
&:checked { | ||
accent-color: ${COLORS.shrub}; // Changes the radio fill color | ||
} | ||
`; | ||
export const RadioLabel = styled.label<SelectedProps>` | ||
color: ${({ isSelected }) => (isSelected ? COLORS.shrub : COLORS.midgray)}; | ||
font-size: 1.25rem; | ||
`; |
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