|
8 | 8 | * @see WAI-ARIA https://www.w3.org/TR/wai-aria-practices-1.2
|
9 | 9 | */
|
10 | 10 |
|
11 |
| -import { defineComponent, h, Fragment, PropType } from "vue" |
| 11 | +import { |
| 12 | + computed, |
| 13 | + defineComponent, |
| 14 | + h, |
| 15 | + InputHTMLAttributes, |
| 16 | + isVNode, |
| 17 | + mergeProps, |
| 18 | + PropType, |
| 19 | + reactive, |
| 20 | +} from "vue" |
12 | 21 | import {
|
13 | 22 | chakra,
|
14 |
| - DOMElements, |
| 23 | + HTMLChakraProps, |
| 24 | + type ThemingProps, |
| 25 | + type StyleResolverProps, |
| 26 | + useMultiStyleConfig, |
15 | 27 | } from "@chakra-ui/vue-system"
|
| 28 | +import * as VS from "@chakra-ui/vue-system" |
| 29 | +import { SNAO, vueThemingProps } from "@chakra-ui/vue-utils" |
| 30 | +import { RadioContext, useRadioGroupContext } from "./radio-context" |
16 | 31 |
|
17 |
| -export interface CRadioProps {} |
| 32 | +export interface CRadioProps |
| 33 | + extends Omit<HTMLChakraProps<"label">, keyof RadioContext>, |
| 34 | + ThemingProps<"Radio">, |
| 35 | + RadioContext { |
| 36 | + /** |
| 37 | + * The spacing between the checkbox and its label text |
| 38 | + * @default 0.5rem |
| 39 | + * @type SystemProps["marginLeft"] |
| 40 | + */ |
| 41 | + spacing?: StyleResolverProps["marginLeft"] |
| 42 | + /** |
| 43 | + * Additional props to be forwarded to the `input` element |
| 44 | + */ |
| 45 | + inputProps?: InputHTMLAttributes |
| 46 | +} |
18 | 47 |
|
19 | 48 | export const CRadio = defineComponent({
|
20 |
| - props: { |
21 |
| - as: { |
22 |
| - type: [Object, String] as PropType<DOMElements>, |
23 |
| - default: "div", |
24 |
| - }, |
| 49 | + props: { |
| 50 | + value: { |
| 51 | + type: String as PropType<CRadioProps["value"]>, |
| 52 | + required: true, |
| 53 | + }, |
| 54 | + disabled: { |
| 55 | + type: Boolean as PropType<CRadioProps["disabled"]>, |
| 56 | + }, |
| 57 | + invalid: { |
| 58 | + type: Boolean as PropType<CRadioProps["invalid"]>, |
| 59 | + }, |
| 60 | + readOnly: { |
| 61 | + type: Boolean as PropType<CRadioProps["readOnly"]>, |
25 | 62 | },
|
26 |
| - setup(props, { slots, attrs }) { |
27 |
| - return () => ( |
28 |
| - <chakra.div as={props.as} {...attrs}> |
29 |
| - {slots} |
30 |
| - </chakra.div> |
31 |
| - ) |
| 63 | + spacing: { |
| 64 | + type: SNAO as PropType<CRadioProps["spacing"]>, |
32 | 65 | },
|
33 |
| - }) |
| 66 | + ...vueThemingProps, |
| 67 | + }, |
| 68 | + setup(props, { slots, attrs }) { |
| 69 | + const groupApi = useRadioGroupContext() |
| 70 | + |
| 71 | + const styleAttrs = computed(() => mergeProps(props, groupApi.value, attrs)) |
| 72 | + |
| 73 | + const styles = useMultiStyleConfig("Radio", styleAttrs) |
| 74 | + |
| 75 | + const radioProps = reactive({ |
| 76 | + value: props.value, |
| 77 | + disabled: props.disabled, |
| 78 | + invalid: props.invalid, |
| 79 | + readOnly: props.readOnly, |
| 80 | + }) |
| 81 | + |
| 82 | + const inputProps = computed(() => { |
| 83 | + const apiInputProps = groupApi.value.getRadioInputProps(radioProps) |
| 84 | + const apiInputState = groupApi.value.getRadioState(radioProps) |
| 85 | + |
| 86 | + return { |
| 87 | + ...apiInputProps, |
| 88 | + modelValue: apiInputState.isChecked, |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + const labelStyles = computed(() => ({ |
| 93 | + userSelect: "none", |
| 94 | + marginStart: props.spacing, |
| 95 | + ...styles.value.label, |
| 96 | + })) |
| 97 | + |
| 98 | + const controlStyles = computed(() => ({ |
| 99 | + display: "inline-flex", |
| 100 | + alignItems: "center", |
| 101 | + justifyContent: "center", |
| 102 | + flexShrink: 0, |
| 103 | + ...styles.value.control, |
| 104 | + })) |
| 105 | + |
| 106 | + const RadioLabel = computed(() => { |
| 107 | + return isVNode(slots.default) ? ( |
| 108 | + <chakra.span |
| 109 | + __label="radio__label" |
| 110 | + {...groupApi.value.getRadioLabelProps(radioProps)} |
| 111 | + > |
| 112 | + {slots.default?.()} |
| 113 | + </chakra.span> |
| 114 | + ) : null |
| 115 | + }) |
| 116 | + |
| 117 | + return () => ( |
| 118 | + <chakra.label {...groupApi.value.getRadioProps(radioProps)} {...attrs}> |
| 119 | + <chakra.input |
| 120 | + __label="radio__input" |
| 121 | + {...inputProps.value} |
| 122 | + __css={labelStyles.value} |
| 123 | + /> |
| 124 | + <chakra.span |
| 125 | + __label="radio__control" |
| 126 | + {...groupApi.value.getRadioControlProps(radioProps)} |
| 127 | + __css={controlStyles.value} |
| 128 | + /> |
| 129 | + {RadioLabel.value} |
| 130 | + </chakra.label> |
| 131 | + ) |
| 132 | + }, |
| 133 | +}) |
0 commit comments