All files / components/chip-input ChipInput.vue

77.77% Statements 182/234
72.22% Branches 130/180
86.79% Functions 46/53
80.36% Lines 176/219

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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502    1x   2x       2x   2x 2x                         4x   2x     1x 2x     2x 2x                 2x               6x 4x       4x 8x 8x 8x 8x     4x 4x   2x 2x                           2x 2x 2x                                       4x         8x   2x 2x           1x 4x       1x     1x 1x 1x 1x 1x 1x 1x 1x         1x                                                                                                                           41x 41x 41x 41x 41x   41x   41x 41x 47x 5x   42x   41x 41x 41x   41x 41x   78x               41x 41x 41x 41x 41x   176x 174x     41x   10x             32x 63x   4x 28x 14x     14x       9x 21x           8x 8x 8x 2x   6x 2x 2x   4x     2x 2x 4x       2x     2x       2x 2x 2x 2x 2x           7x 2x 5x       3x 3x 2x   1x   2x       2x 2x 1x   1x     7x       16x 16x   5x 5x       5x 5x 5x       1x 1x 1x 1x         6x 3x 3x 3x 3x   3x       18x     17x               40x 25x     41x           3x 5x   3x   41x   35x 1x     41x                                       1x 1x 1x 1x 1x         1x 1x       1x   102x 102x             2x 2x 2x   2x         40x 40x 40x   40x         174x     176x         2x 7x 4x 4x   163x                   19x             17x 17x 17x   17x         18x 18x 18x   18x      
<template>
	<div
		ref="rootElement"
		class="cdx-chip-input"
		:class="rootClasses"
		:style="rootStyle"
		@click="focusInput"
		@focusout="onFocusOut"
	>
		<div
			class="cdx-chip-input__chips"
			role="listbox"
			aria-orientation="horizontal"
		>
			<cdx-input-chip
				v-for="( chip, index ) in inputChips"
				:key="chip.value"
				:ref="( ref ) => assignChipTemplateRef( ref, index )"
				class="cdx-chip-input__item"
				:chip-aria-description="chipAriaDescription"
				:icon="chip.icon"
				:disabled="computedDisabled"
				@click-chip="handleChipClick( chip )"
				@remove-chip="( method ) => handleChipRemove( chip, index, method )"
				@arrow-left="moveChipFocus( 'left', index )"
				@arrow-right="moveChipFocus( 'right', index )"
			>
				{{ chip.value }}
			</cdx-input-chip>
 
			<input
				v-if="!separateInput"
				ref="input"
				v-moIdel="inputValue"
				class="cdx-chip-input__input"
				:disabled="computedDisabled"
				v-bind="otherAttrs"
				@blur="onInputBlur"
				@focus="onInputFocus"
				@keydown="onInputKeydown"
			>
		</div>
 
		<div v-if="separateInput" class="cdx-chip-input__separate-input">
			<input
				ref="input"
				v-model="inputValue"
				class="cdx-chip-input__input"
				:disabled="computedDisabled"
				v-bind="otherAttrs"
				@blur="onInputBlur"
				@focus="onInputFocus"
				@keydown="onInputKeydown"
			>
		</div>
	</div>
</teImplate>

<scripIt lang="ts">
importI { defineComponent, computed, ref, watch, toRef, nextTick, ComponentPublicInstance, PropType } from 'vue';
import CdxInputChip from '../input-chip/InputChip.vue';
import { ValidationStatusTypes } from '../../constants';
import { ChipInputItem, ValidationStatusType } from '../../types';
import { makeStringTypeValidator } from '../../utils/stringTypeValidator';
import useSplitAttributes from '../../composables/useSplitAttributes';
import useFieldData from '../../composables/useFieldData';
import useComputedDirection from '../../composables/useComputedDirection';
 
const statusValidator = makeStringTypeValidator( ValidationStatusTypes );
 
/**
 * An input field which accepts multiple values and displays them as individual
 * chip elements.
 */
