/
radio.tsx
148 lines (131 loc) · 3.57 KB
/
radio.tsx
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
chakra,
forwardRef,
layoutPropNames,
omitThemingProps,
SystemProps,
SystemStyleObject,
ThemingProps,
useMultiStyleConfig,
HTMLChakraProps,
} from "@chakra-ui/system"
import { callAll, split, __DEV__ } from "@chakra-ui/utils"
import * as React from "react"
import { useRadioGroupContext } from "./radio-group"
import { useRadio, UseRadioProps } from "./use-radio"
type Omitted = "onChange" | "defaultChecked" | "checked"
interface BaseControlProps extends Omit<HTMLChakraProps<"div">, Omitted> {}
export interface RadioProps
extends UseRadioProps,
ThemingProps<"Radio">,
BaseControlProps {
/**
* The spacing between the checkbox and its label text
* @default 0.5rem
* @type SystemProps["marginLeft"]
*/
spacing?: SystemProps["marginLeft"]
/**
* If `true`, the radio will occupy the full width of its parent container
*
* @deprecated
* This component defaults to 100% width,
* please use the props `maxWidth` or `width` to configure
*/
isFullWidth?: boolean
/**
* Additional props to be forwarded to the `input` element
*/
inputProps?: React.InputHTMLAttributes<HTMLInputElement>
}
/**
* Radio component is used in forms when a user needs to select a single value from
* several options.
*
* @see Docs https://chakra-ui.com/radio
*/
export const Radio = forwardRef<RadioProps, "input">((props, ref) => {
const group = useRadioGroupContext()
const { onChange: onChangeProp, value: valueProp } = props
const styles = useMultiStyleConfig("Radio", { ...group, ...props })
const ownProps = omitThemingProps(props)
const {
spacing = "0.5rem",
children,
isFullWidth,
isDisabled = group?.isDisabled,
isFocusable = group?.isFocusable,
inputProps: htmlInputProps,
...rest
} = ownProps
let isChecked = props.isChecked
if (group?.value != null && valueProp != null) {
isChecked = group.value === valueProp
}
let onChange = onChangeProp
if (group?.onChange && valueProp != null) {
onChange = callAll(group.onChange, onChangeProp)
}
const name = props?.name ?? group?.name
const {
getInputProps,
getCheckboxProps,
getLabelProps,
getRootProps,
htmlProps,
} = useRadio({
...rest,
isChecked,
isFocusable,
isDisabled,
onChange,
name,
})
const [layoutProps, otherProps] = split(htmlProps, layoutPropNames as any)
const checkboxProps = getCheckboxProps(otherProps)
const inputProps = getInputProps(htmlInputProps, ref)
const labelProps = getLabelProps()
const rootProps = Object.assign({}, layoutProps, getRootProps())
const rootStyles = {
width: isFullWidth ? "full" : undefined,
display: "inline-flex",
alignItems: "center",
verticalAlign: "top",
cursor: "pointer",
...styles.container,
}
const checkboxStyles = {
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
flexShrink: 0,
...styles.control,
}
const labelStyles: SystemStyleObject = {
userSelect: "none",
marginStart: spacing,
...styles.label,
}
return (
<chakra.label className="chakra-radio" {...rootProps} __css={rootStyles}>
<input className="chakra-radio__input" {...inputProps} />
<chakra.span
className="chakra-radio__control"
{...checkboxProps}
__css={checkboxStyles}
/>
{children && (
<chakra.span
className="chakra-radio__label"
{...labelProps}
__css={labelStyles}
>
{children}
</chakra.span>
)}
</chakra.label>
)
})
if (__DEV__) {
Radio.displayName = "Radio"
}