All files / components/search-input SearchInput.vue

100% Statements 45/45
94.73% Branches 18/19
100% Functions 12/12
100% Lines 45/45

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    2x 10x       2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x                                                                                                                                                                 37x 37x 37x 37x 37x 37x         37x 37x 37x 37x 37x 3x   37x                                 1x     1x       2x 2x 2x 2x 2x   90x 90x 90x               3x               3x 3x 15x 2x                       60x               2x 2x                                          
<template>
	<div
		class="cdx-search-input"
		:class="rootClasses"
		:style="rootStyle"
	>
		<div class="cdx-search-input__input-wrapper">
			<cdx-text-input
				ref="textInput"
				v-model="wrappedModel"
				class="cdx-search-input__text-input"
				input-type="search"
				:start-icon="searchIcon"
				:disabled="computedDisabled"
				:status="status"
				v-bind="otherAttrs"
				@keydown.enter="handleSubmit"
				@input="$event => $emit( 'input', $event )"
				@change="$event => $emit( 'change', $event )"
				@focus="$event => $emit( 'focus', $event )"
				@blur="$event => $emit( 'blur', $event )"
			/>
			<!--
				@slot A slot for passing in an options menu that needs to be positioned
				relatively to the text input. See TypeaheadSearch for sample usage.
			-->
			<slot />
		</div>
		<cdx-button
			v-if="buttonLabel"
			class="cdx-search-input__end-button"
			:disabled="computedDisabled"
			@click="handleSubmit"
		>
			{{ buttonLabel }}
		</cdx-button>
	</div>
</template>
 
<script lang="ts">
import { defineComponent, computed, toRef, PropType } from 'vue';
import { cdxIconSearch } from '@wikimedia/codex-icons';
 
import CdxButton from '../button/Button.vue';
import CdxTextInput from '../text-input/TextInput.vue';
 
import useModelWrapper from '../../composables/useModelWrapper';
import useSplitAttributes from '../../composables/useSplitAttributes';
import useFieldData from '../../composables/useFieldData';
 
import { ValidationStatusTypes } from '../../constants';
import { makeStringTypeValidator } from '../../utils/stringTypeValidator';
import { ValidationStatusType } from '../../types';
 
const statusValidator = makeStringTypeValidator( ValidationStatusTypes );
 
/**
 * An input for text search.
 */
export default defineComponent( {
	name: 'CdxSearchInput',
 
	components: {
		CdxButton,
		CdxTextInput
	},
 
	/**
	 * Attributes, besides class, will be passed to the TextInput's input element.
	 */
	inheritAttrs: false,
 
	props: {
		/**
		 * Value of the search input, provided by `v-model` binding in the parent component.
		 */
		modelValue: {
			type: [ String, Number ],
			default: ''
		},
		/**
		 * Submit button text.
		 *
		 * If this is provided, a submit button with this label will be added.
		 */
		buttonLabel: {
			type: String,
			default: ''
		},
		/**
		 * Whether the search input is disabled.
		 */
		disabled: {
			type: Boolean,
			default: false
		},
		/**
		 * `status` property of the TextInput component
		 */
		status: {
			type: String as PropType<ValidationStatusType>,
			default: 'default',
			validator: statusValidator
		}
	},
 
	emits: [
		/**
		 * When the input value changes
		 *
		 * @property {string | number} value The new value
		 */
		'update:modelValue',
		/**
		 * When the submit button is clicked.
		 *
		 * @property {string | number} value The current input
		 */
		'submit-click',
		/**
		 * 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'
	],
 
	setup( props, { emit, attrs } ) {
		const wrappedModel = useModelWrapper( toRef( props, 'modelValue' ), emit );
 
		const { computedDisabled } = useFieldData( toRef( props, 'disabled' ) );
 
		const internalClasses = computed( () => {
			return {
				'cdx-search-input--has-end-button': !!props.buttonLabel
			};
		} );
 
		// Get helpers from useSplitAttributes.
		const {
			rootClasses,
			rootStyle,
			otherAttrs
		} = useSplitAttributes( attrs, internalClasses );
 
		const handleSubmit = () => {
			emit( 'submit-click', wrappedModel.value );
		};
 
		return {
			wrappedModel,
			computedDisabled,
			rootClasses,
			rootStyle,
			otherAttrs,
			handleSubmit,
			searchIcon: cdxIconSearch
		};
	},
 
	methods: {
		/**
		 * Focus the component's input element.
		 *
		 * @public
		 */
		focus(): void {
			const textInput = this.$refs.textInput as InstanceType<typeof CdxTextInput>;
			// Run the TextInput component's focus method, which will set focus
			// to the <input> within.
			textInput.focus();
		}
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/input-with-button.less';
@import ( reference ) '../../themes/mixins/public/css-icon.less';
 
.cdx-search-input {
	.cdx-mixin-input-with-button();
 
	&__input-wrapper {
		// This will make an absolutely positioned options menu passed in via the default slot line
		// up with the input, instead of extending the full width including the button.
		position: relative;
	}
 
	// Set up the CSS-only search icon.
	.cdx-text-input__icon.cdx-text-input__start-icon {
		.cdx-mixin-css-icon( @cdx-icon-search );
	}
}
</style>