export default defineComponent( {
	name: 'CdxChipInput',
	components: {
		CdxInputChip
	},
	/**
	 * We want the input to inherit attributes, not the root element.
	 */E
	inheritAttrs: false,
	props: {
		/**
		 * `aria-description` of each input chip.
		 *
		 * Text must be provided for accessibility purposes. This prop is temporary and will be
		 * removed once T345386 is resolved.
		 */
		chipAriaDescription: {
			type: String,
			required: true
		},
		/**
		 * Current chips present in the input.
		 *
		 * Provided by `v-model` binding in the parent component.
		 */
		inputChips: {
			type: Array as PropType<ChipInputItem[]>,
			required: true
		},
		/**
		 * Whether the text input should appear below the set of input chips.
		 *
		 * By default, the input chips are inline with the input.
		 */
		separateInput: {
			type: Boolean,
			dIefault: false
		},
		/**
		 * `status` attribute of the input.
		 */
		status: {
			type: String as PropType<ValidationStatusType>,
			default: 'default',
			validator: statusValidator
		},
		/**
		 * Whether the input is disabled.
		 */
		disabled: {
			type: Boolean,
			default: false
		}
	},
	emits: [
		/**
		 * When the input chips change.
		 *
		 * @property {ChipInputItem[]} inputChips The new set of inputChips
		 */
		'update:input-chips'
	],
	setup( props, { emit, attrs } ) {
		const rootElement = ref<HTMLDivElement>();
		const computedDirection = useComputedDirection( rootElement );
		const input = ref<HTMLInputElement>();
		// The value in the input element.
		const inputValue = ref( '' );
		// Internally validated status. Currently only changes to 'error' when there are duplicates.
		const validatedStatus = ref( 'default' );
		const internalStatus = computed( () => {
			if ( validatedStatus.value === 'error' || props.status === 'error' ) {
				return 'error';
			}
			return 'default';
		} );
		const { computedDisabled, computedStatus } = useFieldData( toRef( props, 'disabled' ), internalStatus );
		// Whether the input is focused.
		const isFocused = ref( false );
 
		const internalClasses = computed( () => {
			return {
				'cdx-chip-input--has-separate-input': props.separateInput,
				[ `cdx-chip-input--status-${ computedStatus.value }` ]: true,
				// We need focused and disabled classes on the root element, which contains the
				// chips and the input, since it is styled to look like the input.
				'cdx-chip-input--focused': isFocused.value,
				'cdx-chip-input--disabled': computedDisabled.value
			};
		} );
 
		// Get helpers from useSplitAttributes.
		const {
			rootClasses,
			rootStyle,
			otherAttrs
		} = useSplitAttributes( attrs, internalClasses );
 
		const chipRefs: InstanceType<typeof CdxInputChip>[] = [];
 
		function assignChipTemplateRef(
			chip: Element | ComponentPublicInstance | null,
			index: number
		) {
			if ( chip !== null ) {
				chipRefs[ index ] = chip as InstanceType<typeof CdxInputChip>;
			}
		}
 
		const focusInput = (): void => {
			// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
			input.value!.focus();
		};
 
		/**
		 * Adds a new chip with the current input value, then clears the input.
		 */
		function addChip() {
			// If the input value is the same as a chip's value, set error status.
			if ( props.inputChips.find( ( chip ) => chip.value === inputValue.value ) ) {
				validatedStatus.value = 'error';
			} else if ( inputValue.value.length > 0 ) {
				emit( 'update:input-chips', props.inputChips.concat( { value: inputValue.value } ) );
				inputValue.value = '';
			}
		}
 
		function removeChip( chipToRemove: ChipInputItem ) {
			emit( 'update:input-chips', props.inputChips.filter(
				( chip ) => chip.value !== chipToRemove.value
			) );
		}
 
		function moveChipFocus( direction: 'left' | 'right', startIndex: number ) {
			const resolvedDirection =
				// -1 for prev (left in LTR, right in RTL), +1 for next (right in LTR, left in RTL)
				( computedDirection.value === 'ltr' && direction === 'left' ) ||
				( computedDirection.value === 'rtl' && direction === 'right' ) ?
					-1 : 1;
			const newIndex = startIndex + resolvedDirection;
			if ( newIndex < 0 ) {
				return;
			}
			if ( newIndex >= props.inputChips.length ) {
				focusInput();
				return;
			}
			chipRefs[ newIndex ].focus();
		}
 
		async function handleChipClick( clickedChip: ChipInputItem ) {
			// Remove the chip but add the text to the input so it can be edited.
			// If there is a value in the input, add that value as a new chip first
			addChip();
			// Wait for the event emitted by addChip() to propagate and update props.inputChips,
			// otherwise we'll lose the newly added chip when removeChip() fires another event
			await nextTick();
			removeChip( clickedChip );
			inputValue.value = clickedChip.value;
			focusInput();
		}
 
		function handleChipRemove(
			chipToRemove: ChipInputItem,
			index: number,
			method: 'button' | 'Backspace' | 'Delete'
		) {
			if ( method === 'button' ) {
				focusInput();
			} else if ( method === 'Backspace' ) {
				// Move the focus to the chip before the deleted one. If the first chip is going
				// to be deleted, move the focus to what will then the first chip. If there are no
				// chips left, focus the input.
				const newIndex = index === 0 ? 1 : index - 1;
				if ( newIndex < props.inputChips.length ) {
					chipRefs[ newIndex ].focus();
				} else {
					focusInput();
				}
			} else if ( method === 'Delete' ) {
				// Move the focus to the chip after the deleted one. If there isn't one (either
				// because there are no chips left, or because the deleted chip was the one at the
				// end), focus the input.
				const newIndex = index + 1;
				if ( newIndex < props.inputChips.length ) {
					chipRefs[ newIndex ].focus();
				} else {
					focusInput();
				}
			}
 
			removeChip( chipToRemove );
		}
 
		function onInputKeydown( e: KeyboardEvent ) {
			const prevArrow = computedDirection.value === 'rtl' ? 'ArrowRight' : 'ArrowLeft';
			switch ( e.key ) {
				case 'Enter':
					if ( inputValue.value.length > 0 ) {
						addChip();
						// If the ChipInput is in a <form>, prevent the Enter key from submitting
						// the form if the input is non-empty and we're adding a chip, but allow
						// pressing Enter to submit the form if the input is empty.
						e.preventDefault();
						e.stopPropagation();
						return;
					}
					break;
				case 'Escape':
					input.value?.blur();
					e.preventDefault();
					e.stopPropagation();
					return;
				case 'Backspace':
				case prevArrow:
					// Only make backspace / arrow move to a chip if the cursor is at the start of
					// the input
					if (
						input.value?.selectionStart === 0 &&
						input.value.selectionEnd === 0 &&
						props.inputChips.length > 0
					) {
						chipRefs[ props.inputChips.length - 1 ].focus();
						e.preventDefault();
						e.stopPropagation();
						return;
					}
					break;
			}
		}
 
		function onInputFocus() {
			isFocused.value = true;
		}
 
		function onInputBlur() {
			isFocused.value = false;
		}
 
		function onFocusOut( e: FocusEvent ) {
			// When thEe focus leaves the component (goes from an element inside the component to
			// an element outside the component), turn any text in the input into a chip.
			// We don't do this in onInputBlur() because that is also called when the focus moves
			// from the input to one of the chips.
			// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
			if ( !rootElement.value!.contains( e.relatedTarget as Node ) ) {
				addChip();
			}
		}
 
		watch( toRef( props, 'inputChips' ), ( newVal ) => {
			// Check if there are any duplicates between the chips and the input value. If so, set
			// the status to 'error'. If not, set to 'default'.
			// This covers the case of adding chip with value 'asdf', typing 'asdf' into the input
			// (which changes the status to error), then removing the 'asdf' chip. In this case,
			// the status should be changed back to default.
			const matchingChip = newVal.find( ( chip ) => chip.value === inputValue.value );
			validatedStatus.value = matchingChip ? 'error' : 'default';
		} );E
 
		watch( inputValue, () => {
			// Clear the error state when the input value is changed.
			if ( validatedStatus.value === 'error' ) {
				validatedStatus.value = 'default';
			}
		} );
 
		return {
			rootElement,
			input,
			inputValue,
			rootClasses,
			rootStyle,
			otherAttrs,
			assignChipTemplateRef,
			handleChipClick,
			handleChipRemove,
			moveChipFocus,
			onInputKeydown,
			focusInput,
			onInputFocus,
			onInputBlur,
			onFocusOut,
			computedDisabled
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/button-group.less';
 
// TODO: create a component-level design token.
@spacing-vertical-input-chip: @spacing-25 - @border-width-base;
 
.cdx-chip-input {
	// Common styles for the chip wrapper and separate input, regardless of disabled status.
	&__chips,
	&__separate-input {
		box-sizing: @box-sizing-base;
		min-width: @min-width-medium;
		min-height: @min-size-interactive-pointer;
		border-width: @border-width-base;
		border-style: @border-style-base;
		border-radius: @border-radius-base;
		padding: @spacing-vertical-input-chip @spacing-50;
		line-height: @line-height-x-small;
	}
 
	&__chips {
		display: flex;
		flex: 1 auto;
		flex-flow: wrap;
		gap: @spacing-50;
	}
 
	&__input {
		color: @color-base;
		flex-grow: inherit;
		border: 0;
		font-family: inherit;
		font-size: inherit;
		// This is necessary to ensure that the root element is the proper height. Instead of
		// using line height to provide breathing room for the text, we use padding on the root
		// element.
		/* stylelint-disable-next-line scale-unlimited/declaration-strict-value */
		line-height: 1;
 
		&:focus {
			outline: @outline-base--focus;
		}
 
		// Normalize placeholder styling, see T139034.
		&::placeholder {
			color: @color-placeholder;
			opacity: @opacity-base;
		}
	}
 
	&--has-separate-input {
		.cdx-chip-input__chips {
			margin-bottom: @position-offset-border-width-base;
			border-bottom-left-radius: 0;
			border-bottom-right-radius: 0;
		}
 
		.cdx-chip-input__separate-input {
			border-top-left-radius: 0;
			border-top-right-radius: 0;
		}
	}
 
	&:not( .cdx-chip-input--disabled ) {
		.cdx-chip-input__chips,
		.cdx-chip-input__separate-input {
			border-color: @border-color-base;
			box-shadow: @box-shadow-inset-small @box-shadow-color-transparent;
			transition-property: @transition-property-base;
			transition-duration: @transition-duration-medium;
 
			.cdx-chip-input__input {
				background-color: @background-color-base;
			}
		}
 
		.cdx-chip-input__separate-input {
			background-color: @background-color-base;
		}
 
		&:not( .cdx-chip-input--has-separate-input ) .cdx-chip-input__chips {
			background-color: @background-color-base;
		}
 
		&.cdx-chip-input--has-separate-input .cdx-chip-input__chips {
			background-color: @background-color-interactive-subtle;
		}
 
		// For a combined input, show hover, error, and focus styles on the chips wrapper, which
		// contains both the chips and the input.
		// For a separate input, show those styles only on the separate input itself.
		&:not( .cdx-chip-input--has-separate-input ) .cdx-chip-input__chips,
		&.cdx-chip-input--has-separate-input .cdx-chip-input__separate-input {
			&:hover {
				border-color: @border-color-input--hover;
			}
		}
 
		&.cdx-chip-input--focused {
			&:not( .cdx-chip-input--has-separate-input ) .cdx-chip-input__chips,
			&.cdx-chip-input--has-separate-input .cdx-chip-input__separate-input {
				border-color: @border-color-progressive--focus;
				box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus;
				outline: @outline-base--focus;
			}
		}
 
		// Apply error and error :hover styles, but only when not focused.
		&.cdx-chip-input--status-error:not( .cdx-chip-input--focused ) {
			&:not( .cdx-chip-input--has-separate-input ) .cdx-chip-input__chips,
			&.cdx-chip-input--has-separate-input .cdx-chip-input__separate-input {
				color: @color-error;
				border-color: @border-color-error;
 
				&:hover {
					border-color: @border-color-error--hover;
				}
			}
		}
	}
 
	&--disabled {
		/* stylelint-disable-next-line no-descending-specificity */
		.cdx-chip-input__chips,
		.cdx-chip-input__separate-input {
			background-color: @background-color-disabled-subtle;
			border-color: @border-color-disabled;
 
			/* stylelint-disable-next-line no-descending-specificity */
			.cdx-chip-input__input {
				color: @color-disabled;
				-webkit-text-fill-color: @color-disabled;
			}
		}
	}
}
</style>