All files / components/checkbox Checkbox.vue

100% Statements 43/43
90.32% Branches 28/31
100% Functions 8/8
100% Lines 43/43

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 370 371 372 373 374 375    3x 15x       3x     3x 3x 3x 3x 3x 3x 3x 3x 3x             3x                                                                                                                                                                         69x 69x 69x 69x 69x 69x 69x 69x   69x       69x 2x         69x 69x 69x       69x 69x                     3x 3x 3x 3x 3x 3x 3x   132x 132x             10x                                               125x               4x                                           3x 3x                                                                                                                                                                                                                                                                                                                        
<template>
	<div class="cdx-checkbox" :class="rootClasses">
		<div class="cdx-checkbox__wrapper">
			<input
				:id="checkboxId"
				ref="input"
				v-model="wrappedModel"
				class="cdx-checkbox__input"
				type="checkbox"
				:aria-describedby="( $slots.description &&
					$slots.description().length > 0 ) ? descriptionId : undefined"
				:value="inputValue"
				:name="name"
				:disabled="computedDisabled"
				:indeterminate.prop="indeterminate"
			>
			<span class="cdx-checkbox__icon" />
			<!-- Only render a Label component if label text has been provided.
			This component can also supply a description to the Checkbox if content
			is provided in the description slot. -->
			<cdx-label
				v-if="$slots.default && $slots.default().length"
				class="cdx-checkbox__label"
				:input-id="checkboxId"
				:description-id="( $slots.description &&
					$slots.description().length > 0 ) ? descriptionId : undefined"
				:disabled="computedDisabled"
				:visually-hidden="hideLabel"
			>
				<!-- @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-checkbox__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 can appear by itself or in a multiselect group.
 *
 * Checkboxes can be selected, unselected or in an indeterminate state.
 *
 */
