@bento/radio 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.mdx +108 -0
- package/dist/index.cjs +96 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +57 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.js +89 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -0
- package/src/index.tsx +2 -0
- package/src/radio-group-state.tsx +4 -0
- package/src/radio-group.tsx +89 -0
- package/src/radio.tsx +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 GoDaddy Operating Company, LLC.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.mdx
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Meta, ArgTypes, Story, Controls, Source } from '@storybook/addon-docs/blocks';
|
|
2
|
+
import * as Stories from './radio.stories.tsx';
|
|
3
|
+
import Uncontrolled from './examples/uncontrolled.tsx?raw';
|
|
4
|
+
import Controlled from './examples/controlled.tsx?raw';
|
|
5
|
+
import SingleRadio from './examples/single-radio.tsx?raw';
|
|
6
|
+
|
|
7
|
+
<Meta of={Stories} name="Overview" />
|
|
8
|
+
|
|
9
|
+
# Radio
|
|
10
|
+
|
|
11
|
+
The `@bento/radio` package provides accessible, customizable radio controls. It exports the **RadioGroup** and **Radio** primitives, enabling you to build groups of radio options with consistent keyboard navigation, focus management, and ARIA support.
|
|
12
|
+
|
|
13
|
+
The `RadioGroup` allows a user to select a single item from a list of `Radio` components.
|
|
14
|
+
|
|
15
|
+
The `Radio` is a single radio option that can be selected by the user.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install --save @bento/radio
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Props
|
|
24
|
+
|
|
25
|
+
The `@bento/radio` package exports the `RadioGroup` and `Radio` primitives:
|
|
26
|
+
|
|
27
|
+
```jsx
|
|
28
|
+
import { RadioGroup, Radio } from '@bento/radio';
|
|
29
|
+
|
|
30
|
+
<RadioGroup label="Favorite fruit">
|
|
31
|
+
<Radio value="apple">Apple</Radio>
|
|
32
|
+
<Radio value="banana">Banana</Radio>
|
|
33
|
+
<Radio value="orange">Orange</Radio>
|
|
34
|
+
</RadioGroup>;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The following properties are available to be used on the `RadioGroup` and `Radio` primitives:
|
|
38
|
+
|
|
39
|
+
### RadioGroup
|
|
40
|
+
|
|
41
|
+
<ArgTypes of={Stories.RadioGroupProps} />
|
|
42
|
+
|
|
43
|
+
### Radio
|
|
44
|
+
|
|
45
|
+
<ArgTypes of={Stories.RadioProps} />
|
|
46
|
+
|
|
47
|
+
## Data Attributes, Slot Map and Props
|
|
48
|
+
|
|
49
|
+
The data attributes, slot map and props can be used to style and customize the `RadioGroup` and `Radio` primitives.
|
|
50
|
+
|
|
51
|
+
### `RadioGroup` Data Attributes
|
|
52
|
+
|
|
53
|
+
| Attribute | Description |
|
|
54
|
+
| --------------- | ------------------------------- |
|
|
55
|
+
| `data-disabled` | Indicates the group is disabled |
|
|
56
|
+
| `data-readonly` | Indicates the group is readonly |
|
|
57
|
+
| `data-required` | Indicates the group is required |
|
|
58
|
+
| `data-invalid` | Indicates the group is invalid |
|
|
59
|
+
|
|
60
|
+
### `Radio` Data Attributes
|
|
61
|
+
|
|
62
|
+
| Attribute | Description |
|
|
63
|
+
| -------------------- | ------------------------------------ |
|
|
64
|
+
| `data-pressed` | Indicates the radio is being pressed |
|
|
65
|
+
| `data-hovered` | Indicates the radio is hovered |
|
|
66
|
+
| `data-focused` | Indicates the radio has focus |
|
|
67
|
+
| `data-focus-visible` | Indicates focus should be visible |
|
|
68
|
+
| `data-selected` | Indicates the radio is selected |
|
|
69
|
+
| `data-disabled` | Indicates the radio is disabled |
|
|
70
|
+
| `data-readonly` | Indicates the radio is readonly |
|
|
71
|
+
| `data-invalid` | Indicates the radio is invalid |
|
|
72
|
+
| `data-required` | Indicates the radio is required |
|
|
73
|
+
|
|
74
|
+
### `RadioGroup` Slot Map
|
|
75
|
+
|
|
76
|
+
| Slot Name | Description |
|
|
77
|
+
| ------------------- | ---------------------------- |
|
|
78
|
+
| `group.label` | Label for radiogroup |
|
|
79
|
+
| `group.description` | Description for radiogroup |
|
|
80
|
+
| `group.error` | Error message for radiogroup |
|
|
81
|
+
|
|
82
|
+
### `Radio` Slot Map
|
|
83
|
+
|
|
84
|
+
| Slot Name | Description |
|
|
85
|
+
| ------------------------ | ------------------------ |
|
|
86
|
+
| `control.icon-checked` | Icon for checked radio |
|
|
87
|
+
| `control.icon-unchecked` | Icon for unchecked radio |
|
|
88
|
+
| `control.label` | Text for radio |
|
|
89
|
+
|
|
90
|
+
## Examples
|
|
91
|
+
|
|
92
|
+
### Uncontrolled
|
|
93
|
+
|
|
94
|
+
The `RadioGroup` is uncontrolled by default.
|
|
95
|
+
|
|
96
|
+
<Source language="tsx" code={Uncontrolled} />
|
|
97
|
+
|
|
98
|
+
### Controlled
|
|
99
|
+
|
|
100
|
+
The `RadioGroup` can be controlled by passing a `value` and `onChange` prop.
|
|
101
|
+
|
|
102
|
+
<Source language="tsx" code={Controlled} />
|
|
103
|
+
|
|
104
|
+
### Single Radio
|
|
105
|
+
|
|
106
|
+
Even if you only have one radio option, it must always be placed inside a `RadioGroup`.
|
|
107
|
+
|
|
108
|
+
<Source language="tsx" code={SingleRadio} />
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React2 = require('react');
|
|
4
|
+
var control = require('@bento/control');
|
|
5
|
+
var useDataAttributes = require('@bento/use-data-attributes');
|
|
6
|
+
var icon = require('@bento/icon');
|
|
7
|
+
var slots = require('@bento/slots');
|
|
8
|
+
var useProps = require('@bento/use-props');
|
|
9
|
+
var utils = require('@react-aria/utils');
|
|
10
|
+
var reactAria = require('react-aria');
|
|
11
|
+
var reactStately = require('react-stately');
|
|
12
|
+
|
|
13
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
+
|
|
15
|
+
var React2__default = /*#__PURE__*/_interopDefault(React2);
|
|
16
|
+
|
|
17
|
+
// src/radio.tsx
|
|
18
|
+
var RadioGroupStateContext = React2__default.default.createContext(null);
|
|
19
|
+
|
|
20
|
+
// src/radio.tsx
|
|
21
|
+
var Radio = slots.withSlots("BentoRadio", function Radio2(args) {
|
|
22
|
+
const { props, apply } = useProps.useProps(args);
|
|
23
|
+
const state = React2__default.default.useContext(RadioGroupStateContext);
|
|
24
|
+
const ref = React2__default.default.useRef(null);
|
|
25
|
+
const inputRef = utils.useObjectRef(React2.useMemo(() => utils.mergeRefs(ref, props.inputRef), [ref, props.inputRef]));
|
|
26
|
+
const { isFocused, isFocusVisible, focusProps } = reactAria.useFocusRing();
|
|
27
|
+
const { inputProps, labelProps, isSelected, isPressed } = reactAria.useRadio(props, state, inputRef);
|
|
28
|
+
const interactionDisabled = props.isDisabled || state.isReadOnly;
|
|
29
|
+
const { hoverProps, isHovered } = reactAria.useHover({
|
|
30
|
+
...props,
|
|
31
|
+
isDisabled: interactionDisabled
|
|
32
|
+
});
|
|
33
|
+
return /* @__PURE__ */ React2__default.default.createElement(
|
|
34
|
+
control.Control,
|
|
35
|
+
{
|
|
36
|
+
slot: "control",
|
|
37
|
+
label: props.children,
|
|
38
|
+
labelProps: utils.mergeProps(labelProps, hoverProps),
|
|
39
|
+
inputRef,
|
|
40
|
+
inputProps: utils.mergeProps(inputProps, focusProps),
|
|
41
|
+
...apply(props, ["isDisabled", "value", "autoFocus"]),
|
|
42
|
+
...useDataAttributes.useDataAttributes({
|
|
43
|
+
selected: isSelected,
|
|
44
|
+
pressed: isPressed,
|
|
45
|
+
hovered: isHovered,
|
|
46
|
+
focused: isFocused,
|
|
47
|
+
focusVisible: isFocusVisible,
|
|
48
|
+
disabled: props.isDisabled,
|
|
49
|
+
readonly: state.isReadOnly,
|
|
50
|
+
invalid: state.isInvalid,
|
|
51
|
+
required: state.isRequired
|
|
52
|
+
})
|
|
53
|
+
},
|
|
54
|
+
isSelected ? /* @__PURE__ */ React2__default.default.createElement(icon.Icon, { slot: "icon-checked", icon: "radioChecked" }, /* @__PURE__ */ React2__default.default.createElement("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", "aria-hidden": "true" }, /* @__PURE__ */ React2__default.default.createElement("circle", { cx: 12, cy: 12, r: 8 - 6 / 2, fill: "none", stroke: "orange", strokeWidth: 6 }))) : /* @__PURE__ */ React2__default.default.createElement(icon.Icon, { slot: "icon-unchecked", icon: "radioUnchecked" }, /* @__PURE__ */ React2__default.default.createElement("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", "aria-hidden": "true" }, /* @__PURE__ */ React2__default.default.createElement("circle", { cx: 12, cy: 12, r: 8, fill: "none", stroke: "gray", strokeWidth: 2 })))
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
var RadioGroup = slots.withSlots("BentoRadioGroup", function RadioGroup2(args) {
|
|
58
|
+
const { errorMessage, ...restArgs } = args;
|
|
59
|
+
const { props, apply } = useProps.useProps(restArgs);
|
|
60
|
+
const state = reactStately.useRadioGroupState(props);
|
|
61
|
+
const { radioGroupProps, labelProps, descriptionProps, errorMessageProps, ...validationResult } = reactAria.useRadioGroup(
|
|
62
|
+
props,
|
|
63
|
+
state
|
|
64
|
+
);
|
|
65
|
+
const displayedErrorMessage = React2.useMemo(
|
|
66
|
+
function getErrorMessage() {
|
|
67
|
+
return typeof errorMessage === "function" ? errorMessage(validationResult) : errorMessage || validationResult.validationErrors.join(", ");
|
|
68
|
+
},
|
|
69
|
+
[errorMessage, validationResult]
|
|
70
|
+
);
|
|
71
|
+
return /* @__PURE__ */ React2__default.default.createElement(
|
|
72
|
+
control.ControlGroup,
|
|
73
|
+
{
|
|
74
|
+
slot: "group",
|
|
75
|
+
labelProps,
|
|
76
|
+
descriptionProps,
|
|
77
|
+
errorMessage: displayedErrorMessage,
|
|
78
|
+
errorMessageProps,
|
|
79
|
+
...radioGroupProps,
|
|
80
|
+
...apply(props, ["isInvalid", "isDisabled", "isReadOnly", "isRequired", "validationBehavior"]),
|
|
81
|
+
...useDataAttributes.useDataAttributes({
|
|
82
|
+
orientation: props.orientation || "vertical",
|
|
83
|
+
invalid: state.isInvalid,
|
|
84
|
+
disabled: state.isDisabled,
|
|
85
|
+
readonly: state.isReadOnly,
|
|
86
|
+
required: state.isRequired
|
|
87
|
+
})
|
|
88
|
+
},
|
|
89
|
+
/* @__PURE__ */ React2__default.default.createElement(RadioGroupStateContext.Provider, { value: state }, props.children)
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
exports.Radio = Radio;
|
|
94
|
+
exports.RadioGroup = RadioGroup;
|
|
95
|
+
//# sourceMappingURL=index.cjs.map
|
|
96
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/radio-group-state.tsx","../src/radio.tsx","../src/radio-group.tsx"],"names":["React","withSlots","Radio","useProps","useObjectRef","useMemo","mergeRefs","useFocusRing","useRadio","useHover","Control","mergeProps","useDataAttributes","Icon","RadioGroup","useRadioGroupState","useRadioGroup","ControlGroup"],"mappings":";;;;;;;;;;;;;;;;;AAGO,IAAM,sBAAA,GAAyBA,uBAAA,CAAM,aAAA,CAAsC,IAAI,CAAA;;;AC8B/E,IAAM,KAAA,GAAQC,eAAA,CAAU,YAAA,EAAc,SAASC,OAAM,IAAA,EAAkB;AAC5E,EAAA,MAAM,EAAE,KAAA,EAAO,KAAA,EAAM,GAAIC,kBAAS,IAAI,CAAA;AACtC,EAAA,MAAM,KAAA,GAAQH,uBAAAA,CAAM,UAAA,CAAW,sBAAsB,CAAA;AACrD,EAAA,MAAM,GAAA,GAAMA,uBAAAA,CAAM,MAAA,CAAyB,IAAI,CAAA;AAC/C,EAAA,MAAM,QAAA,GAAWI,kBAAA,CAAaC,cAAA,CAAQ,MAAMC,gBAAU,GAAA,EAAK,KAAA,CAAM,QAAQ,CAAA,EAAG,CAAC,GAAA,EAAK,KAAA,CAAM,QAAQ,CAAC,CAAC,CAAA;AAClG,EAAA,MAAM,EAAE,SAAA,EAAW,cAAA,EAAgB,UAAA,KAAeC,sBAAA,EAAa;AAC/D,EAAA,MAAM,EAAE,YAAY,UAAA,EAAY,UAAA,EAAY,WAAU,GAAIC,kBAAA,CAAS,KAAA,EAAqB,KAAA,EAAO,QAAQ,CAAA;AACvG,EAAA,MAAM,mBAAA,GAAsB,KAAA,CAAM,UAAA,IAAc,KAAA,CAAM,UAAA;AACtD,EAAA,MAAM,EAAE,UAAA,EAAY,SAAA,EAAU,GAAIC,kBAAA,CAAS;AAAA,IACzC,GAAG,KAAA;AAAA,IACH,UAAA,EAAY;AAAA,GACb,C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A;AAAA,MACC,GAAG,eAAA;AAAA,MACH,GAAG,MAAM,KAAA,EAAO,CAAC,aAAa,YAAA,EAAc,YAAA,EAAc,YAAA,EAAc,oBAAoB,CAAC,CAAA;AAAA,MAC7F,GAAGL,mCAAAA,CAAkB;AAAA,QACpB,WAAA,EAAa,MAAM,WAAA,IAAe,UAAA;AAAA,QAClC,SAAS,KAAA,CAAM,SAAA;AAAA,QACf,UAAU,KAAA,CAAM,UAAA;AAAA,QAChB,UAAU,KAAA,CAAM,UAAA;AAAA,QAChB,UAAU,KAAA,CAAM;AAAA,OACjB;AAAA,KAAA;AAAA,oBAEDZ,wBAAA,aAAA,CAAC,sBAAA,CAAuB,UAAvB,EAAgC,KAAA,EAAO,KAAA,EAAA,EAAQ,KAAA,CAAM,QAAS;AAAA,GACjE;AAEJ,CAAC","file":"index.cjs","sourcesContent":["import React from 'react';\nimport { RadioGroupState } from 'react-stately';\n\nexport const RadioGroupStateContext = React.createContext<RadioGroupState | null>(null);\n","import React, { useMemo } from 'react';\nimport { Control, type ControlProps } from '@bento/control';\nimport { useDataAttributes } from '@bento/use-data-attributes';\nimport { Icon } from '@bento/icon';\nimport { withSlots } from '@bento/slots';\nimport { useProps } from '@bento/use-props';\nimport { mergeProps, mergeRefs, useObjectRef } from '@react-aria/utils';\nimport { useFocusRing, useHover, useRadio, type AriaRadioProps } from 'react-aria';\nimport { RadioGroupStateContext } from './radio-group-state';\n\nexport interface RadioProps extends AriaRadioProps, Partial<Omit<ControlProps, keyof AriaRadioProps>> {\n /** The value of the radio button, used when submitting an HTML form. */\n value: string;\n\n /** A ref for the HTML input element. */\n inputRef?: React.Ref<HTMLInputElement>;\n\n /** The label for the Radio. Accepts any renderable node. */\n children?: React.ReactNode;\n\n /**\n * Whether the radio button is disabled or not. Shows that a selection exists,\n * but is not available in that circumstance.\n */\n isDisabled?: boolean;\n\n /** Whether the element should receive focus on render. */\n autoFocus?: boolean;\n}\n\n/**\n * The `Radio` is a single radio option that can be selected by the user.\n */\nexport const Radio = withSlots('BentoRadio', function Radio(args: RadioProps) {\n const { props, apply } = useProps(args);\n const state = React.useContext(RadioGroupStateContext)!;\n const ref = React.useRef<HTMLInputElement>(null);\n const inputRef = useObjectRef(useMemo(() => mergeRefs(ref, props.inputRef), [ref, props.inputRef]));\n const { isFocused, isFocusVisible, focusProps } = useFocusRing();\n const { inputProps, labelProps, isSelected, isPressed } = useRadio(props as RadioProps, state, inputRef);\n const interactionDisabled = props.isDisabled || state.isReadOnly;\n const { hoverProps, isHovered } = useHover({\n ...props,\n isDisabled: interactionDisabled\n });\n\n return (\n <Control\n slot=\"control\"\n label={props.children}\n labelProps={mergeProps(labelProps, hoverProps)}\n inputRef={inputRef}\n inputProps={mergeProps(inputProps, focusProps)}\n {...apply(props, ['isDisabled', 'value', 'autoFocus'])}\n {...useDataAttributes({\n selected: isSelected,\n pressed: isPressed,\n hovered: isHovered,\n focused: isFocused,\n focusVisible: isFocusVisible,\n disabled: props.isDisabled,\n readonly: state.isReadOnly,\n invalid: state.isInvalid,\n required: state.isRequired\n })}\n >\n {isSelected ? (\n <Icon slot=\"icon-checked\" icon=\"radioChecked\">\n <svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\" aria-hidden=\"true\">\n <circle cx={12} cy={12} r={8 - 6 / 2} fill=\"none\" stroke=\"orange\" strokeWidth={6} />\n </svg>\n </Icon>\n ) : (\n <Icon slot=\"icon-unchecked\" icon=\"radioUnchecked\">\n <svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\" aria-hidden=\"true\">\n <circle cx={12} cy={12} r={8} fill=\"none\" stroke=\"gray\" strokeWidth={2} />\n </svg>\n </Icon>\n )}\n </Control>\n );\n});\n","import React, { useMemo } from 'react';\nimport { ControlGroup, ControlGroupProps } from '@bento/control';\nimport { withSlots } from '@bento/slots';\nimport { useProps } from '@bento/use-props';\nimport { useRadioGroup, type AriaRadioGroupProps } from 'react-aria';\nimport { useRadioGroupState } from 'react-stately';\nimport { RadioGroupStateContext } from './radio-group-state';\nimport { useDataAttributes } from '@bento/use-data-attributes';\nimport { type ValidationResult } from '@react-types/shared';\n\nexport interface RadioGroupProps\n extends AriaRadioGroupProps,\n Partial<Omit<ControlGroupProps, keyof AriaRadioGroupProps>> {\n /** The current value (controlled). */\n value?: string;\n\n /** The default value (uncontrolled). */\n defaultValue?: string;\n\n /** Whether the input is disabled. */\n isDisabled?: boolean;\n\n /** Whether the input can be selected but not changed by the user. */\n isReadOnly?: boolean;\n\n /** Whether user input is required on the input before form submission. */\n isRequired?: boolean;\n\n /** Whether the input value is invalid. */\n isInvalid?: boolean;\n\n /** The name of the input element, used when submitting an HTML form. */\n name?: string;\n\n /**\n * The <form> element to associate the input with.\n * The value of this attribute must be the id of a <form> in the same document.\n */\n form?: string;\n\n /** Radio children. */\n children: React.ReactNode;\n\n /** Error message for the radio group. */\n errorMessage?: React.ReactNode | ((val: ValidationResult) => React.ReactNode);\n}\n\n/**\n * The `RadioGroup` allows a user to select a single item from a list of `Radio` components.\n */\nexport const RadioGroup = withSlots('BentoRadioGroup', function RadioGroup(args: RadioGroupProps) {\n const { errorMessage, ...restArgs } = args;\n const { props, apply } = useProps(restArgs);\n const state = useRadioGroupState(props);\n const { radioGroupProps, labelProps, descriptionProps, errorMessageProps, ...validationResult } = useRadioGroup(\n props,\n state\n );\n\n const displayedErrorMessage = useMemo(\n function getErrorMessage() {\n return typeof errorMessage === 'function'\n ? errorMessage(validationResult)\n : errorMessage || validationResult.validationErrors.join(', ');\n },\n [errorMessage, validationResult]\n );\n\n return (\n <ControlGroup\n slot=\"group\"\n labelProps={labelProps}\n descriptionProps={descriptionProps}\n errorMessage={displayedErrorMessage}\n errorMessageProps={errorMessageProps}\n {...radioGroupProps}\n {...apply(props, ['isInvalid', 'isDisabled', 'isReadOnly', 'isRequired', 'validationBehavior'])}\n {...useDataAttributes({\n orientation: props.orientation || 'vertical',\n invalid: state.isInvalid,\n disabled: state.isDisabled,\n readonly: state.isReadOnly,\n required: state.isRequired\n })}\n >\n <RadioGroupStateContext.Provider value={state}>{props.children}</RadioGroupStateContext.Provider>\n </ControlGroup>\n );\n});\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as _bento_slots from '@bento/slots';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { ControlProps, ControlGroupProps } from '@bento/control';
|
|
4
|
+
import { AriaRadioProps, AriaRadioGroupProps } from 'react-aria';
|
|
5
|
+
import { ValidationResult } from '@react-types/shared';
|
|
6
|
+
|
|
7
|
+
interface RadioProps extends AriaRadioProps, Partial<Omit<ControlProps, keyof AriaRadioProps>> {
|
|
8
|
+
/** The value of the radio button, used when submitting an HTML form. */
|
|
9
|
+
value: string;
|
|
10
|
+
/** A ref for the HTML input element. */
|
|
11
|
+
inputRef?: React.Ref<HTMLInputElement>;
|
|
12
|
+
/** The label for the Radio. Accepts any renderable node. */
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* Whether the radio button is disabled or not. Shows that a selection exists,
|
|
16
|
+
* but is not available in that circumstance.
|
|
17
|
+
*/
|
|
18
|
+
isDisabled?: boolean;
|
|
19
|
+
/** Whether the element should receive focus on render. */
|
|
20
|
+
autoFocus?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The `Radio` is a single radio option that can be selected by the user.
|
|
24
|
+
*/
|
|
25
|
+
declare const Radio: React.NamedExoticComponent<RadioProps & _bento_slots.Slots>;
|
|
26
|
+
|
|
27
|
+
interface RadioGroupProps extends AriaRadioGroupProps, Partial<Omit<ControlGroupProps, keyof AriaRadioGroupProps>> {
|
|
28
|
+
/** The current value (controlled). */
|
|
29
|
+
value?: string;
|
|
30
|
+
/** The default value (uncontrolled). */
|
|
31
|
+
defaultValue?: string;
|
|
32
|
+
/** Whether the input is disabled. */
|
|
33
|
+
isDisabled?: boolean;
|
|
34
|
+
/** Whether the input can be selected but not changed by the user. */
|
|
35
|
+
isReadOnly?: boolean;
|
|
36
|
+
/** Whether user input is required on the input before form submission. */
|
|
37
|
+
isRequired?: boolean;
|
|
38
|
+
/** Whether the input value is invalid. */
|
|
39
|
+
isInvalid?: boolean;
|
|
40
|
+
/** The name of the input element, used when submitting an HTML form. */
|
|
41
|
+
name?: string;
|
|
42
|
+
/**
|
|
43
|
+
* The <form> element to associate the input with.
|
|
44
|
+
* The value of this attribute must be the id of a <form> in the same document.
|
|
45
|
+
*/
|
|
46
|
+
form?: string;
|
|
47
|
+
/** Radio children. */
|
|
48
|
+
children: React.ReactNode;
|
|
49
|
+
/** Error message for the radio group. */
|
|
50
|
+
errorMessage?: React.ReactNode | ((val: ValidationResult) => React.ReactNode);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The `RadioGroup` allows a user to select a single item from a list of `Radio` components.
|
|
54
|
+
*/
|
|
55
|
+
declare const RadioGroup: React.NamedExoticComponent<RadioGroupProps & _bento_slots.Slots>;
|
|
56
|
+
|
|
57
|
+
export { Radio, RadioGroup, type RadioGroupProps, type RadioProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as _bento_slots from '@bento/slots';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { ControlProps, ControlGroupProps } from '@bento/control';
|
|
4
|
+
import { AriaRadioProps, AriaRadioGroupProps } from 'react-aria';
|
|
5
|
+
import { ValidationResult } from '@react-types/shared';
|
|
6
|
+
|
|
7
|
+
interface RadioProps extends AriaRadioProps, Partial<Omit<ControlProps, keyof AriaRadioProps>> {
|
|
8
|
+
/** The value of the radio button, used when submitting an HTML form. */
|
|
9
|
+
value: string;
|
|
10
|
+
/** A ref for the HTML input element. */
|
|
11
|
+
inputRef?: React.Ref<HTMLInputElement>;
|
|
12
|
+
/** The label for the Radio. Accepts any renderable node. */
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* Whether the radio button is disabled or not. Shows that a selection exists,
|
|
16
|
+
* but is not available in that circumstance.
|
|
17
|
+
*/
|
|
18
|
+
isDisabled?: boolean;
|
|
19
|
+
/** Whether the element should receive focus on render. */
|
|
20
|
+
autoFocus?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The `Radio` is a single radio option that can be selected by the user.
|
|
24
|
+
*/
|
|
25
|
+
declare const Radio: React.NamedExoticComponent<RadioProps & _bento_slots.Slots>;
|
|
26
|
+
|
|
27
|
+
interface RadioGroupProps extends AriaRadioGroupProps, Partial<Omit<ControlGroupProps, keyof AriaRadioGroupProps>> {
|
|
28
|
+
/** The current value (controlled). */
|
|
29
|
+
value?: string;
|
|
30
|
+
/** The default value (uncontrolled). */
|
|
31
|
+
defaultValue?: string;
|
|
32
|
+
/** Whether the input is disabled. */
|
|
33
|
+
isDisabled?: boolean;
|
|
34
|
+
/** Whether the input can be selected but not changed by the user. */
|
|
35
|
+
isReadOnly?: boolean;
|
|
36
|
+
/** Whether user input is required on the input before form submission. */
|
|
37
|
+
isRequired?: boolean;
|
|
38
|
+
/** Whether the input value is invalid. */
|
|
39
|
+
isInvalid?: boolean;
|
|
40
|
+
/** The name of the input element, used when submitting an HTML form. */
|
|
41
|
+
name?: string;
|
|
42
|
+
/**
|
|
43
|
+
* The <form> element to associate the input with.
|
|
44
|
+
* The value of this attribute must be the id of a <form> in the same document.
|
|
45
|
+
*/
|
|
46
|
+
form?: string;
|
|
47
|
+
/** Radio children. */
|
|
48
|
+
children: React.ReactNode;
|
|
49
|
+
/** Error message for the radio group. */
|
|
50
|
+
errorMessage?: React.ReactNode | ((val: ValidationResult) => React.ReactNode);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The `RadioGroup` allows a user to select a single item from a list of `Radio` components.
|
|
54
|
+
*/
|
|
55
|
+
declare const RadioGroup: React.NamedExoticComponent<RadioGroupProps & _bento_slots.Slots>;
|
|
56
|
+
|
|
57
|
+
export { Radio, RadioGroup, type RadioGroupProps, type RadioProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import React2, { useMemo } from 'react';
|
|
2
|
+
import { Control, ControlGroup } from '@bento/control';
|
|
3
|
+
import { useDataAttributes } from '@bento/use-data-attributes';
|
|
4
|
+
import { Icon } from '@bento/icon';
|
|
5
|
+
import { withSlots } from '@bento/slots';
|
|
6
|
+
import { useProps } from '@bento/use-props';
|
|
7
|
+
import { useObjectRef, mergeRefs, mergeProps } from '@react-aria/utils';
|
|
8
|
+
import { useFocusRing, useRadio, useHover, useRadioGroup } from 'react-aria';
|
|
9
|
+
import { useRadioGroupState } from 'react-stately';
|
|
10
|
+
|
|
11
|
+
// src/radio.tsx
|
|
12
|
+
var RadioGroupStateContext = React2.createContext(null);
|
|
13
|
+
|
|
14
|
+
// src/radio.tsx
|
|
15
|
+
var Radio = withSlots("BentoRadio", function Radio2(args) {
|
|
16
|
+
const { props, apply } = useProps(args);
|
|
17
|
+
const state = React2.useContext(RadioGroupStateContext);
|
|
18
|
+
const ref = React2.useRef(null);
|
|
19
|
+
const inputRef = useObjectRef(useMemo(() => mergeRefs(ref, props.inputRef), [ref, props.inputRef]));
|
|
20
|
+
const { isFocused, isFocusVisible, focusProps } = useFocusRing();
|
|
21
|
+
const { inputProps, labelProps, isSelected, isPressed } = useRadio(props, state, inputRef);
|
|
22
|
+
const interactionDisabled = props.isDisabled || state.isReadOnly;
|
|
23
|
+
const { hoverProps, isHovered } = useHover({
|
|
24
|
+
...props,
|
|
25
|
+
isDisabled: interactionDisabled
|
|
26
|
+
});
|
|
27
|
+
return /* @__PURE__ */ React2.createElement(
|
|
28
|
+
Control,
|
|
29
|
+
{
|
|
30
|
+
slot: "control",
|
|
31
|
+
label: props.children,
|
|
32
|
+
labelProps: mergeProps(labelProps, hoverProps),
|
|
33
|
+
inputRef,
|
|
34
|
+
inputProps: mergeProps(inputProps, focusProps),
|
|
35
|
+
...apply(props, ["isDisabled", "value", "autoFocus"]),
|
|
36
|
+
...useDataAttributes({
|
|
37
|
+
selected: isSelected,
|
|
38
|
+
pressed: isPressed,
|
|
39
|
+
hovered: isHovered,
|
|
40
|
+
focused: isFocused,
|
|
41
|
+
focusVisible: isFocusVisible,
|
|
42
|
+
disabled: props.isDisabled,
|
|
43
|
+
readonly: state.isReadOnly,
|
|
44
|
+
invalid: state.isInvalid,
|
|
45
|
+
required: state.isRequired
|
|
46
|
+
})
|
|
47
|
+
},
|
|
48
|
+
isSelected ? /* @__PURE__ */ React2.createElement(Icon, { slot: "icon-checked", icon: "radioChecked" }, /* @__PURE__ */ React2.createElement("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", "aria-hidden": "true" }, /* @__PURE__ */ React2.createElement("circle", { cx: 12, cy: 12, r: 8 - 6 / 2, fill: "none", stroke: "orange", strokeWidth: 6 }))) : /* @__PURE__ */ React2.createElement(Icon, { slot: "icon-unchecked", icon: "radioUnchecked" }, /* @__PURE__ */ React2.createElement("svg", { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", "aria-hidden": "true" }, /* @__PURE__ */ React2.createElement("circle", { cx: 12, cy: 12, r: 8, fill: "none", stroke: "gray", strokeWidth: 2 })))
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
var RadioGroup = withSlots("BentoRadioGroup", function RadioGroup2(args) {
|
|
52
|
+
const { errorMessage, ...restArgs } = args;
|
|
53
|
+
const { props, apply } = useProps(restArgs);
|
|
54
|
+
const state = useRadioGroupState(props);
|
|
55
|
+
const { radioGroupProps, labelProps, descriptionProps, errorMessageProps, ...validationResult } = useRadioGroup(
|
|
56
|
+
props,
|
|
57
|
+
state
|
|
58
|
+
);
|
|
59
|
+
const displayedErrorMessage = useMemo(
|
|
60
|
+
function getErrorMessage() {
|
|
61
|
+
return typeof errorMessage === "function" ? errorMessage(validationResult) : errorMessage || validationResult.validationErrors.join(", ");
|
|
62
|
+
},
|
|
63
|
+
[errorMessage, validationResult]
|
|
64
|
+
);
|
|
65
|
+
return /* @__PURE__ */ React2.createElement(
|
|
66
|
+
ControlGroup,
|
|
67
|
+
{
|
|
68
|
+
slot: "group",
|
|
69
|
+
labelProps,
|
|
70
|
+
descriptionProps,
|
|
71
|
+
errorMessage: displayedErrorMessage,
|
|
72
|
+
errorMessageProps,
|
|
73
|
+
...radioGroupProps,
|
|
74
|
+
...apply(props, ["isInvalid", "isDisabled", "isReadOnly", "isRequired", "validationBehavior"]),
|
|
75
|
+
...useDataAttributes({
|
|
76
|
+
orientation: props.orientation || "vertical",
|
|
77
|
+
invalid: state.isInvalid,
|
|
78
|
+
disabled: state.isDisabled,
|
|
79
|
+
readonly: state.isReadOnly,
|
|
80
|
+
required: state.isRequired
|
|
81
|
+
})
|
|
82
|
+
},
|
|
83
|
+
/* @__PURE__ */ React2.createElement(RadioGroupStateContext.Provider, { value: state }, props.children)
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export { Radio, RadioGroup };
|
|
88
|
+
//# sourceMappingURL=index.js.map
|
|
89
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/radio-group-state.tsx","../src/radio.tsx","../src/radio-group.tsx"],"names":["React","Radio","withSlots","RadioGroup","useProps","useMemo","useDataAttributes"],"mappings":";;;;;;;;;;;AAGO,IAAM,sBAAA,GAAyBA,MAAA,CAAM,aAAA,CAAsC,IAAI,CAAA;;;AC8B/E,IAAM,KAAA,GAAQ,SAAA,CAAU,YAAA,EAAc,SAASC,OAAM,IAAA,EAAkB;AAC5E,EAAA,MAAM,EAAE,KAAA,EAAO,KAAA,EAAM,GAAI,SAAS,IAAI,CAAA;AACtC,EAAA,MAAM,KAAA,GAAQD,MAAAA,CAAM,UAAA,CAAW,sBAAsB,CAAA;AACrD,EAAA,MAAM,GAAA,GAAMA,MAAAA,CAAM,MAAA,CAAyB,IAAI,CAAA;AAC/C,EAAA,MAAM,QAAA,GAAW,YAAA,CAAa,OAAA,C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AAA;AAAA,IACA;AAAA,GACF;AAEA,EAAA,MAAM,qBAAA,GAAwBC,OAAAA;AAAA,IAC5B,SAAS,eAAA,GAAkB;AACzB,MAAA,OAAO,OAAO,YAAA,KAAiB,UAAA,GAC3B,YAAA,CAAa,gBAAgB,IAC7B,YAAA,IAAgB,gBAAA,CAAiB,gBAAA,CAAiB,IAAA,CAAK,IAAI,CAAA;AAAA,IACjE,CAAA;AAAA,IACA,CAAC,cAAc,gBAAgB;AAAA,GACjC;AAEA,EAAA,uBACEL,MAAAA,CAAA,aAAA;AAAA,IAAC,YAAA;AAAA,IAAA;AAAA,MACC,IAAA,EAAK,OAAA;AAAA,MACL,UAAA;AAAA,MACA,gBAAA;AAAA,MACA,YAAA,EAAc,qBAAA;AAAA,MACd,iBAAA;AAAA,MACC,GAAG,eAAA;AAAA,MACH,GAAG,MAAM,KAAA,EAAO,CAAC,aAAa,YAAA,EAAc,YAAA,EAAc,YAAA,EAAc,oBAAoB,CAAC,CAAA;AAAA,MAC7F,GAAGM,iBAAAA,CAAkB;AAAA,QACpB,WAAA,EAAa,MAAM,WAAA,IAAe,UAAA;AAAA,QAClC,SAAS,KAAA,CAAM,SAAA;AAAA,QACf,UAAU,KAAA,CAAM,UAAA;AAAA,QAChB,UAAU,KAAA,CAAM,UAAA;AAAA,QAChB,UAAU,KAAA,CAAM;AAAA,OACjB;AAAA,KAAA;AAAA,oBAEDN,OAAA,aAAA,CAAC,sBAAA,CAAuB,UAAvB,EAAgC,KAAA,EAAO,KAAA,EAAA,EAAQ,KAAA,CAAM,QAAS;AAAA,GACjE;AAEJ,CAAC","file":"index.js","sourcesContent":["import React from 'react';\nimport { RadioGroupState } from 'react-stately';\n\nexport const RadioGroupStateContext = React.createContext<RadioGroupState | null>(null);\n","import React, { useMemo } from 'react';\nimport { Control, type ControlProps } from '@bento/control';\nimport { useDataAttributes } from '@bento/use-data-attributes';\nimport { Icon } from '@bento/icon';\nimport { withSlots } from '@bento/slots';\nimport { useProps } from '@bento/use-props';\nimport { mergeProps, mergeRefs, useObjectRef } from '@react-aria/utils';\nimport { useFocusRing, useHover, useRadio, type AriaRadioProps } from 'react-aria';\nimport { RadioGroupStateContext } from './radio-group-state';\n\nexport interface RadioProps extends AriaRadioProps, Partial<Omit<ControlProps, keyof AriaRadioProps>> {\n /** The value of the radio button, used when submitting an HTML form. */\n value: string;\n\n /** A ref for the HTML input element. */\n inputRef?: React.Ref<HTMLInputElement>;\n\n /** The label for the Radio. Accepts any renderable node. */\n children?: React.ReactNode;\n\n /**\n * Whether the radio button is disabled or not. Shows that a selection exists,\n * but is not available in that circumstance.\n */\n isDisabled?: boolean;\n\n /** Whether the element should receive focus on render. */\n autoFocus?: boolean;\n}\n\n/**\n * The `Radio` is a single radio option that can be selected by the user.\n */\nexport const Radio = withSlots('BentoRadio', function Radio(args: RadioProps) {\n const { props, apply } = useProps(args);\n const state = React.useContext(RadioGroupStateContext)!;\n const ref = React.useRef<HTMLInputElement>(null);\n const inputRef = useObjectRef(useMemo(() => mergeRefs(ref, props.inputRef), [ref, props.inputRef]));\n const { isFocused, isFocusVisible, focusProps } = useFocusRing();\n const { inputProps, labelProps, isSelected, isPressed } = useRadio(props as RadioProps, state, inputRef);\n const interactionDisabled = props.isDisabled || state.isReadOnly;\n const { hoverProps, isHovered } = useHover({\n ...props,\n isDisabled: interactionDisabled\n });\n\n return (\n <Control\n slot=\"control\"\n label={props.children}\n labelProps={mergeProps(labelProps, hoverProps)}\n inputRef={inputRef}\n inputProps={mergeProps(inputProps, focusProps)}\n {...apply(props, ['isDisabled', 'value', 'autoFocus'])}\n {...useDataAttributes({\n selected: isSelected,\n pressed: isPressed,\n hovered: isHovered,\n focused: isFocused,\n focusVisible: isFocusVisible,\n disabled: props.isDisabled,\n readonly: state.isReadOnly,\n invalid: state.isInvalid,\n required: state.isRequired\n })}\n >\n {isSelected ? (\n <Icon slot=\"icon-checked\" icon=\"radioChecked\">\n <svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\" aria-hidden=\"true\">\n <circle cx={12} cy={12} r={8 - 6 / 2} fill=\"none\" stroke=\"orange\" strokeWidth={6} />\n </svg>\n </Icon>\n ) : (\n <Icon slot=\"icon-unchecked\" icon=\"radioUnchecked\">\n <svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\" aria-hidden=\"true\">\n <circle cx={12} cy={12} r={8} fill=\"none\" stroke=\"gray\" strokeWidth={2} />\n </svg>\n </Icon>\n )}\n </Control>\n );\n});\n","import React, { useMemo } from 'react';\nimport { ControlGroup, ControlGroupProps } from '@bento/control';\nimport { withSlots } from '@bento/slots';\nimport { useProps } from '@bento/use-props';\nimport { useRadioGroup, type AriaRadioGroupProps } from 'react-aria';\nimport { useRadioGroupState } from 'react-stately';\nimport { RadioGroupStateContext } from './radio-group-state';\nimport { useDataAttributes } from '@bento/use-data-attributes';\nimport { type ValidationResult } from '@react-types/shared';\n\nexport interface RadioGroupProps\n extends AriaRadioGroupProps,\n Partial<Omit<ControlGroupProps, keyof AriaRadioGroupProps>> {\n /** The current value (controlled). */\n value?: string;\n\n /** The default value (uncontrolled). */\n defaultValue?: string;\n\n /** Whether the input is disabled. */\n isDisabled?: boolean;\n\n /** Whether the input can be selected but not changed by the user. */\n isReadOnly?: boolean;\n\n /** Whether user input is required on the input before form submission. */\n isRequired?: boolean;\n\n /** Whether the input value is invalid. */\n isInvalid?: boolean;\n\n /** The name of the input element, used when submitting an HTML form. */\n name?: string;\n\n /**\n * The <form> element to associate the input with.\n * The value of this attribute must be the id of a <form> in the same document.\n */\n form?: string;\n\n /** Radio children. */\n children: React.ReactNode;\n\n /** Error message for the radio group. */\n errorMessage?: React.ReactNode | ((val: ValidationResult) => React.ReactNode);\n}\n\n/**\n * The `RadioGroup` allows a user to select a single item from a list of `Radio` components.\n */\nexport const RadioGroup = withSlots('BentoRadioGroup', function RadioGroup(args: RadioGroupProps) {\n const { errorMessage, ...restArgs } = args;\n const { props, apply } = useProps(restArgs);\n const state = useRadioGroupState(props);\n const { radioGroupProps, labelProps, descriptionProps, errorMessageProps, ...validationResult } = useRadioGroup(\n props,\n state\n );\n\n const displayedErrorMessage = useMemo(\n function getErrorMessage() {\n return typeof errorMessage === 'function'\n ? errorMessage(validationResult)\n : errorMessage || validationResult.validationErrors.join(', ');\n },\n [errorMessage, validationResult]\n );\n\n return (\n <ControlGroup\n slot=\"group\"\n labelProps={labelProps}\n descriptionProps={descriptionProps}\n errorMessage={displayedErrorMessage}\n errorMessageProps={errorMessageProps}\n {...radioGroupProps}\n {...apply(props, ['isInvalid', 'isDisabled', 'isReadOnly', 'isRequired', 'validationBehavior'])}\n {...useDataAttributes({\n orientation: props.orientation || 'vertical',\n invalid: state.isInvalid,\n disabled: state.isDisabled,\n readonly: state.isReadOnly,\n required: state.isRequired\n })}\n >\n <RadioGroupStateContext.Provider value={state}>{props.children}</RadioGroupStateContext.Provider>\n </ControlGroup>\n );\n});\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bento/radio",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Radio component",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup-node",
|
|
10
|
+
"lint": "biome lint && tsc",
|
|
11
|
+
"posttest": "npm run lint",
|
|
12
|
+
"pretest": "npm run build",
|
|
13
|
+
"test": "vitest --run",
|
|
14
|
+
"test:watch": "vitest"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/godaddy/bento.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"accessibility",
|
|
22
|
+
"aria",
|
|
23
|
+
"bento",
|
|
24
|
+
"component",
|
|
25
|
+
"interactive",
|
|
26
|
+
"library",
|
|
27
|
+
"radio",
|
|
28
|
+
"react"
|
|
29
|
+
],
|
|
30
|
+
"author": "GoDaddy Operating Company, LLC",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/godaddy/bento/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/godaddy/bento#readme",
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"src",
|
|
39
|
+
"package.json"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@bento/control": "^0.1.1",
|
|
43
|
+
"@bento/icon": "^0.1.1",
|
|
44
|
+
"@bento/slots": "^0.1.2",
|
|
45
|
+
"@bento/use-data-attributes": "^0.1.0",
|
|
46
|
+
"@bento/use-props": "^0.1.0",
|
|
47
|
+
"@react-aria/utils": "^3.30.0",
|
|
48
|
+
"react-aria": "^3.44.0",
|
|
49
|
+
"react-stately": "^3.42.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"react": "18.x || 19.x",
|
|
53
|
+
"react-dom": "18.x || 19.x"
|
|
54
|
+
},
|
|
55
|
+
"exports": {
|
|
56
|
+
".": {
|
|
57
|
+
"import": {
|
|
58
|
+
"types": "./dist/index.d.ts",
|
|
59
|
+
"default": "./dist/index.js"
|
|
60
|
+
},
|
|
61
|
+
"require": {
|
|
62
|
+
"types": "./dist/index.d.cts",
|
|
63
|
+
"default": "./dist/index.cjs"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"./package.json": "./package.json"
|
|
67
|
+
},
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public",
|
|
70
|
+
"registry": "https://registry.npmjs.org/"
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import React, { useMemo } from 'react';
|
|
2
|
+
import { ControlGroup, ControlGroupProps } from '@bento/control';
|
|
3
|
+
import { withSlots } from '@bento/slots';
|
|
4
|
+
import { useProps } from '@bento/use-props';
|
|
5
|
+
import { useRadioGroup, type AriaRadioGroupProps } from 'react-aria';
|
|
6
|
+
import { useRadioGroupState } from 'react-stately';
|
|
7
|
+
import { RadioGroupStateContext } from './radio-group-state';
|
|
8
|
+
import { useDataAttributes } from '@bento/use-data-attributes';
|
|
9
|
+
import { type ValidationResult } from '@react-types/shared';
|
|
10
|
+
|
|
11
|
+
export interface RadioGroupProps
|
|
12
|
+
extends AriaRadioGroupProps,
|
|
13
|
+
Partial<Omit<ControlGroupProps, keyof AriaRadioGroupProps>> {
|
|
14
|
+
/** The current value (controlled). */
|
|
15
|
+
value?: string;
|
|
16
|
+
|
|
17
|
+
/** The default value (uncontrolled). */
|
|
18
|
+
defaultValue?: string;
|
|
19
|
+
|
|
20
|
+
/** Whether the input is disabled. */
|
|
21
|
+
isDisabled?: boolean;
|
|
22
|
+
|
|
23
|
+
/** Whether the input can be selected but not changed by the user. */
|
|
24
|
+
isReadOnly?: boolean;
|
|
25
|
+
|
|
26
|
+
/** Whether user input is required on the input before form submission. */
|
|
27
|
+
isRequired?: boolean;
|
|
28
|
+
|
|
29
|
+
/** Whether the input value is invalid. */
|
|
30
|
+
isInvalid?: boolean;
|
|
31
|
+
|
|
32
|
+
/** The name of the input element, used when submitting an HTML form. */
|
|
33
|
+
name?: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The <form> element to associate the input with.
|
|
37
|
+
* The value of this attribute must be the id of a <form> in the same document.
|
|
38
|
+
*/
|
|
39
|
+
form?: string;
|
|
40
|
+
|
|
41
|
+
/** Radio children. */
|
|
42
|
+
children: React.ReactNode;
|
|
43
|
+
|
|
44
|
+
/** Error message for the radio group. */
|
|
45
|
+
errorMessage?: React.ReactNode | ((val: ValidationResult) => React.ReactNode);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The `RadioGroup` allows a user to select a single item from a list of `Radio` components.
|
|
50
|
+
*/
|
|
51
|
+
export const RadioGroup = withSlots('BentoRadioGroup', function RadioGroup(args: RadioGroupProps) {
|
|
52
|
+
const { errorMessage, ...restArgs } = args;
|
|
53
|
+
const { props, apply } = useProps(restArgs);
|
|
54
|
+
const state = useRadioGroupState(props);
|
|
55
|
+
const { radioGroupProps, labelProps, descriptionProps, errorMessageProps, ...validationResult } = useRadioGroup(
|
|
56
|
+
props,
|
|
57
|
+
state
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const displayedErrorMessage = useMemo(
|
|
61
|
+
function getErrorMessage() {
|
|
62
|
+
return typeof errorMessage === 'function'
|
|
63
|
+
? errorMessage(validationResult)
|
|
64
|
+
: errorMessage || validationResult.validationErrors.join(', ');
|
|
65
|
+
},
|
|
66
|
+
[errorMessage, validationResult]
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<ControlGroup
|
|
71
|
+
slot="group"
|
|
72
|
+
labelProps={labelProps}
|
|
73
|
+
descriptionProps={descriptionProps}
|
|
74
|
+
errorMessage={displayedErrorMessage}
|
|
75
|
+
errorMessageProps={errorMessageProps}
|
|
76
|
+
{...radioGroupProps}
|
|
77
|
+
{...apply(props, ['isInvalid', 'isDisabled', 'isReadOnly', 'isRequired', 'validationBehavior'])}
|
|
78
|
+
{...useDataAttributes({
|
|
79
|
+
orientation: props.orientation || 'vertical',
|
|
80
|
+
invalid: state.isInvalid,
|
|
81
|
+
disabled: state.isDisabled,
|
|
82
|
+
readonly: state.isReadOnly,
|
|
83
|
+
required: state.isRequired
|
|
84
|
+
})}
|
|
85
|
+
>
|
|
86
|
+
<RadioGroupStateContext.Provider value={state}>{props.children}</RadioGroupStateContext.Provider>
|
|
87
|
+
</ControlGroup>
|
|
88
|
+
);
|
|
89
|
+
});
|
package/src/radio.tsx
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import React, { useMemo } from 'react';
|
|
2
|
+
import { Control, type ControlProps } from '@bento/control';
|
|
3
|
+
import { useDataAttributes } from '@bento/use-data-attributes';
|
|
4
|
+
import { Icon } from '@bento/icon';
|
|
5
|
+
import { withSlots } from '@bento/slots';
|
|
6
|
+
import { useProps } from '@bento/use-props';
|
|
7
|
+
import { mergeProps, mergeRefs, useObjectRef } from '@react-aria/utils';
|
|
8
|
+
import { useFocusRing, useHover, useRadio, type AriaRadioProps } from 'react-aria';
|
|
9
|
+
import { RadioGroupStateContext } from './radio-group-state';
|
|
10
|
+
|
|
11
|
+
export interface RadioProps extends AriaRadioProps, Partial<Omit<ControlProps, keyof AriaRadioProps>> {
|
|
12
|
+
/** The value of the radio button, used when submitting an HTML form. */
|
|
13
|
+
value: string;
|
|
14
|
+
|
|
15
|
+
/** A ref for the HTML input element. */
|
|
16
|
+
inputRef?: React.Ref<HTMLInputElement>;
|
|
17
|
+
|
|
18
|
+
/** The label for the Radio. Accepts any renderable node. */
|
|
19
|
+
children?: React.ReactNode;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether the radio button is disabled or not. Shows that a selection exists,
|
|
23
|
+
* but is not available in that circumstance.
|
|
24
|
+
*/
|
|
25
|
+
isDisabled?: boolean;
|
|
26
|
+
|
|
27
|
+
/** Whether the element should receive focus on render. */
|
|
28
|
+
autoFocus?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The `Radio` is a single radio option that can be selected by the user.
|
|
33
|
+
*/
|
|
34
|
+
export const Radio = withSlots('BentoRadio', function Radio(args: RadioProps) {
|
|
35
|
+
const { props, apply } = useProps(args);
|
|
36
|
+
const state = React.useContext(RadioGroupStateContext)!;
|
|
37
|
+
const ref = React.useRef<HTMLInputElement>(null);
|
|
38
|
+
const inputRef = useObjectRef(useMemo(() => mergeRefs(ref, props.inputRef), [ref, props.inputRef]));
|
|
39
|
+
const { isFocused, isFocusVisible, focusProps } = useFocusRing();
|
|
40
|
+
const { inputProps, labelProps, isSelected, isPressed } = useRadio(props as RadioProps, state, inputRef);
|
|
41
|
+
const interactionDisabled = props.isDisabled || state.isReadOnly;
|
|
42
|
+
const { hoverProps, isHovered } = useHover({
|
|
43
|
+
...props,
|
|
44
|
+
isDisabled: interactionDisabled
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<Control
|
|
49
|
+
slot="control"
|
|
50
|
+
label={props.children}
|
|
51
|
+
labelProps={mergeProps(labelProps, hoverProps)}
|
|
52
|
+
inputRef={inputRef}
|
|
53
|
+
inputProps={mergeProps(inputProps, focusProps)}
|
|
54
|
+
{...apply(props, ['isDisabled', 'value', 'autoFocus'])}
|
|
55
|
+
{...useDataAttributes({
|
|
56
|
+
selected: isSelected,
|
|
57
|
+
pressed: isPressed,
|
|
58
|
+
hovered: isHovered,
|
|
59
|
+
focused: isFocused,
|
|
60
|
+
focusVisible: isFocusVisible,
|
|
61
|
+
disabled: props.isDisabled,
|
|
62
|
+
readonly: state.isReadOnly,
|
|
63
|
+
invalid: state.isInvalid,
|
|
64
|
+
required: state.isRequired
|
|
65
|
+
})}
|
|
66
|
+
>
|
|
67
|
+
{isSelected ? (
|
|
68
|
+
<Icon slot="icon-checked" icon="radioChecked">
|
|
69
|
+
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
|
|
70
|
+
<circle cx={12} cy={12} r={8 - 6 / 2} fill="none" stroke="orange" strokeWidth={6} />
|
|
71
|
+
</svg>
|
|
72
|
+
</Icon>
|
|
73
|
+
) : (
|
|
74
|
+
<Icon slot="icon-unchecked" icon="radioUnchecked">
|
|
75
|
+
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
|
|
76
|
+
<circle cx={12} cy={12} r={8} fill="none" stroke="gray" strokeWidth={2} />
|
|
77
|
+
</svg>
|
|
78
|
+
</Icon>
|
|
79
|
+
)}
|
|
80
|
+
</Control>
|
|
81
|
+
);
|
|
82
|
+
});
|