-
Notifications
You must be signed in to change notification settings - Fork 26
/
radio-option.js
124 lines (118 loc) 路 3.88 KB
/
radio-option.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import React from 'react';
import PropTypes from 'prop-types';
import { isValidElementType } from 'react-is';
import styled from '@emotion/styled';
import { css } from '@emotion/core';
import { customProperties as vars } from '@commercetools-uikit/design-system';
import { filterDataAttributes } from '@commercetools-uikit/utils';
import {
RadioOptionCheckedIcon,
RadioOptionUncheckedIcon,
} from '../../internals/icons';
import accessibleHiddenInputStyles from '../../internals/accessible-hidden-input.styles';
import { getLabelStyles, getContainerStyles } from './radio-option.styles';
const Input = styled.input`
&:focus + div > svg [id$='borderAndContent'] > [id$='border'] {
stroke: ${vars.borderColorForInputWhenFocused};
}
`;
const Option = props => {
const labelProps = props.id ? { htmlFor: props.id } : {};
return (
<label
css={getLabelStyles(props)}
role="radio"
aria-checked={props.isChecked}
onFocus={props.onFocus}
onBlur={props.onBlur}
{...labelProps}
>
<Input
css={accessibleHiddenInputStyles}
id={props.id}
name={props.name}
value={props.value}
onChange={props.isReadOnly ? undefined : props.onChange}
disabled={props.isDisabled}
checked={props.isChecked}
type="radio"
readOnly={props.isReadOnly}
aria-readonly={props.isReadOnly}
{...filterDataAttributes(props)}
/>
<div css={getContainerStyles(props)}>
{props.isChecked ? (
<RadioOptionCheckedIcon size="medium" />
) : (
<RadioOptionUncheckedIcon size="medium" />
)}
</div>
<div
css={css`
width: 100%;
margin-left: ${vars.spacingS};
font-size: 1rem;
font-family: inherit;
color: ${props.isDisabled
? vars.fontColorForInputWhenDisabled
: vars.fontColorForInput};
`}
>
{props.children}
</div>
</label>
);
};
Option.displayName = 'RadioOption';
Option.propTypes = {
// Direct props
value: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
children: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
PropTypes.func,
]).isRequired,
components: PropTypes.shape({
wrapper: (props, propName) => {
if (props[propName]) {
if (typeof props[propName] !== 'function') {
return new Error(
`Invalid prop 'components.wrapper' supplied to 'RadioInput.Option': the prop is not a function`
);
}
if (props[propName].length !== 1) {
return new Error(
`Invalid prop 'components.wrapper' supplied to 'RadioInput.Option': the supplied function should expect one argument`
);
}
if (isValidElementType(props[propName](React.Fragment))) {
return new Error(
`Invalid prop 'components.wrapper' supplied to 'RadioInput.Option': the function supplied should return a valid react element`
);
}
}
return null;
},
}),
// Injected props from the parent Group component
id: PropTypes.string,
name: PropTypes.string,
isChecked: PropTypes.bool,
isDisabled: PropTypes.bool,
isReadOnly: PropTypes.bool,
hasError: PropTypes.bool,
hasWarning: PropTypes.bool,
onChange: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
// This prop forces Radio.Option to be rendered in a hovered state (though isDisabled takes
// precedence over that). We need that to address a use-case when hovering is comming
// from somewhere up the hierarchy. There is no need to touch this prop in case
// all you need is a general highlighting on hover of Radio.Option body, which is solved
// by a corresponding :hover selector in the syles of this component.
isHovered: PropTypes.bool,
};
Option.defaultProps = {
components: {},
};
export default Option;