1- import React , { useRef , useEffect } from 'react'
2- import styled from 'styled-components'
3- import { addFocus } from '../../utils/mixins'
1+ import React , { useRef , useEffect } from "react" ;
2+ import { addFocus } from "../../utils/mixins" ;
3+ import styled from "../../utils/styled" ;
4+ import { Story , IStory } from "../../utils/story" ;
45
5- const Container = styled . div `
6- input:focus + ${ Label } ::before, input:active + ${ Label } ::before {
7- ${ addFocus }
8- }
9- `
10-
11- const Label = styled . label `
6+ const Label = styled . label < { checked : boolean } > `
127 margin-left: 0.5rem;
138 font-size: 1.25rem;
149 position: relative;
1510
1611 &::before {
17- content: '' ;
12+ content: "" ;
1813 display: inline-block;
1914 height: 12px;
2015 width: 12px;
@@ -23,10 +18,15 @@ const Label = styled.label`
2318 position: absolute;
2419 left: -19px;
2520 top: 4px;
26- background-color: ${ props =>
27- props . checked ? props . theme . primary . main : '' } ;
21+ background-color: ${ props => ( props . checked ? props . theme . primary . main : "" ) } ;
2822 }
29- `
23+ ` ;
24+
25+ const Container = styled . div `
26+ input:focus + ${ Label } ::before, input:active + ${ Label } ::before {
27+ ${ addFocus }
28+ }
29+ ` ;
3030
3131const Input = styled . input `
3232 font-size: 1.25rem;
@@ -35,39 +35,50 @@ const Input = styled.input`
3535 &:active {
3636 outline: none;
3737 }
38- `
38+ ` ;
3939
40- const InputRadio = ( { currentValue, input, onChange, transition } ) => {
41- const inputRadioRef = useRef ( null )
40+ export interface InputRadioProps {
41+ currentValue : string ;
42+ input : IStory [ "INPUT" ] ;
43+ onChange : ( e : React . ChangeEvent < HTMLInputElement > ) => void ;
44+ transition : ( ) => void ;
45+ }
46+
47+ const InputRadio : React . FC < InputRadioProps > = ( { currentValue, input, onChange, transition } ) => {
48+ const inputRadioRef = useRef < HTMLInputElement > ( null ) ;
4249 useEffect ( ( ) => {
43- inputRadioRef . current . focus ( )
44- } , [ inputRadioRef ] )
50+ if ( inputRadioRef . current ) {
51+ inputRadioRef . current . focus ( ) ;
52+ }
53+ } , [ inputRadioRef ] ) ;
4554 return (
4655 < React . Fragment >
47- { input [ 'OPTIONS' ] . map ( ( option , index ) => (
48- < Container key = { option } >
49- < Input
50- ref = { index === 0 ? inputRadioRef : null }
51- tabIndex = { index === 0 ? '1' : '' }
52- value = { option }
53- checked = { option === currentValue }
54- onChange = { onChange }
55- onKeyUp = { e => {
56- if ( ( e . keyCode === 13 || e . keyCode === 39 ) && currentValue ) {
57- transition ( )
58- }
59- } }
60- type = "radio"
61- name = { option }
62- id = { option }
63- />
64- < Label checked = { option === currentValue } htmlFor = { option } >
65- { option }
66- </ Label >
67- </ Container >
68- ) ) }
56+ { input &&
57+ input . OPTIONS &&
58+ input . OPTIONS . map ( ( option , index ) => (
59+ < Container key = { option } >
60+ < Input
61+ ref = { index === 0 ? inputRadioRef : null }
62+ tabIndex = { index === 0 ? 1 : 0 }
63+ value = { option }
64+ checked = { option === currentValue }
65+ onChange = { onChange }
66+ onKeyUp = { e => {
67+ if ( ( e . keyCode === 13 || e . keyCode === 39 ) && currentValue ) {
68+ transition ( ) ;
69+ }
70+ } }
71+ type = "radio"
72+ name = { option }
73+ id = { option }
74+ />
75+ < Label checked = { option === currentValue } htmlFor = { option } >
76+ { option }
77+ </ Label >
78+ </ Container >
79+ ) ) }
6980 </ React . Fragment >
70- )
71- }
81+ ) ;
82+ } ;
7283
73- export default InputRadio
84+ export default InputRadio ;
0 commit comments