All files / components/text-input TextInput.vue

95.23% Statements 100/105
85.24% Branches 52/61
91.3% Functions 21/23
95.74% Lines 90/94

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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525    6x 198x 959x 198x     198x   6x 24x       6x     6x 6x 6x 6x 6x 6x 6x 6x 6x 6x       6x                                                                                                                                                                                                                                           142x 142x     142x 142x 142x 142x 142x 142x       142x 142x 143x   142x   143x             142x 142x 142x 142x         142x   198x 198x 198x 198x   142x 171x       142x 3x 3x   142x         23x     23x     142x 11x   142x 7x   142x 33x   142x 7x   142x                                                         5x 5x                         6x 6x 6x 6x 6x   220x 220x               11x                   11x 11x 11x   11x         7x 7x 7x   7x         33x 33x 33x   33x         7x 7x 7x   7x         23x 23x 23x   23x                                                           6x 6x                                                                                                                                                                                                                                                                                                                                      
<template>
	<div
		class="cdx-text-input"
		:class="rootClasses"
		:style="rootStyle"
	>E
		<!-- size="1" is to prevent the browser from setting an implicit min-width -->
		<input
			:id="computedInputId"
			ref="input"
			v-model="wrappedModel"
			class="cdx-text-input__input"
			:class="inputClasses"
			v-bind="otherAttrsMinusId"
			:type="inputType"
			:aria-describedby="descriptionId"
			:disabled="computedDisabled"
			size="1"
			@input="onInput"
			@change="onChange"
			@focus="onFocus"
			@blur="onBlur"
			@keydown="onKeydown"
		>
		<cdx-icon
			v-if="startIcon"
			:icon="startIcon"
			class="cdx-text-input__icon-vue cdx-text-input__start-icon"
		/>
		<cdx-icon
			v-if="endIcon"
			:icon="endIcon"
			class="cdx-text-input__icon-vue cdx-text-input__end-icon"
		/>
		<cdx-icon
			v-if="isClearable"
			:icon="cdxIconClear"
			class="cdx-text-input__icon-vue cdx-text-input__clear-icon"
			@mousedown.prevent
			@click="onClear"
		/>
	</div>
</template>
 
<script lang="ts">
import { defineComponent, PropType, toRef, computed, inject } from 'vue';
import { Icon, cdxIconClear } from '@wikimedia/codex-icons';
import CdxIcon from '../icon/Icon.vue';
import { TextInputTypes, ValidationStatusTypes, FieldDescriptionIdKey } from '../../constants';
import { TextInputType, ValidationStatusType } from '../../types';
import { makeStringTypeValidator } from '../../utils/stringTypeValidator';
import useModelWrapper from '../../composables/useModelWrapper';
import useSplitAttributes from '../../composables/useSplitAttributes';
import useFieldData from '../../composables/useFieldData';
 
const textInputTypeValidator = makeStringTypeValidator( TextInputTypes );
const statusValidator = makeStringTypeValidator( ValidationStatusTypes );
 
/**
 * A form element that accepts a single line of text input from the user.
 */
