All files / components/field Field.vue

100% Statements 53/53
94.59% Branches 35/37
100% Functions 13/13
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 232 233 234 235 236 237 238 239    1x 4x       1x     1x 1x 1x 1x 1x 1x 1x 1x       1x                                                                                                                                   22x         25x 25x 25x 25x 25x 25x 25x 25x           25x   25x         25x   25x 22x   25x 25x 22x   25x   25x 25x 25x 21x       25x 3x   25x                     1x 1x 1x 1x 1x 1x 1x         25x 25x 25x         25x                     25x                 6x                                           3x                     1x 1x                                            
<template>
	<component
		:is="isFieldset ? 'fieldset' : 'div'"
		class="cdx-field"
		:class="rootClasses"
		:aria-disabled="!isFieldset && computedDisabled ? true : undefined"
		:disabled="isFieldset && computedDisabled ? true : undefined"
	>
		<cdx-label
			:id="labelId"
			:icon="labelIcon"
			:visually-hidden="hideLabel"
			:optional-flag="optionalFlag"
			:input-id="inputId"
			:description-id="descriptionId"
			:disabled="computedDisabled"
			:is-legend="isFieldset"
		>
			<template #default>
				<!-- @slot Label text. -->
				<slot name="label" />
			</template>
			<template v-if="$slots.description && $slots.description().length > 0" #description>
				<!-- @slot Short description text. -->
				<slot name="description" />
			</template>
		</cdx-label>
 
		<div class="cdx-field__control">
			<!-- @slot Input, control, or input group. -->
			<slot />
		</div>
 
		<div class="cdx-field__help-text">
			<!-- @slot Further explanation of how to use this field. -->
			<slot name="help-text" />
		</div>
 
		<div
			v-if="!computedDisabled && validationMessage"
			class="cdx-field__validation-message"
		>
			<cdx-message :type="validationMessageType" :inline="true">
				{{ validationMessage }}
			</cdx-message>
		</div>
	</component>
</template>
 
<script lang="ts">
import { defineComponent, PropType, provide, toRefs, computed } from 'vue';
import { Icon } from '@wikimedia/codex-icons';
 
import CdxLabel from '../label/Label.vue';
import CdxMessage from '../../components/message/Message.vue';
 
import { ValidationStatusTypes, DisabledKey, FieldInputIdKey, FieldDescriptionIdKey, FieldStatusKey } from '../../constants';
import { ValidationStatusType, ValidationMessages } from '../../types';
import { makeStringTypeValidator } from '../../utils/stringTypeValidator';
import useGeneratedId from '../../composables/useGeneratedId';
import useComputedDisabled from '../../composables/useComputedDisabled';
const statusValidator = makeStringTypeValidator( ValidationStatusTypes );
 
/**
 * Form field with a label, an input or control, and an optional validation message.
 */
export default defineComponent( {
	name: 'CdxField',
	components: { CdxLabel, CdxMessage },
	props: {
		/**
		 * Icon before the label text.
		 *
		 * Do not use this if including a start icon within the input component.
		 */
		labelIcon: {
			type: [ String, Object ] as PropType<Icon>,
			default: ''
		},
		/**
		 * Text to indicate that the field is optional.
		 *
		 * For example, this might be '(optional)' in English. This text will be placed next to
		 * the label text.
		 */
		optionalFlag: {
			type: String,
			default: ''
		},
		/**
		 * Whether the label should be visually hidden.
		 *
		 * Note that this will also hide the description.
		 */
		hideLabel: {
			type: Boolean,
			default: false
		},
		/**
		 * Whether this field contains a group of inputs.
		 *
		 * When true, this outputs a `<fieldset>` element with a semantic `<legend>`. When false,
		 * it outputs a `<div>` with a semantic `<label>`.
		 */
		isFieldset: {
			type: Boolean,
			default: false
		},
		/**
		 * Whether the entire field is disabled.
		 */
		disabled: {
			type: Boolean,
			default: false
		},
		/**
		 * `status` attribute of the input. This also determines which validation message is shown.
		 */
		status: {
			type: String as PropType<ValidationStatusType>,
			default: 'default',
			validator: statusValidator
		},
		/**
		 * Message text keyed on validation status type.
		 */
		messages: {
			type: Object as PropType<ValidationMessages>,
			default: () => {
				return {};
			}
		}
	},
	setup( props, { slots } ) {
		const { disabled, status, isFieldset } = toRefs( props );
		const computedDisabled = useComputedDisabled( disabled );
 
		const rootClasses = computed( () => {
			return {
				'cdx-field--disabled': computedDisabled.value,
				'cdx-field--is-fieldset': isFieldset.value
			};
		} );
 
		// An id attribute is added to the label in case it's useful to dev users.
		const labelId = useGeneratedId( 'label' );
		// The description ID is provided to the input for `aria-describedby`.
		const descriptionId = useGeneratedId( 'description' );
 
		// In the case of single input fields (not fieldsets), the input id is passed to the label
		// and provided to the input component.
		// For fieldsets, this is all taken care of by the native HTML elements (<fieldset> and
		// <legend>).
		const inputId = useGeneratedId( 'input' );
 
		// Provide the input ID and description ID to child components
		const computedInputId = computed( () => !isFieldset.value ? inputId : undefined );
		provide( FieldInputIdKey, computedInputId );
		const computedDescriptionId = computed( () =>
			!isFieldset.value && slots.description ? descriptionId : undefined
		);
		provide( FieldDescriptionIdKey, computedDescriptionId );
 
		// Provide the values of the disabled and status props to child components.
		provide( DisabledKey, computedDisabled );
		provide( FieldStatusKey, status );
 
		const validationMessage = computed( () =>
			props.status !== 'default' && props.status in props.messages ?
				props.messages[ props.status ] :
				''
		);
 
		// We don't allow notice validation messages, but this assures TypeScript that we won't try
		// passing 'default' to the type prop of the Message component.
		const validationMessageType = computed( () =>
			props.status === 'default' ? 'notice' : props.status );
 
		return {
			rootClasses,
			computedDisabled,
			labelId,
			descriptionId,
			inputId,
			validationMessage,
			validationMessageType
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
 
.cdx-field {
	// These styles (margin, border, and padding) are added by both browsers and in MediaWiki,
	// e.g. the fieldset styles in resources/src/mediawiki.skinning/elements.less. Border and
	// padding are unset here to ensure that this component looks the same whether it's a single
	// field or a fieldset.
	// Add vertical space between adjacent elements by adding top margin.
	// Normalize/reset the fieldset by setting margin zero to the other sides of the element.
	margin: @spacing-100 0 0 0;
	// Remove border and padding.
	border: 0;
	padding: 0;
 
	&:first-child {
		margin-top: 0;
	}
 
	&__help-text {
		line-height: @line-height-xx-small;
	}
 
	&__help-text,
	&__validation-message {
		margin-top: @spacing-50;
 
		@media screen and ( min-width: @min-width-breakpoint-tablet ) {
			margin-top: @spacing-25;
		}
	}
 
	&:not( .cdx-field--disabled ) {
		.cdx-field__help-text {
			color: @color-subtle;
		}
	}
 
	/* stylelint-disable no-descending-specificity */
	&--disabled {
		.cdx-field__help-text {
			color: @color-disabled;
		}
	}
	/* stylelint-enable no-descending-specificity */
}
</style>