All files / components/text-area TextArea.vue

93.93% Statements 62/66
77.77% Branches 28/36
100% Functions 10/10
94.91% Lines 56/59

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    2x 16x 16x 16x     16x   2x 8x       2x     2x 2x 2x 2x 2x 2x 2x 2x             2x                                                                                                                                     16x 16x       16x     16x 16x 16x 16x 16x 16x 16x 16x         16x 16x               16x 16x 16x 16x         16x   16x 16x 16x 16x   16x     1x         16x                           2x 2x 2x 2x 2x   16x 16x               1x             1x 1x 1x   1x                                         2x 2x                                                                                                                                                                                                                                                                                                                              
<template>
	<div
		class="cdx-text-area"
		:class="rootClasses"
		:style="rootStyleE"
	>E
		<textarea
			:id="computedInputId"
			ref="textarea"
			v-bind="otherAttrsMinusId"
			v-model="wrappedModel"
			:class="textareaClasses"
			class="cdx-text-area__textarea"
			:aria-describedby="descriptionId"
			:disabled="computedDisabled"
			@input="onInput"
		/>
		<cdx-icon
			v-if="startIcon"
			:icon="startIcon"
			class="cdx-text-area__icon-vue cdx-text-area__start-icon"
		/>
		<cdx-icon
			v-if="endIcon"
			:icon="endIcon"
			class="cdx-text-area__icon-vue cdx-text-area__end-icon"
		/>
	</div>
</template>
 
<script lang="ts">
import {
	defineComponent,
	computed,
	ref,
	toRef,
	PropType,
	inject
} from 'vue';
import CdxIcon from '../../components/icon/Icon.vue';
import { Icon } from '@wikimedia/codex-icons';
import useSplitAttributes from '../../composables/useSplitAttributes';
import useModelWrapper from '../../composables/useModelWrapper';
import useFieldData from '../../composables/useFieldData';
import { ValidationStatusType } from '../../types';
import { ValidationStatusTypes, FieldDescriptionIdKey } from '../../constants';
import { makeStringTypeValidator } from '../../utils/stringTypeValidator';
 
const statusValidator = makeStringTypeValidator( ValidationStatusTypes );
 
/**
 * Multi-line text input that allows manual resizing.
 *
 * This form element is useful when you want to create a sizeable amount of free-form text,
 * for example comments, reviews, feedback, or short essay responses.
 */