export default defineComponent( {
	name: 'CdxTextInput',
	components: { CdxIcon },
	/**
	 * We want the input to inherit attributes, not the root element.
	 */
	inheritAttrs: false,
 
	expose: [
		'focus',
		'blur'
	],
 
	props: {
		/**
		 * Current value of the input.
		 *
		 * Provided by `v-model` binding in the parent component.
		 */
		modelValue: {
			type: [ String, Number ],
			default: ''
		},
		/**
		 * `type` attribute of the input.
		 *
		 * @values 'text', 'search', 'number', 'email', 'password', 'tel', 'url',
		 * 'week', 'month', 'date', 'datetime-local', 'time'
		 */
		inputType: {
			type: String as PropType<TextInputType>,
			default: 'text',
			validator: textInputTypeValidator
		},
		/**
		 * `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
		},
		/**
		 * An icon at the start of the input element. Similar to a `::before` pseudo-element.
		 */
		startIcon: {
			type: [ String, Object ] as PropType<Icon | undefined>,
			default: undefined
		},
		/**
		 * An icon at the end of the input element. Similar to an `::after` pseudo-element.
		 */
		endIcon: {
			type: [ String, Object ] as PropType<Icon | undefined>,
			default: undefined
		},
		/**
		 * Add a clear button at the end of the input element.
		 *
		 * When the clear button is pressed, the input's value is set to an empty string.
		 * The clear button is displayed when input text is present.
		 */
		clearable: {
			type: Boolean,
			default: false
		}
	},
	emits: [
		/**
		 * When the input value changes
		 *
		 * @property {string | number} modelValue The new model value
		 */
		'update:modelValue',
		/**
		 * When the user presses a key.
		 *
		 * This event is not emitted when the user presses the Home or End key (T314728),
		 * but is emitted for Ctrl/Cmd+Home and Ctrl/Cmd+End.
		 *
		 * @property {KeyboardEvent}
		 */
		'keydown',
		/**
		 * When the input value changes via direct use of the input
		 *
		 * @property {InputEvent} event
		 */
		'input',
		/**
		 * When an input value change is committed by the user (e.g. on blur)
		 *
		 * @property {Event} event
		 */
		'change',
		/**
		 * When the input comes into focus
		 *
		 * @property {FocusEvent} event
		 */
		'focus',
		/**
		 * When the input loses focus
		 *
		 * @property {FocusEvent} event
		 */
		'blur',
		/**
		 * When the input value is cleared through the use of the clear button
		 *
		 * @property {MouseEvent} event
		 */
		'clear'
	],
	setup( props, { emit, attrs } ) {
		// If there is a parent Field component, it may be providing some data to this component.
		// Grab computed values of each relevant property.
		const idAttribute = attrs.id as string|undefined;
		const {
			computedDisabled,
			computedStatus,
			computedInputId
		} = useFieldData(
			toRef( props, 'disabled' ),
			toRef( props, 'status' ),
			idAttribute
		);
		const descriptionId = inject( FieldDescriptionIdKey, undefined );
 
		// 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 );
 
		const isClearable = computed( () => {
			return props.clearable && !!wrappedModel.value && !computedDisabled.value;
		} );I

		const internalClasses = computed( () => {
			return {
				'cdx-text-input--has-start-icon': !!props.startIcon,
				'cdx-text-input--has-end-icon': !!props.endIcon,
				'cdx-text-input--clearable': isClearable.value,
				[ `cdx-text-input--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 inputClasses = computed( () => {
			return {
				'cdx-text-input__input--has-value': !!wrappedModel.value
			};
		} );
 
		const onClear = ( event: MouseEvent ) => {
			wrappedModel.value = '';
			emit( 'clear', event );
		};
 
		const onKeydown = ( event: KeyboardEvent ) => {
			// Hide keydown events for Home and End (but not Ctrl/Cmd+Home/End) from the parent
			// This avoids a pitfall when delegating TextInput events to a Menu component, where
			// we want Home/End to move the cursor in the TextInput rather than move the highlight
			// in the Menu (T314728)
			if ( ( event.key === 'Home' || event.key === 'End' ) && !event.ctrlKey && !event.metaKey ) {
				return;
			}
			emit( 'keydown', event );
		};
 
		// Emit other events to the parent in case they're needed.
		const onInput = ( event: Event ) => {
			emit( 'input', event );
		};
		const onChange = ( event: Event ) => {
			emit( 'change', event );
		};
		const onFocus = ( event: FocusEvent ) => {
			emit( 'focus', event );
		};
		const onBlur = ( event: FocusEvent ) => {
			emit( 'blur', event );
		};
 
		return {
			computedInputId,
			descriptionId,
			wrappedModel,
			isClearable,
			rootClasses,
			rootStyle,
			otherAttrsMinusId,
			inputClasses,
			computedDisabled,
			onClear,
			onInput,
			onChange,
			onKeydown,
			onFocus,
			onBlur,
			cdxIconClear
		};
	},
 
	// Public methods
	// These must be in the methods block, not in the setup function, otherwise their documentation
	// won't be picked up by vue-docgen
	methods: {
		/**
		 * Focus the component's input element.
		 *
		 * @public
		 */
		focus(): void {
			const input = this.$refs.input as HTMLInputElement;
			input.focus();
		},
 
		/**
		 * Blur the component's input element.
		 *
		 * @public
		 */
		blur(): void {
			const input = this.$refs.input as HTMLInputElement;
			input.blur();
		}
	}
} );
</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/public/css-icon.less';
 
.cdx-text-input {
	// For proper positioning of icons and slotted elements.
	position: relative;
	box-sizing: @box-sizing-base;
	// min-width is set here instead of in the <input> so that the input's width is consistent
	// regardless of whether the clear button and start/end icon are present
	min-width: @min-width-medium;
	// Set the border-radius on the root element rather than the <input>, so that the focus outline
	// will also have rounded or straight corners to match the input's corners.
	border-radius: @border-radius-base;
	overflow: hidden;
 
	// Added double ampersand to add specificity to Vue and CSS start icons
	& &__start-icon {
		// Border width is included here and for other icon positions because the icon position will
		// be offset from the border, not inside the border, so we need to include its width in the
		// offset value.
		.cdx-mixin-icon( start, @param-external-padding: @spacing-50 + @border-width-base );
	}
 
	// Ensure CSS end icon is size small.
	&__icon&__end-icon {
		.cdx-mixin-css-icon-size( @size-icon-small );
		.cdx-mixin-css-icon-background( @size-icon-small );
	}
 
	// Added double ampersand to add specificity to Vue and CSS end icons
	&__clear-icon.cdx-icon,
	& &__end-icon {
		.cdx-mixin-icon(
			end,
			@min-size-icon-small,
			@size-icon-small,
			@spacing-50 + @border-width-base
		);
	}
 
	&__clear-icon.cdx-icon {
		// The clear icon result in a pointer cursor on hover.
		&:hover {
			cursor: @cursor-base--hover;
		}
 
		// Move the clear icon farther left when it appears next to an end icon.
		.cdx-text-input__end-icon.cdx-icon + & {
			right: calc( @spacing-horizontal-input-text-two-end-icons + @border-width-base );
		}
	}
 
	&__input {
		display: block;
		box-sizing: @box-sizing-base;
		min-height: @min-size-interactive-pointer;
		width: @size-full;
		margin: 0;
		border-width: @border-width-base;
		border-style: @border-style-base;
		// Override border-radius styles from User Agent Stylesheet in iOS Safari
		border-radius: @border-radius-sharp;
		padding: @spacing-25 @spacing-50;
		font-family: inherit;
		font-size: inherit;
		line-height: @line-height-xx-small;
 
		&:enabled {
			background-color: @background-color-base;
			color: @color-base;
			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-text-input__icon-vue {
				color: @color-placeholder;
			}
 
			~ .cdx-text-input__icon {
				opacity: @opacity-icon-placeholder;
			}
 
			&:hover {
				border-color: @border-color-input--hover;
			}
 
			// Show darker icons when the input is focused or has value.
			// Note that the "has value" part doesn't apply to CSS-only text inputs.
			&:focus,
			&.cdx-text-input__input--has-value {
				~ .cdx-text-input__icon-vue {
					color: @color-base;
				}
 
				~ .cdx-text-input__icon {
					opacity: @opacity-base;
				}
			}
 
			&:focus {
				border-color: @border-color-progressive--focus;
				box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus;
				outline: @outline-base--focus;
			}
 
			&:read-only {
				background-color: @background-color-interactive-subtle;
			}
		}
 
		/* stylelint-disable-next-line no-descending-specificity */
		&:disabled {
			background-color: @background-color-disabled-subtle;
			color: @color-disabled;
			-webkit-text-fill-color: @color-disabled;
			border-color: @border-color-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;
 
			~ .cdx-text-input__icon-vue {
				color: @color-disabled;
				pointer-events: none;
			}
 
			~ .cdx-text-input__icon {
				opacity: @opacity-icon-base--disabled;
			}
		}
 
		// Normalize placeholder styling, see T139034.
		&::placeholder {
			color: @color-placeholder;
			opacity: @opacity-base;
		}
 
		// Support IE 10-11, and Edge 12+: Hide proprietary pseudo-element.
		// See https://developer.mozilla.org/en-US/docs/Web/CSS/::-ms-clear
		&::-ms-clear {
			display: none;
		}
 
		&[ type='search' ] {
			// Support Safari/iOS: Normalize by applying `none`,
			// Chrome would accept `textfield` as well.
			/* stylelint-disable plugin/no-unsupported-browser-features */
			/* autoprefixer: ignore next */
			-webkit-appearance: none;
			// Support Firefox.
			/* autoprefixer: ignore next */
			-moz-appearance: textfield;
			/* stylelint-enable plugin/no-unsupported-browser-features */
 
			// Support: Safari, Chrome (Blink).
			&::-webkit-search-decoration,
			&::-webkit-search-cancel-button {
				display: none;
			}
		}
	}
 
	&--has-start-icon {
		.cdx-text-input__input {
			.cdx-mixin-icon-wrapper-padding( start, @spacing-50 );
		}
	}
 
	// Either with an end icon or clearable.
	&--has-end-icon,
	&--clearable {
		.cdx-text-input__input {
			.cdx-mixin-icon-wrapper-padding(
				end,
				@spacing-50,
				@size-icon-small
			);
		}
	}
 
	// Both with an end icon and clearable.
	&--has-end-icon&--clearable {
		.cdx-text-input__input {
			.cdx-mixin-icon-wrapper-padding(
				end,
				@spacing-horizontal-input-text-two-end-icons,
				@size-icon-small
			);
		}
	}
 
	&--status-error {
		.cdx-text-input__input:enabled {
			border-color: @border-color-error;
 
			&:hover {
				border-color: @border-color-error--hover;
			}
 
			&:focus {
				border-color: @border-color-progressive--focus;
			}
		}
	}
}
</style>