All files / components/radio Radio.vue

100% Statements 37/37
89.65% Branches 26/29
100% Functions 8/8
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250    1x 5x       1x     1x 1x 1x 1x 1x 1x       1x                                                                                                                   10x 10x 10x 10x 10x 10x       10x   10x 10x 10x           10x       1x         10x 10x                     1x 1x 1x 1x 1x 1x   10x 10x           1x                                             6x               2x                     1x 1x                                                                                                                                                      
<template>
	<span class="cdx-radio" :class="rootClasses">
		<input
			:id="radioId"
			ref="input"
			v-model="wrappedModel"
			class="cdx-radio__input"
			type="radio"
			:aria-describedby="( $slots.description &&
				$slots.description().length > 0 ) ? descriptionId : undefined"
			:name="name"
			:value="inputValue"
			:disabled="computedDisabled"
		>
		<span class="cdx-radio__icon" />
		<!-- Only render a Label component if label text has been provided. This component can also
			supply a description to the Radio if content is provided in the description slot. -->
		<cdx-label
			v-if="$slots.default && $slots.default().length"
			class="cdx-radio__label"
			:input-id="radioId"
			:description-id="( $slots.description &&
				$slots.description().length > 0 ) ? descriptionId : undefined"
			:disabled="computedDisabled"
			@click="focusInput"
		>
			<!-- @slot Label text. -->
			<slot />
			<template v-if="$slots.description && $slots.description().length > 0" #description>
				<!-- @slot Short description text. -->
				<slot name="description" />
			</template>
		</cdx-label>
	</span>
</template>
 
<script lang="ts">
import { defineComponent, ref, toRef, computed } from 'vue';
import CdxLabel from '../label/Label.vue';
import useLabelChecker from '../../composables/useLabelChecker';
import useModelWrapper from '../../composables/useModelWrapper';
import useGeneratedId from '../../composables/useGeneratedId';
import useFieldData from '../../composables/useFieldData';
 
/**
 * A binary input that is usually combined in a group of two or more options.
 */
export default defineComponent( {
	name: 'CdxRadio',
	components: { CdxLabel },
	props: {
		/**
		 * Value of the currently selected radio.
		 *
		 * Provided by `v-model` binding in the parent component.
		 */
		modelValue: {
			type: [ String, Number, Boolean ],
			default: ''
		},
		/**
		 * HTML "value" attribute to assign to the input.
		 *
		 * Required for input groups.
		 */
		inputValue: {
			type: [ String, Number, Boolean ],
			default: false
		},
		/**
		 * HTML "name" attribute to assign to the input.
		 */
		name: {
			type: String,
			required: true
		},
		/**
		 * Whether the disabled attribute should be added to the input.
		 */
		disabled: {
			type: Boolean,
			default: false
		},
		/**
		 * Whether the component should display inline.
		 *
		 * By default, `display: block` is set and a margin exists between
		 * sibling components, for a stacked layout.
		 */
		inline: {
			type: Boolean,
			default: false
		}
	},
	emits: [
		/**
		 * Emitted when modelValue changes.
		 *
		 * @property {string | number | boolean} modelValue The new model value
		 */
		'update:modelValue'
	],
	setup( props, { emit, slots, attrs } ) {
		useLabelChecker( slots.default?.(), attrs, 'CdxRadio' );
 
		const rootClasses = computed( (): Record<string, boolean> => {
			return {
				'cdx-radio--inline': props.inline
			};
		} );
 
		const { computedDisabled } = useFieldData( toRef( props, 'disabled' ) );
 
		// Declare template ref.
		const input = ref<HTMLInputElement>();
		const radioId = useGeneratedId( 'radio' );
		const descriptionId = useGeneratedId( 'description' );
 
		/**
		 * When the label is clicked, focus on the input.
		 *
		 * This doesn't happen automatically in Firefox or Safari.
		 */
		const focusInput = (): void => {
			// This method only gets called when the label is interacted with,
			// so we can be confident that the input ref also exists.
			// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
			input.value!.focus();
		};
 
		// Take the modelValue provided by the parent component via v-model and
		// generate a wrapped model that we can use for the input element in
		// this component.
		const wrappedModel = useModelWrapper( toRef( props, 'modelValue' ), emit );
 
		return {
			rootClasses,
			computedDisabled,
			input,
			radioId,
			descriptionId,
			focusInput,
			wrappedModel
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/binary-input.less';
 
.cdx-radio {
	// Common binary input styles.
	.cdx-mixin-binary-input();
 
	// Custom-styled radio that's visible to the user.
	&__icon {
		border-radius: @border-radius-circle;
 
		// Add `:focus` state's inner circle.
		&::before {
			content: ' ';
			position: absolute;
			top: -@size-25;
			right: -@size-25;
			bottom: -@size-25;
			left: -@size-25;
			border: @border-width-base @border-style-base @border-color-transparent;
			border-radius: @border-radius-circle;
		}
	}
 
	// HTML `<input type="radio">`.
	// Based on the HTML attributes of the radio input, we can change the style of the adjacent
	// `span`, which will look like a custom-styled radio.
	&__input {
		&:enabled {
			& + .cdx-radio__icon {
				border-color: @border-color-input-binary;
			}
 
			// Note: there is no focus behavior for the input in its unchecked state because you
			// can't focus on it without selecting it.
			&:hover + .cdx-radio__icon {
				border-color: @border-color-input-binary--hover;
			}
 
			&:active + .cdx-radio__icon {
				background-color: @background-color-progressive--active;
				border-color: @border-color-progressive--active;
			}
 
			&:focus + .cdx-radio__icon {
				border-color: @border-color-input-binary--focus;
				box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus;
			}
 
			&:checked {
				& + .cdx-radio__icon {
					border-width: @border-width-input-radio--checked;
					border-color: @border-color-input-binary--checked;
				}
 
				&:hover + .cdx-radio__icon {
					border-color: @border-color-input-binary--hover;
				}
 
				&:focus + .cdx-radio__icon {
					&::before {
						border-color: @border-color-inverted;
					}
				}
 
				// Put `:active` after `:focus` at 'filled' progressive components. Otherwise a
				// focus outline would be visible when actively clicked.
				&:active + .cdx-radio__icon {
					background-color: @background-color-base-fixed;
					border-color: @border-color-progressive--active;
 
					&::before {
						border-color: @border-color-progressive--active;
					}
				}
			}
		}
 
		/* stylelint-disable no-descending-specificity */
		&:disabled {
			// DEPRECATED: Support CSS-only components that don't set the `cdx-label` class on the
			// label (T353885)
			& ~ .cdx-radio__label,
			& ~ .cdx-radio__label.cdx-label {
				color: @color-disabled;
			}
 
			& + .cdx-radio__icon {
				background-color: @background-color-disabled;
				border-color: @border-color-disabled;
			}
 
			&:checked + .cdx-radio__icon {
				border-width: @border-width-input-radio--checked;
			}
		}
		/* stylelint-enable no-descending-specificity */
	}
}
</style>