export default defineComponent( {
	name: 'CdxCheckbox',
	components: { CdxLabel },
	props: {
		/**
		 * Value of the checkbox or checkbox group.
		 *
		 * Provided by `v-model` binding in the parent component.
		 */
		modelValue: {
			type: [ Boolean, Array ] as PropType<boolean | ( string|number )[]>,
			default: false
		},
		/**
		 * 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,
			default: null
		},
		/**
		 * Whether the disabled attribute should be added to the input.
		 */
		disabled: {
			type: Boolean,
			default: false
		},
		/**
		 * Whether the indeterminate visual state should be displayed.
		 *
		 * This is unrelated to the value provided by `v-model`, and the indeterminate visual state
		 * will override the checked or unchecked visual state.
		 */
		indeterminate: {
			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
		},
		/**
		 * Whether the label should be visually hidden.
		 *
		 * When true, the label will remain accessible to assistive technology.
		 */
		hideLabel: {
			type: Boolean,
			default: false
		},
		/**
		 * Validation status of the Checkbox.
		 */
		status: {
			type: String as PropType<ValidationStatusType>,
			default: 'default',
			validator: statusValidator
		}
	},
	emits: [
		/**
		 * Emitted when modelValue changes.
		 *
		 * @property {boolean | string[] | number[]} modelValue The new model value
		 */
		'update:modelValue'
	],
	setup( props, { emit, slots, attrs } ) {
		useLabelChecker( slots.default?.(), attrs, 'CdxCheckbox' );
 
		const {
			computedDisabled,
			computedStatus
		} = useFieldData(
			toRef( props, 'disabled' ),
			toRef( props, 'status' )
		);
 
		const rootClasses = computed( (): Record<string, boolean> => {
			return {
				'cdx-checkbox--inline': props.inline,
				[ `cdx-checkbox--status-${ computedStatus.value }` ]: true
			};
		} );
 
		const customInputClasses = computed( (): Record<string, boolean> => {
			return {
				'cdx-checkbox__custom-input--inline': props.inline
			};
		} );
 
		// Declare template ref.
		const input = ref<HTMLInputElement>();
		const checkboxId = useGeneratedId( 'checkbox' );
		const descriptionId = useGeneratedId( 'description' );
 
		// 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,
			checkboxId,
			descriptionId,
			wrappedModel,
			customInputClasses
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/binary-input.less';
 
.cdx-checkbox {
	.cdx-mixin-binary-input();

	// Custom-styled checkbox that's visible to the user. More styles are added below depending on
	// the state of the input element.
	&__icon {
		background-size: 0 0;
		border-radius: @border-radius-base;
	}
 
	// HTML `<input type="checkbox">`.
	// Based on the HTML attributes of the checkbox input, we can change the style of the adjacent
	// `span`, which will look like a custom-styled checkbox.
	&__input {
		// Indeterminate state.
		&:indeterminate + .cdx-checkbox__icon::before {
			content: ' ';
			background-color: @background-color-base-fixed;
			position: absolute;
			// Center indeterminate line vertically with negative half pixel.
			top: calc( @size-half - ( @size-absolute-1 / 2 ) );
			// Center horizontally.
			right: calc( @spacing-25 - @border-width-base );
			left: calc( @spacing-25 - @border-width-base );
			/* stylelint-disable-next-line scale-unlimited/declaration-strict-value */
			height: ( @size-absolute-1 * 2 );
		}
 
		// Checked state whether or not the input is enabled or disabled.
		&:checked:not( :indeterminate ) + .cdx-checkbox__icon::before {
			content: ' ';
			background-image: @background-image-input-checkbox--checked;
			background-position: @background-position-base;
			background-repeat: no-repeat;
			// This must have two values to match `background-size: 0 0` above,
			// otherwise the transition does not work (at least in Chrome).
			background-size: @size-icon-small @size-icon-small;
			position: absolute;
			// Use `@size-full` as `@size-input-binary` would lead to offset by
			// `@border-width-base`.
			width: @size-full;
			height: @size-full;
		}
 
		/* stylelint-disable no-descending-specificity */
		&:enabled {
			& + .cdx-checkbox__icon {
				background-color: @background-color-base;
				border-color: @border-color-interactive;
			}
 
			&:hover + .cdx-checkbox__icon {
				background-color: @background-color-interactive-subtle--hover;
				border-color: @border-color-interactive--hover;
			}
 
			&:focus:not( :active ) + .cdx-checkbox__icon {
				border-color: @border-color-progressive--focus;
				box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus;
				// In Windows high contrast mode the outline becomes visible.
				outline: @outline-base--focus;
			}
 
			&:active + .cdx-checkbox__icon {
				background-color: @background-color-interactive-subtle--active;
				border-color: @border-color-interactive--active;
			}
 
			&:checked,
			&:indeterminate {
				& + .cdx-checkbox__icon {
					background-color: @background-color-progressive;
					border-color: @border-color-transparent;
				}
 
				&:hover + .cdx-checkbox__icon {
					background-color: @background-color-progressive--hover;
				}
 
				&:focus:not( :active ):not( :hover ) + .cdx-checkbox__icon {
					background-color: @background-color-progressive;
				}
 
				&:focus:not( :active ) + .cdx-checkbox__icon {
					// Make `box-shadow` feature a `1px` White inset outline with a
					// value combination.
					/* stylelint-disable-next-line stylistic/declaration-colon-newline-after,
						stylistic/value-list-comma-newline-after */
					box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus,
						@box-shadow-inset-medium @box-shadow-color-inverted;
				}
 
				&:active + .cdx-checkbox__icon {
					background-color: @background-color-progressive--active;
				}
			}
 
			.cdx-checkbox--status-error & {
				& ~ .cdx-checkbox__label {
					color: @color-error;
				}
 
				& + .cdx-checkbox__icon {
					background-color: @background-color-error-subtle;
					border-color: @border-color-error;
				}
 
				&:hover + .cdx-checkbox__icon {
					background-color: @background-color-error-subtle--hover;
					border-color: @border-color-error--hover;
				}
 
				&:focus + .cdx-checkbox__icon {
					border-color: @border-color-progressive--focus;
				}
 
				&:active + .cdx-checkbox__icon {
					background-color: @background-color-error-subtle--active;
					border-color: @border-color-error--active;
				}
 
				&:checked,
				&:indeterminate {
					& + .cdx-checkbox__icon {
						background-color: @background-color-error;
						border-color: @border-color-transparent;
					}
 
					&:hover + .cdx-checkbox__icon {
						background-color: @background-color-error--hover;
					}
 
					&:active + .cdx-checkbox__icon {
						background-color: @background-color-error--active;
					}
 
					&:focus:not( :active ) + .cdx-checkbox__icon {
						background-color: @background-color-error;
						border-color: @border-color-progressive--focus;
					}
				}
			}
		}
 
		&:disabled {
			& + .cdx-checkbox__icon {
				background-color: @background-color-disabled-subtle;
				border-color: @border-color-disabled;
			}
 
			&:checked,
			&:indeterminate {
				& + .cdx-checkbox__icon {
					background-color: @background-color-disabled;
					border-color: @border-color-transparent;
				}
			}
 
			&:indeterminate + .cdx-checkbox__icon::before {
				background-color: @color-disabled-emphasized;
			}
 
			// DEPRECATED: Support CSS-only components that don't set the `cdx-label` class on the
			// label (T353885)
			& ~ .cdx-checkbox__label,
			& ~ .cdx-checkbox__label.cdx-label {
				color: @color-disabled;
			}
		}
		/* stylelint-enable no-descending-specificity */
	}
}
</style>