|
| 1 | +/** |
| 2 | + * Hey! Welcome to @chakra-ui/vue-next CRadio |
| 3 | + * |
| 4 | + * Radios are used when only one choice may be selected in a series of options |
| 5 | + * |
| 6 | + * @see Docs https://next.vue.chakra-ui.com/c-radio |
| 7 | + * @see Source https://github.com/chakra-ui/chakra-ui-vue-next/blob/main/packages/c-radio/src/c-radio/c-radio.ts |
| 8 | + * @see WAI-ARIA https://www.w3.org/TR/wai-aria-practices-1.2 |
| 9 | + */ |
| 10 | + |
| 11 | +import { |
| 12 | + computed, |
| 13 | + defineComponent, |
| 14 | + h, |
| 15 | + InputHTMLAttributes, |
| 16 | + isVNode, |
| 17 | + mergeProps, |
| 18 | + PropType, |
| 19 | + reactive, |
| 20 | +} from "vue" |
| 21 | +import { |
| 22 | + chakra, |
| 23 | + HTMLChakraProps, |
| 24 | + type ThemingProps, |
| 25 | + type StyleResolverProps, |
| 26 | + useMultiStyleConfig, |
| 27 | +} from "@chakra-ui/vue-system" |
| 28 | +import * as VS from "@chakra-ui/vue-system" |
| 29 | +import { getValidChildren, SNAO, vueThemingProps } from "@chakra-ui/vue-utils" |
| 30 | +import { RadioContext, useRadioGroupContext } from "./radio-context" |
| 31 | + |
| 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 | +} |
| 47 | + |
| 48 | +export const CRadio = defineComponent({ |
| 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"]>, |
| 62 | + }, |
| 63 | + spacing: { |
| 64 | + type: SNAO as PropType<CRadioProps["spacing"]>, |
| 65 | + default: "0.5rem", |
| 66 | + }, |
| 67 | + ...vueThemingProps, |
| 68 | + }, |
| 69 | + setup(props, { slots, attrs }) { |
| 70 | + const groupApi = useRadioGroupContext() |
| 71 | + |
| 72 | + const styleAttrs = computed(() => mergeProps(props, groupApi.value, attrs)) |
| 73 | + |
| 74 | + const styles = useMultiStyleConfig("Radio", styleAttrs) |
| 75 | + |
| 76 | + const radioProps = reactive({ |
| 77 | + value: props.value, |
| 78 | + disabled: props.disabled, |
| 79 | + invalid: props.invalid, |
| 80 | + readOnly: props.readOnly, |
| 81 | + }) |
| 82 | + |
| 83 | + const inputProps = computed(() => { |
| 84 | + const apiInputProps = groupApi.value.getRadioInputProps(radioProps) |
| 85 | + const apiInputState = groupApi.value.getRadioState(radioProps) |
| 86 | + |
| 87 | + return { |
| 88 | + ...apiInputProps, |
| 89 | + // modelValue: apiInputState.isChecked, |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + const rootStyles = computed(() => ({ |
| 94 | + display: "inline-flex", |
| 95 | + alignItems: "center", |
| 96 | + verticalAlign: "top", |
| 97 | + cursor: "pointer", |
| 98 | + position: "relative", |
| 99 | + ...styles.value.container, |
| 100 | + })) |
| 101 | + |
| 102 | + const labelStyles = computed(() => ({ |
| 103 | + userSelect: "none", |
| 104 | + marginStart: props.spacing, |
| 105 | + ...styles.value.label, |
| 106 | + })) |
| 107 | + |
| 108 | + const controlStyles = computed(() => ({ |
| 109 | + display: "inline-flex", |
| 110 | + alignItems: "center", |
| 111 | + justifyContent: "center", |
| 112 | + flexShrink: 0, |
| 113 | + ...styles.value.control, |
| 114 | + })) |
| 115 | + |
| 116 | + return () => ( |
| 117 | + <chakra.label |
| 118 | + {...groupApi.value.getRadioProps(radioProps)} |
| 119 | + __css={rootStyles.value} |
| 120 | + {...attrs} |
| 121 | + > |
| 122 | + <chakra.input |
| 123 | + __chakraIsRaw |
| 124 | + __label="radio__input" |
| 125 | + {...inputProps.value} |
| 126 | + /> |
| 127 | + <chakra.span |
| 128 | + __label="radio__control" |
| 129 | + {...groupApi.value.getRadioControlProps(radioProps)} |
| 130 | + __css={controlStyles.value} |
| 131 | + /> |
| 132 | + <chakra.span |
| 133 | + __label="radio__label" |
| 134 | + {...groupApi.value.getRadioLabelProps(radioProps)} |
| 135 | + __css={labelStyles.value} |
| 136 | + > |
| 137 | + {() => getValidChildren(slots)} |
| 138 | + </chakra.span> |
| 139 | + </chakra.label> |
| 140 | + ) |
| 141 | + }, |
| 142 | +}) |
0 commit comments