export default defineComponent( {
	name: 'CdxTextArea',
	components: { CdxIcon },
	inheritAttrs: false,
	props: {
		/**
		 * Current value of the textarea.
		 *
		 * Provided by `v-model` binding in the parent component.
		 */
		modelValue: {
			type: String,
			default: ''
		},
		/**
		 * `status` attribute of the textarea.
		 */
		status: {
			type: String as PropType<ValidationStatusType>,
			default: 'default',
			validator: statusValidator
		},
		/**
		 * Whether the textarea is disabled.
		 */
		disabled: {
			type: Boolean,
			default: false
		},
		/**
		 * Describes whether the textarea grows vertically to show all text.
		 *
		 * When autosize is true, the textarea automatically grows in height (vertically).
		 * The height of the textarea expands while the user types in the textarea.
		 * The content inside the textarea is visible and there's no scroll.
		 *
		 * @values true, false
		 */
		autosize: {
			type: Boolean,
			default: false
		},
		/**
		 * An icon at the start of the textarea element. Similar to a `::before` pseudo-element.
		 */
		startIcon: {
			type: [ String, Object ] as PropType<Icon | undefined>,
			default: undefined
		},
		/**
		 * An icon at the end of the textarea element. Similar to an `::after` pseudo-element.
		 */
		endIcon: {
			type: [ String, Object ] as PropType<Icon | undefined>,
			default: undefined
		}
	},
	emits: [
		/**
		 * When the textarea value changes.
		 *
		 * @property {string} modelValue The new model value
		 */
		'update:modelValue'
	],
	setup( props, { attrs, emit } ) {
		// Take the modelValue provided by the parent component via v-model and
		// generate a wrapped model that we can use for the textarea element in
		// this component.
		const wrappedModel = useModelWrapper( toRef( props, 'modelValue' ), emit );
 
		// 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 );
 
		const textareaClasses = computed( () => {
			return {
				'cdx-text-area__textarea--has-value': !!wrappedModel.value,
				'cdx-text-area__textarea--is-autosize': props.autosize
			};
		} );I

		const internalClasses = computed( () => {
			return {
				'cdx-text-area--status-default': computedStatus.value === 'default',
				'cdx-text-area--status-error': computedStatus.value === 'error',
				'cdx-text-area--has-start-icon': !!props.startIcon,
				'cdx-text-area--has-end-icon': !!props.endIcon
			};
		} );
 
		// Get helpers from useSplitAttributes() composable.
		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 textarea = ref<HTMLTextAreaElement>();
 
		// Allows the textarea to grow aka auto-resize while typing.
		function onInput() {
			if ( textarea.value && props.autosize ) {
				textarea.value.style.height = 'auto';
				textarea.value.style.height = `${ textarea.value.scrollHeight }px`;
			}
		}
 
		return {
			rootClasses,
			rootStyle,
			wrappedModel,
			computedDisabled,
			computedInputId,
			descriptionId,
			textareaClasses,
			otherAttrsMinusId,
			textarea,
			onInput
		};
	}
} );
</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-area {
	// Added for positioning of icons.
	position: relative;
 
	// Added double ampersand to add specificity to Vue and CSS start icons
	& &__start-icon {
		.cdx-mixin-icon(
			start,
			@param-external-padding: @spacing-50 + @border-width-base,
			@param-top: @spacing-25,
			@param-height: @size-150
		);
	}
 
	// Added double ampersand to add specificity to Vue and CSS end icons
	& &__end-icon {
		.cdx-mixin-icon(
			end,
			@min-size-icon-small,
			@size-icon-small,
			@spacing-50 + @border-width-base,
			@spacing-25,
			@size-150
		);
	}
 
	// Ensure background image rules for CSS start icon
	&__icon&__start-icon {
		.cdx-mixin-css-icon-background();
	}
 
	// Ensure CSS end icon is size small and it's height is consistent with the Vue icon.
	&__icon&__end-icon {
		// Note: the icon-size mixin overrides the height property @size-150 (via cdx-mixin-icon)
		// therefore, we override height again to get the desired height of @size-150
		.cdx-mixin-css-icon-size( @size-icon-small );
		.cdx-mixin-css-icon-background( @size-icon-small );
		height: @size-150;
	}
 
	&__textarea {
		display: block;
		box-sizing: @box-sizing-base;
		min-height: @min-height-text-area;
		width: @size-full;
		border-width: @border-width-base;
		border-style: @border-style-base;
		border-radius: @border-radius-base;
		padding: @spacing-25 @spacing-50;
		overflow: auto;
		font-family: inherit;
		font-size: inherit;
		line-height: @line-height-x-small;
		// TODO: Support Safari/Webkit for iOS. The grabber tool is not present in mobile safari.
		/* stylelint-disable-next-line plugin/no-unsupported-browser-features */
		resize: vertical;
 
		&--is-autosize {
			/* stylelint-disable-next-line plugin/no-unsupported-browser-features */
			resize: none;
			overflow: hidden;
 
			// Support Safari/Webkit - supports macOS only
			&::-webkit-resizer {
				display: none;
			}
		}
 
		&: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-area__icon-vue.cdx-icon {
				color: @color-placeholder;
			}
 
			~ .cdx-text-area__icon {
				opacity: @opacity-icon-placeholder;
			}
 
			&:hover {
				border-color: @border-color-input--hover;
			}
 
			&:focus,
			&.cdx-text-area__textarea--has-value {
				~ .cdx-text-area__icon-vue.cdx-icon {
					color: @color-base;
				}
 
				~ .cdx-text-area__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;
			border-color: @border-color-disabled;
 
			/* stylelint-disable-next-line no-descending-specificity */
			~ .cdx-text-area__icon-vue.cdx-icon {
				color: @color-disabled;
			}
 
			/* stylelint-disable-next-line no-descending-specificity */
			~ .cdx-text-area__icon {
				opacity: @opacity-icon-base--disabled;
			}
		}
 
		// Normalize placeholder styling, see T139034.
		&::placeholder {
			color: @color-placeholder;
			opacity: @opacity-base;
		}
	}
 
	// Add additional padding to textarea when the start icon exists.
	// Sets the start icon to 1.25em relative to the font size.
	&--has-start-icon {
		/* stylelint-disable-next-line no-descending-specificity */
		.cdx-text-area__textarea {
			.cdx-mixin-icon-wrapper-padding( start, @spacing-50 );
		}
	}
 
	// Add additional padding to textarea when the end icon exists.
	// Sets the end icon to 1em relative to the font size.
	&--has-end-icon {
		/* stylelint-disable-next-line no-descending-specificity */
		.cdx-text-area__textarea {
			.cdx-mixin-icon-wrapper-padding( end, @spacing-50, @size-icon-small );
		}
	}
 
	&--status-error {
		.cdx-text-area__textarea:enabled:not( :read-only ) {
			border-color: @border-color-error;
 
			&:hover {
				border-color: @border-color-error--hover;
			}
 
			&:focus {
				border-color: @border-color-progressive--focus;
			}
		}
	}
}
</style>