-
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.
* Add MdRadioButton component * Clean up stories that were using an older way of creating markdown documentation
- Loading branch information
1 parent
dd50a8a
commit 1fa7e3d
Showing
11 changed files
with
300 additions
and
29 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
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> | ||
``` |
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,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); | ||
} |
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,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, | ||
}, | ||
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; |
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
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
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.