All files / components/input-chip InputChip.vue

100% Statements 54/54
92.59% Branches 25/27
100% Functions 10/10
100% Lines 53/53

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    2x 4x       2x     2x 2x 2x 2x           2x                                                                                                         79x 79x 79x 79x           19x   1x 1x 1x 1x   1x 1x 1x 1x     7x 7x 7x 7x   5x 5x 5x 5x   5x 5x 5x 5x     79x                           11x       2x 2x 2x 2x 2x 2x   175x 175x 175x                 19x 19x 19x   19x   3x                                     3x   138x                   2x 2x                                                                                            
<template>
	<div
		ref="rootElement"
		class="cdx-input-chip"
		:class="rootClasses"
		tabindex="0"
		role="option"
		:aria-description="chipAriaDescription"
		@keydown="onKeydown"
		@click="$emit( 'click-chip' )"
	>
		<cdx-icon
			v-if="icon"
			:icon="icon"
			size="small"
		/>
		<span class="cdx-input-chip__text">
			<!-- @slot Chip text. -->
			<slot />
		</span>
		<cdx-button
			class="cdx-input-chip__button"
			weight="quiet"
			tabindex="-1"
			aria-hidden="true"
			:disabled="disabled"
			@click.stop="$emit( 'remove-chip', 'button' )"
		>
			<cdx-icon :icon="cdxIconClose" size="x-small" />
		</cdx-button>
	</div>
</template>
 
<script lang="ts">
import { defineComponent, computed, ref, PropType } from 'vue';
import CdxButton from '../../components/button/Button.vue';
import CdxIcon from '../../components/icon/Icon.vue';
import { cdxIconClose, Icon } from '@wikimedia/codex-icons';
 
/**
 * Interactive chip used within the ChipInput.
 *
 * This component is not available for public use and should only be used inside ChipInput.
 */
export default defineComponent( {
	name: 'CdxInputChip',
	components: {
		CdxButton,
		CdxIcon
	},
	props: {
		/**
		 * `aria-description` for the 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
		},
		/**
		 * Custom icon.
		 */
		icon: {
			type: [ String, Object ] as PropType<Icon>,
			default: null
		},
		/**
		 * Whether the input chip can be removed.
		 */
		disabled: {
			type: Boolean,
			default: false
		}
	},
	expose: [
		'focus'
	],
	emits: [
		/**
		 * Emitted when a chip is removed by the user.
		 *
		 * @property {'button'|'Backspace'|'Delete'} method How the chip was removed
		 */
		'remove-chip',
		/**
		 * Emitted when a chip is clicked by the user.
		 */
		'click-chip',
		/**
		 * Emitted when the user presses the left arrow key.
		 */
		'arrow-left',
		/**
		 * Emitted when the user presses the right arrow key.
		 */
		'arrow-right'
	],
	setup( props, { emit } ) {
		const rootElement = ref<HTMLDivElement>();
		const rootClasses = computed( () => {
			return {
				'cdx-input-chip--disabled': props.disabled
			};
		} );
 
		function onKeydown( e: KeyboardEvent ) {
			switch ( e.key ) {
				case 'Enter':
					emit( 'click-chip' );
					e.preventDefault();
					e.stopPropagation();
					break;
				case 'Escape':
					rootElement.value?.blur();
					e.preventDefault();
					e.stopPropagation();
					break;
				case 'Backspace':
				case 'Delete':
					emit( 'remove-chip', e.key );
					e.preventDefault();
					e.stopPropagation();
					break;
				case 'ArrowLeft':
					emit( 'arrow-left' );
					e.preventDefault();
					e.stopPropagation();
					break;
				case 'ArrowRight':
					emit( 'arrow-right' );
					e.preventDefault();
					e.stopPropagation();
					break;
			}
		}
 
		return {
			rootElement,
			rootClasses,
			onKeydown,
			cdxIconClose
		};
	},
	methods: {
		/**
		 * Focus the chip.
		 *
		 * @public
		 */
		focus() {
			( this.$refs.rootElement as HTMLDivElement ).focus();
		}
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/common.less';
 
// TODO: create a design token.
@min-size-clear-button: 20px;
 
.cdx-input-chip {
	background-color: @background-color-interactive-subtle;
	color: @color-base;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: @spacing-25;
	max-width: @size-3200;
	border: @border-width-base @border-style-base @border-color-subtle;
	border-radius: @border-radius-pill;
	padding: 0 0 0 @spacing-50;
	font-size: @font-size-small;
	line-height: @line-height-small;
 
	&:not( .cdx-input-chip--disabled ) {
		transition-property: @transition-property-base;
		transition-duration: @transition-duration-medium;
 
		&:hover {
			background-color: @background-color-base;
			cursor: @cursor-base--hover;
		}
 
		&:focus {
			outline: @outline-base--focus;
		}
 
		&:active {
			background-color: @background-color-interactive;
			border-color: @border-color-interactive;
		}
 
		&:focus:not( :active ) {
			border-color: @border-color-progressive--focus;
			box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus;
		}
	}
 
	&--disabled {
		background-color: @background-color-disabled;
		color: @color-inverted;
		border-color: @border-color-transparent;
 
		.cdx-icon {
			color: @color-inverted;
		}
	}
 
	&__text {
		.text-overflow( @param-visible: false );
	}
 
	// The remove button is small and round, which is not supported within the Button component
	// itself, so the styles are included here.
	&__button.cdx-button {
		min-width: @min-size-clear-button;
		min-height: @min-size-clear-button;
		margin-right: @size-absolute-1;
		border-radius: @border-radius-pill;
		padding-right: @spacing-12;
		padding-left: @spacing-12;
		font-size: @font-size-x-small;
	}
}
</style>