All files / components/select Select.vue

95.37% Statements 103/108
85% Branches 68/80
95.65% Functions 22/23
97.97% Lines 97/99

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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450    2x 26x 26x 26x     26x   2x 14x       2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x                                                                                                     26x                                                                     26x 26x   26x 26x 26x 26x 26x   26x 26x 26x 26x 26x     26x 26x 27x 91x       26x   27x   26x   27x 1x   26x 1x     25x   26x   39x                   26x 26x 26x 26x         26x   26x 26x 26x 26x   26x   65x     1x       4x 1x   3x 3x       8x 1x   7x       26x 26x                                             2x 2x 2x 2x 2x 2x   42x 42x 42x                                     4x 4x 4x   4x         1x 1x 1x   1x         8x 8x 8x   8x             42x                                     3x   8x           186x 186x                 2x 2x                                                                                                                                                                                                                                                    
<template>
	<div
		class="cdx-select-vue"
		:class="rootClasses"
		:style="rootStyle"
		E:aria-disabled="computedDisabled"
	>
		<div
			:id="computedHandleId"
			ref="handle"
			class="cdx-select-vue__handle"
			v-bind="otherAttrsMinusId"
			tabindex="0"
			role="combobox"
			:aria-controls="menuId"
			:aria-activedescendant="highlightedId"
			:aria-expanded="expanded"
			:aria-describedby="descriptionId"
			@click="onClick"
			@blur="onBlur"
			@keydown="onKeydown"
		>
			<!--
				@slot Display of the current selection or default label
				@binding {MenuItemData|undefined} selected-menu-item The currently selected menu
				item
				@binding {string} default-label The default label, provided via a prop
			-->
			<slot
				name="label"
				:selected-menu-item="selectedMenuItem"
				:default-label="defaultLabel"
			>
				{{ currentLabel }}
			</slot>
			<cdx-icon
				v-if="startIcon"
				:icon="startIcon"
				class="cdx-select-vue__start-icon"
			/>
			<cdx-icon
				:icon="cdxIconExpand"
				class="cdx-select-vue__indicator"
			/>
		</div>
 
		<cdx-menu
			:id="menuId"
			ref="menu"
			v-model:selected="modelWrapper"
			v-model:expanded="expanded"
			:menu-items="menuItems"
			v-bind="menuConfig"
			@load-more="$emit( 'load-more' )"
		>
			<template #default="{ menuItem }">
				<!--
					@slot Display of an individual item in the menu
					@binding {MenuItemData} menu-item The current menu item
				-->
				<slot name="menu-item" :menu-item="menuItem" />
			</template>
		</cdx-menu>
	</div>
</template>
 
<script lang="ts">
import {
	PropType,
	defineComponent,
	computed,
	ref,
	toRef,
	inject
} from 'vue';
import { Icon, cdxIconExpand } from '@wikimedia/codex-icons';
 
import CdxIcon from '../icon/Icon.vue';
import CdxMenu from '../menu/Menu.vue';
 
import useGeneratedId from '../../composables/useGeneratedId';
import useModelWrapper from '../../composables/useModelWrapper';
import useFieldData from '../../composables/useFieldData';
import useSplitAttributes from '../../composables/useSplitAttributes';
import useFloatingMenu from '../../composables/useFloatingMenu';
 
import { MenuItemData, MenuConfig, ValidationStatusType } from '../../types';
import { ValidationStatusTypes, FieldDescriptionIdKey } from '../../constants';
import { makeStringTypeValidator } from '../../utils/stringTypeValidator';
const statusValidator = makeStringTypeValidator( ValidationStatusTypes );
 
/**
 * An input with a dropdown menu of predefined, selectable items.
 */
