Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit aaadd1a

Browse files
feat(c-radio): add group
1 parent d5fbb49 commit aaadd1a

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { PropType, defineComponent } from "vue"
2+
import { UseRadioGroupProps, useRadioGroup } from "./use-radio-group"
3+
import {
4+
HTMLChakraProps,
5+
ThemingProps,
6+
chakra,
7+
useMultiStyleConfig,
8+
} from "@chakra-ui/vue-system"
9+
import { RadioGroupProvider, RadioGroupStylesProvider } from "./radio-context"
10+
import { useThemingProps } from "@chakra-ui/vue-utils"
11+
12+
export interface CRadioGroupProps
13+
extends UseRadioGroupProps,
14+
HTMLChakraProps<"div">,
15+
Omit<ThemingProps<"Radio">, "orientation"> {}
16+
17+
export const CRadioGroup = defineComponent({
18+
name: "CRadioGroup",
19+
props: {
20+
dir: {
21+
type: String as PropType<CRadioGroupProps["dir"]>,
22+
},
23+
disabled: {
24+
type: Boolean as PropType<CRadioGroupProps["disabled"]>,
25+
},
26+
form: {
27+
type: String as PropType<CRadioGroupProps["form"]>,
28+
},
29+
getRootNode: {
30+
type: Function as PropType<CRadioGroupProps["getRootNode"]>,
31+
},
32+
id: {
33+
type: String as PropType<CRadioGroupProps["id"]>,
34+
},
35+
ids: {
36+
type: Object as PropType<CRadioGroupProps["ids"]>,
37+
},
38+
name: {
39+
type: String as PropType<CRadioGroupProps["name"]>,
40+
},
41+
orientation: {
42+
type: String as PropType<CRadioGroupProps["orientation"]>,
43+
},
44+
readonly: {
45+
type: Boolean as PropType<CRadioGroupProps["readOnly"]>,
46+
},
47+
},
48+
emits: ["change", "update:modelValue"],
49+
setup(props, { slots, attrs }) {
50+
const api = useRadioGroup(props)
51+
52+
const themeProps = useThemingProps(props)
53+
54+
const styles = useMultiStyleConfig("Radio", themeProps)
55+
56+
RadioGroupProvider(api)
57+
58+
RadioGroupStylesProvider(styles)
59+
60+
return () => (
61+
<chakra.div __label="radio-group" {...api.value.rootProps} {...attrs}>
62+
{slots.default?.(api.value)}
63+
</chakra.div>
64+
)
65+
},
66+
})

packages/c-radio/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from "./c-radio"
1+
export { CRadioGroup, type CRadioGroupProps } from "./c-radio-group"
2+
export { RadioGroupContext } from "./radio-context"

packages/c-radio/src/radio-context.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as radio from "@zag-js/radio-group"
22
import { createContext } from "@chakra-ui/vue-utils"
33
import type { UseRadioGroupReturn } from "./use-radio-group"
4+
import { UnwrapRef } from "vue"
5+
import { createStylesContext } from "@chakra-ui/vue-system"
46

57
export type RadioContext = Parameters<
68
ReturnType<typeof radio.connect>["getRadioProps"]
@@ -11,10 +13,13 @@ export const [RadioProvider, useRadioContext] = createContext<RadioContext>({
1113
strict: true,
1214
})
1315

14-
export type RadioGroupContext = radio.Context
16+
export type RadioGroupContext = UnwrapRef<UseRadioGroupReturn>
1517

1618
export const [RadioGroupProvider, useRadioGroupContext] =
1719
createContext<UseRadioGroupReturn>({
1820
name: "CRadioGroupContext",
1921
strict: true,
2022
})
23+
24+
export const [RadioGroupStylesProvider, useRadioGroupStyles] =
25+
createStylesContext("CRadioGroup")

packages/c-radio/src/use-radio-group.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { computed, getCurrentInstance, reactive } from "vue"
22
import { normalizeProps, useMachine } from "@zag-js/vue"
33
import * as radio from "@zag-js/radio-group"
44
import { useId } from "@chakra-ui/vue-composables"
5+
import { Optional } from "@chakra-ui/vue-utils"
56
import type { RadioGroupContext } from "./radio-context"
67

7-
export type UseRadioGroupProps = RadioGroupContext & {
8-
modelValue?: RadioGroupContext["value"]
8+
export type UseRadioGroupProps = Optional<radio.Context, "id"> & {
9+
modelValue?: radio.Context["value"]
910
}
1011

1112
export const useRadioGroup = (props: UseRadioGroupProps) => {

0 commit comments

Comments
 (0)