-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add MdRadioButton component #127
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Structure | ||
|
||
To use the `RadioGroup` css in `@miljodirektoratet/md-css` as a standalone, without the accompanying React component, please use the following HTML structure. | ||
|
||
Class names in brackets [] are optional-/togglable-/decorator- or state dependant classes. | ||
|
||
See [Storybook](https://miljodir.github.io/md-components) for examples and more info. | ||
|
||
```html | ||
<div className="md-radiobutton [md-checkbox--disabled]"> | ||
<span className="md-radiobutton__check-area"> | ||
<span className="md-radiobutton__selected-dot" /> | ||
</span> | ||
<input | ||
id={String(radioGroupId) || undefined} | ||
type="radio" | ||
value={value} | ||
checked="{true|false}" | ||
disabled="{disabled}" | ||
{...otherProps} | ||
/> | ||
<label>{label}</label> | ||
</div> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
.md-radiobutton { | ||
font-family: 'Open sans'; | ||
font-size: 16px; | ||
border: none; | ||
padding: 0; | ||
} | ||
|
||
.md-radiobutton { | ||
display: flex; | ||
align-items: center; | ||
cursor: pointer; | ||
position: relative; | ||
} | ||
|
||
.md-radiobutton:focus-within { | ||
outline: 2px solid var(--mdPrimaryColor); | ||
outline-offset: 2px; | ||
} | ||
|
||
.md-radiobutton:hover { | ||
text-decoration: underline; | ||
} | ||
|
||
.md-radiobutton input[type='radio'] { | ||
position: absolute; | ||
left: 0; | ||
opacity: 0; | ||
cursor: pointer; | ||
} | ||
|
||
.md-radiobutton__check-area { | ||
width: 1.45rem; | ||
height: 1.45rem; | ||
display: block; | ||
background-color: #fff; | ||
border: 1px solid var(--mdPrimaryColor); | ||
border-radius: 50%; | ||
margin: 0 0.5rem 0 0; | ||
box-sizing: border-box; | ||
position: relative; | ||
} | ||
|
||
.md-radiobutton:not(.md-radiobutton--disabled):focus-within .md-radiobutton__check-area, | ||
.md-radiobutton:not(.md-radiobutton--disabled):hover .md-radiobutton__check-area { | ||
background-color: var(--mdPrimaryColor20); | ||
text-decoration: underline; | ||
} | ||
|
||
.md-radiobutton__selected-dot { | ||
width: 0.6em; | ||
height: 0.6em; | ||
display: block; | ||
background-color: var(--mdPrimaryColor); | ||
border-radius: 50%; | ||
margin: 0 auto; | ||
position: absolute; | ||
left: 0; | ||
right: 0; | ||
top: 50%; | ||
transform: translateY(-50%); | ||
opacity: 0; | ||
animation: fadeIn 0.3s ease-out 1 forwards; | ||
} | ||
|
||
/* Disabled */ | ||
.md-radiobutton--disabled:hover { | ||
text-decoration: none; | ||
cursor: default; | ||
} | ||
.md-radiobutton--disabled .md-radiobutton__check-area { | ||
background-color: var(--mdGreyColor20); | ||
border-color: var(--mdGreyColor60); | ||
} | ||
.md-radiobutton--disabled .md-radiobutton__selected-dot { | ||
background-color: var(--mdGreyColor60); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import classnames from 'classnames'; | ||
import React, { useState } from 'react'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
export interface MdRadioButtonProps { | ||
checked?: boolean | undefined; | ||
label?: string; | ||
value?: any; | ||
id?: string | number; | ||
disabled?: boolean; | ||
className?: string; | ||
onChange(_e: React.ChangeEvent<HTMLInputElement>): void; | ||
onBlur?(_e: React.FocusEvent<HTMLInputElement>): void; | ||
onFocus?(_e: React.FocusEvent<HTMLInputElement>): void; | ||
[otherProps: string]: unknown; | ||
} | ||
|
||
const MdRadioButton: React.FunctionComponent<MdRadioButtonProps> = ({ | ||
id, | ||
disabled, | ||
className, | ||
value, | ||
label, | ||
checked, | ||
...otherProps | ||
}: MdRadioButtonProps) => { | ||
const radioGroupId = id || uuidv4(); | ||
const [,] = useState(false); | ||
|
||
const classNames = classnames( | ||
'md-radiobutton', | ||
{ | ||
'md-radiobutton--disabled': !!disabled, | ||
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. Consider changing this to an element property and style via 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. Evt gjøre disse tingene i ny PR så man slipper å lage ulik konvenskjon på tvers av komponenter. Eks. radiobutton bruker :disabled mens radiogroup bruker --disabled. Skal se på det. |
||
}, | ||
className, | ||
); | ||
|
||
return ( | ||
<div className={classNames}> | ||
<span className="md-radiobutton__check-area">{checked && <span className="md-radiobutton__selected-dot" />}</span> | ||
<input | ||
id={String(radioGroupId) || undefined} | ||
type="radio" | ||
value={value} | ||
checked={checked} | ||
disabled={disabled} | ||
{...otherProps} | ||
/> | ||
<label htmlFor={String(radioGroupId) || undefined}>{label && label !== '' && label}</label> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MdRadioButton; |
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.
Keep in mind
:disabled
is a selector. This works with thedisabled
prop on an element. Any reason not to use this?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.
Copy paste css from existing component. Avoiding refactoring unrelated things and follow existing structure.
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.
E.g prøver å ikke henge meg opp i måten det har blitt gjort tidligere - isåfall burde det refaktorers overalt det er relevant. Men skal se på det.