export default defineComponent( {
	name: 'CdxSelect',
	components: {
		CdxIcon,
		CdxMenu
	},
	/**
	 * We want the select handle to inherit attributes, not the root element.
	 */
	inheritAttrs: false,
 
	props: {
		/**
		 * Menu items. See the MenuItemData type.
		 */
		menuItems: {
			type: Array as PropType<MenuItemData[]>,
			required: true
		},
		/**
		 * Value of the current selection.
		 *
		 * Must be bound with `v-model:selected`.
		 *
		 * The property should be initialized to `null` rather than using a falsy value.
		 */
		selected: {
			type: [ String, Number, null ] as PropType<string|number|null>,
			required: true
		},
		/**
		 * Label to display when no selection has been made.
		 */
		defaultLabel: {
			type: String,
			default: ''
		},
 
		/**
		 * Whether the dropdown is disabled.
		 */
		disabled: {
			type: Boolean,
			default: false
		},
 
		/**
		 * Configuration for various menu features. All properties default to false.
		 *
		 * See the MenuConfig type.
		 */
		menuConfig: {
			type: Object as PropType<MenuConfig>,
			default: () => {
				return {} as MenuConfig;
			}
		},
 
		/**
		 * An icon at the start of the select element
		 * displayed when no selection has been made.
		 */
		defaultIcon: {
			type: [ String, Object ] as PropType<Icon | undefined>,
			default: undefined
		},
 
		/**
		 * `status` attribute of the input.
		 */
		status: {
			type: String as PropType<ValidationStatusType>,
			default: 'default',
			validator: statusValidator
		}
	},
 
	emits: [
		/**
		 * When the selection value changes.
		 *
		 * @property {string | number | null} modelValue The new model value
		 */
		'update:selected',
		/**
		 * When the user scrolls towards the bottom of the menu.
		 *
		 * If it is possible to add or load more menu items, then now would be a good moment
		 * so that the user can experience infinite scrolling.
		 */
		'load-more'
	],
 
	setup( props, { emit, attrs } ) {
		// Set up basic reactive values and template refs.
		const handle = ref<HTMLDivElement>();
		const menu = ref<InstanceType<typeof CdxMenu>>();
		const descriptionId = inject( FieldDescriptionIdKey, undefined );
		const menuId = useGeneratedId( 'select-menu' );
		const expanded = ref( false );
 
		// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
		const handleId = ( attrs.id as string|undefined ) || useGeneratedId( 'select-handle' );
		const {
			computedDisabled,
			computedStatus,
			computedInputId: computedHandleId
		} = useFieldData(
			toRef( props, 'disabled' ),
			toRef( props, 'status' ),
			handleId
		);
 
		// The value of the component's current selection is tracked in the parent as a v-model
		// binding.
		const modelWrapper = useModelWrapper( toRef( props, 'selected' ), emit, 'update:selected' );
 
		const selectedMenuItem = computed( () =>
			props.menuItems.find( ( menuItem ) => menuItem.value === props.selected )
		);
 
		// Set up the label that should display in the handle.
		const currentLabel = computed( () => {
			return selectedMenuItem.value ?
				selectedMenuItem.value.label ?? selectedMenuItem.value.value :
				props.defaultLabel;
		} );
 
		const startIcon = computed( () => {
			if ( props.defaultIcon && !selectedMenuItem.value ) {
				return props.defaultIcon;
			// The selected item includes an icon
			} else if ( selectedMenuItem.value?.icon ) {
				return selectedMenuItem.value.icon;
			}
			// Explicit return is needed to satisfy vue/return-in-computed-property
			return undefined;
		} );
 
		const internalClasses = computed( (): Record<string, boolean> => {
			return {
				'cdx-select-vue--enabled': !computedDisabled.value,
				'cdx-select-vue--disabled': computedDisabled.value,
				'cdx-select-vue--expanded': expanded.value,
				'cdx-select-vue--value-selected': !!selectedMenuItem.value,
				'cdx-select-vue--no-selections': !selectedMenuItem.value,
				'cdx-select-vue--has-start-icon': !!startIcon.value,
				[ `cdx-select-vue--status-${ computedStatus.value }` ]: true
			};
		} );
 
		// Get helpers from useSplitAttributes.
		const {
			rootClasses,
			rootStyle,
			otherAttrs
		} = useSplitAttributes( attrs, internalClasses );
 
		// Normally, we use v-bind to bind otherAttrs to the appropriate element. In this case, we
		// do not want to include the id attribute, since we're using the computed ID via the
		// useComputedId composable.
		// This removes the ID and stores all other attributes in otherAttrsMinusId.
		const otherAttrsMinusId = computed( () => {
			// eslint-disable-next-line @typescript-eslint/no-unused-vars
			const { id, ...everythingElse } = otherAttrs.value;
			return everythingElse;
		} );
 
		const highlightedId = computed( () => menu.value?.getHighlightedMenuItem()?.id );
 
		function onBlur(): void {
			expanded.value = false;
		}
 
		function onClick(): void {
			if ( computedDisabled.value ) {
				return;
			}
 
			expanded.value = !expanded.value;
			handle.value?.focus();
		}
 
		function onKeydown( e: KeyboardEvent ) {
			if ( computedDisabled.value ) {
				return;
			}
			menu.value?.delegateKeyNavigation( e, { characterNavigation: true } );
		}
 
		useFloatingMenu( handle, menu );
 
		return {
			handle,
			menu,
			computedHandleId,
			descriptionId,
			menuId,
			modelWrapper,
			selectedMenuItem,
			highlightedId,
			expanded,
			computedDisabled,
			onBlur,
			currentLabel,
			rootClasses,
			rootStyle,
			otherAttrsMinusId,
			onClick,
			onKeydown,
			startIcon,
			cdxIconExpand
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/icon-alignment.less';
@import ( reference ) '../../themes/mixins/select.less';
@import ( reference ) '../../themes/mixins/public/css-icon.less';
 
// HACK: We can't use the CSS icon mixin for the select handle's arrow icon, because it uses
// mask-image, which applies to the background of the entire element. We can't do that with the
// entire <select> element, and pseudo-elements can't be used for the icon either.
// Instead, we really need background rules. Unfortunately, we can't rely on our color tokens here,
// since they will soon output CSS custom properties, which won't work for the SVG's fill property.
// TODO: Make this work in night mode.
@color-base-hex: #202122;
@color-disabled-hex: #72777d;
@icon-expand-svg-content: extract( @cdx-icon-expand, 1 );
 
.get-select-icon-background-image( @param-select-icon-color ) {
	@escaped-icon-color: escape( @param-select-icon-color );
	background-image: url( 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20" viewBox="0 0 20 20" fill="@{escaped-icon-color}">@{icon-expand-svg-content}</svg>' );
}
 
// CSS-only and Vue implementations are too divergent to combine, so they are included separately.
// This is the CSS-only version, which is a `<select>` element.
.cdx-select {
	.cdx-select__handle();
	/* stylelint-disable-next-line plugin/no-unsupported-browser-features */
	appearance: none;
	/* stylelint-disable-next-line scale-unlimited/declaration-strict-value */
	background-position: center right @spacing-75;
	background-repeat: no-repeat;
	background-size: calc( ~'max( @{size-icon-x-small}, @{min-size-icon-x-small} )' );
 
	&:disabled {
		.cdx-select__handle--disabled();
		.get-select-icon-background-image( @color-disabled-hex );
 
		// Support: Chrome, which sets an opacity less than 1 for disabled select elements.
		opacity: @opacity-base;
	}
 
	&:enabled {
		.cdx-select__handle--enabled();
		.get-select-icon-background-image( @color-base-hex );
	}
}
 
// This is the Vue implementation.
.cdx-select-vue {
	display: inline-block;
	position: relative;
 
	&__handle {
		.cdx-select__handle();
		position: relative;
		width: @size-full;
	}
 
	&--has-start-icon .cdx-select-vue__handle {
		.cdx-mixin-icon-wrapper-padding( start );
	}
 
	&__start-icon.cdx-icon {
		.cdx-mixin-icon( start );
	}
 
	&__indicator.cdx-icon {
		color: @color-base;
		.cdx-mixin-icon(
			end,
			@min-size-icon-x-small,
			@size-icon-x-small,
			@spacing-75
		);
	}
 
	&--enabled {
		.cdx-select-vue__handle {
			.cdx-select__handle--enabled();
 
			&:hover {
				.cdx-select-vue__indicator {
					color: @color-base--hover;
				}
			}
		}
 
		/* Expanded Menu only happens when enabled. */
		&.cdx-select-vue--expanded {
			.cdx-select-vue__handle {
				background-color: @background-color-base;
 
				/* stylelint-disable-next-line max-nesting-depth */
				.cdx-select-vue__indicator {
					color: @color-base;
				}
			}
		}
	}
 
	/* stylelint-disable no-descending-specificity */
	&--disabled {
		.cdx-select-vue__handle {
			.cdx-select__handle--disabled();
			// Don't implement coined effect on text-shadow from OOUI.
			// This has never gone through design review and was a hack to increase
			// color contrast.
			// text-shadow: @text-shadow-base--disabled;
			cursor: @cursor-base--disabled;
		}
 
		.cdx-select-vue__indicator,
		.cdx-select-vue__start-icon {
			color: @color-disabled;
		}
	}
	/* stylelint-enable no-descending-specificity */
 
	&--status-error&--enabled {
		.cdx-select-vue__handle {
			border-color: @border-color-error;
 
			&:hover {
				border-color: @border-color-error--hover;
			}
 
			&:focus {
				border-color: @border-color-progressive--focus;
			}
		}
	}
 
	// Overrides when used within a Dialog component
	.cdx-dialog & {
		// The menu is positioned relative to the dialog backdrop, not the triggering element.
		position: static;
	}
}
</style>