All files / components/radio Radio.vue

100% Statements 45/45
90.32% Branches 28/31
100% Functions 9/9
100% Lines 45/45

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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369    2x 10x       2x     2x 2x 2x 2x 2x 2x 2x 2x 2x       2x                                                                                                                                   15x 15x 15x 15x 15x 15x 15x 15x   15x       15x 2x         15x 15x 15x           15x       1x         15x 15x                       2x 2x 2x 2x 2x 2x 2x   15x 15x             1x                                             10x               6x                                           2x 2x                                                                                                                                                                                                                                                                                                                                  
<template>
	<div class="cdx-radio" :class="rootClasses">
		<div class="cdx-radio__wrapper">
			<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>
		</div>
		<!-- Only render custom input component(s) if custom input has been provided. -->
		<div
			v-if="$slots[ 'custom-input' ]"
			class="cdx-radio__custom-input"
			:class="customInputClasses"
		>
			<!-- @slot Custom input. -->
			<slot name="custom-input" />
		</div>
	</div>
</template>
 
<script lang="ts">
import { defineComponent, PropType, 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';
import { ValidationStatusType } from '../../types';
import { makeStringTypeValidator } from '../../utils/stringTypeValidator';
import { ValidationStatusTypes } from '../../constants';
 
const statusValidator = makeStringTypeValidator( ValidationStatusTypes );
 
/**
 * 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
		},
		/**
		 * Validation status of the Radio.
		 */
		status: {
			type: String as PropType<ValidationStatusType>,
			default: 'default',
			validator: statusValidator
		}
	},
	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 {
			computedDisabled,
			computedStatus
		} = useFieldData(
			toRef( props, 'disabled' ),
			toRef( props, 'status' )
		);
 
		const rootClasses = computed( (): Record<string, boolean> => {
			return {
				'cdx-radio--inline': props.inline,
				[ `cdx-radio--status-${ computedStatus.value }` ]: true
			};
		} );
 
		const customInputClasses = computed( (): Record<string, boolean> => {
			return {
				'cdx-radio__custom-input--inline': props.inline
			};
		} );
 
		// 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,
			customInputClasses
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/binary-input.less';
 
@border-width-input-radio--checked-error-focus: 4px;

.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 {
				background-color: @background-color-base;
				border-color: @border-color-interactive;
			}
 
			// 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 {
				background-color: @background-color-interactive-subtle--hover;
				border-color: @border-color-interactive--hover;
			}
 
			&:active + .cdx-radio__icon {
				background-color: @background-color-interactive-subtle--active;
				border-color: @border-color-interactive--active;
			}
 
			&:focus:not( :active ) + .cdx-radio__icon {
				border-color: @border-color-progressive--focus;
				box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus;
			}
 
			&:checked {
				/* stylelint-disable-next-line no-descending-specificity */
				& + .cdx-radio__icon {
					background-color: @background-color-base-fixed;
					border-width: @border-width-input-radio--checked;
					border-color: @border-color-progressive;
				}
 
				&:hover + .cdx-radio__icon {
					border-color: @border-color-progressive--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 */
			.cdx-radio--status-error & {
				& ~ .cdx-radio__label {
					color: @color-error;
				}
 
				& + .cdx-radio__icon {
					background-color: @background-color-error-subtle;
					border-color: @border-color-error;
				}
 
				&:hover + .cdx-radio__icon {
					background-color: @background-color-error-subtle--hover;
					border-color: @border-color-error--hover;
				}
 
				&:focus + .cdx-radio__icon {
					border-color: @border-color-progressive--focus;
				}
 
				&:active + .cdx-radio__icon {
					background-color: @background-color-error-subtle--active;
					border-color: @border-color-error--active;
					box-shadow: none;
				}
 
				&:checked {
					& + .cdx-radio__icon {
						// A background color token is being used in place
						// of a border token because the Radio background
						// is built with borders only.
						background-color: @background-color-base-fixed;
						border-color: @background-color-error;
					}
 
					&:hover + .cdx-radio__icon {
						border-color: @background-color-error--hover;
					}
					/* stylelint-disable-next-line selector-not-notation */
					&:focus:not( &:active ) + .cdx-radio__icon {
						border-width: @border-width-base;
 
						&::before {
							top: @spacing-12;
							right: @spacing-12;
							bottom: @spacing-12;
							left: @spacing-12;
							// TODO: Replace border-width with a new component-level token when
							// it becomes available (T374454).
							border-width: @border-width-input-radio--checked-error-focus;
							border-color: @border-color-error;
						}
					}
 
					&:active + .cdx-radio__icon {
						border-color: @background-color-error--active;
						box-shadow: none;
 
						&::before {
							border-color: @border-color-transparent;
						}
					}
				}
			}
			/* stylelint-enable no-descending-specificity */
		}
 
		/* 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-subtle;
				border-color: @border-color-disabled;
			}
 
			&:checked + .cdx-radio__icon {
				// A color token is being used here for a background color
				// because the center circle of a Radio acts more like an icon
				// than it does a background, similar to a Checkbox or ToggleSwitch.
				// Additionally, a background color token is being used on
				// a border-color because of how Radio is built and the need
				// to be consistent with Checkbox and ToggleSwitch.
				background-color: @color-disabled-emphasized;
				border-width: @border-width-input-radio--checked;
				border-color: @background-color-disabled;
			}
		}
		/* stylelint-enable no-descending-specificity */
	}
}
</style>