@fiscozen/input 0.1.16 → 1.0.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/FzInput.vue CHANGED
@@ -1,10 +1,335 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * FzInput Component
4
+ *
5
+ * Flexible input component with icon support, validation states, and multiple variants.
6
+ * Supports left/right icons (static or clickable buttons), floating label variant,
7
+ * error/valid states, and full accessibility features.
8
+ *
9
+ * @component
10
+ * @example
11
+ * <FzInput label="Email" type="email" v-model="email" />
12
+ * <FzInput label="Password" type="password" rightIcon="eye" @fzinput:right-icon-click="toggleVisibility" />
13
+ */
14
+ import { computed, toRefs, Ref, ref, watch, useSlots } from "vue";
15
+ import { FzInputProps, type InputEnvironment } from "./types";
16
+ import { FzIcon } from "@fiscozen/icons";
17
+ import { FzIconButton } from "@fiscozen/button";
18
+ import useInputStyle from "./useInputStyle";
19
+ import { generateInputId, sizeToEnvironmentMapping } from "./utils";
20
+
21
+ const props = withDefaults(defineProps<FzInputProps>(), {
22
+ error: false,
23
+ type: "text",
24
+ rightIconButtonVariant: "invisible",
25
+ secondRightIconButtonVariant: "invisible",
26
+ variant: "normal",
27
+ environment: "frontoffice",
28
+ });
29
+
30
+ /**
31
+ * Deprecation warning and normalization for size prop.
32
+ * Watches the size prop and warns once on mount if it's provided.
33
+ * Normalizes size values to environment for backward compatibility.
34
+ */
35
+ watch(
36
+ () => props.size,
37
+ (size) => {
38
+ if (size !== undefined) {
39
+ const mappedEnvironment = sizeToEnvironmentMapping[size];
40
+
41
+ // Check if both environment and size are provided and conflict
42
+ if (props.environment && props.environment !== mappedEnvironment) {
43
+ console.warn(
44
+ `[FzInput] Both "size" and "environment" props are provided. ` +
45
+ `"environment=${props.environment}" will be used and "size=${size}" will be ignored. ` +
46
+ `Please remove the deprecated "size" prop.`
47
+ );
48
+ } else {
49
+ console.warn(
50
+ `[FzInput] The "size" prop is deprecated and will be removed in a future version. ` +
51
+ `Please use environment="${mappedEnvironment}" instead of size="${size}".`
52
+ );
53
+ }
54
+ }
55
+ },
56
+ { immediate: true }
57
+ );
58
+
59
+ /**
60
+ * Deprecation warning for rightIconSize prop.
61
+ * Icons now have a fixed size of "md" and this prop is ignored.
62
+ */
63
+ watch(
64
+ () => props.rightIconSize,
65
+ (rightIconSize) => {
66
+ if (rightIconSize !== undefined) {
67
+ console.warn(
68
+ `[FzInput] The "rightIconSize" prop is deprecated and will be removed in a future version. ` +
69
+ `Icons now have a fixed size of "md". The provided value "${rightIconSize}" will be ignored.`
70
+ );
71
+ }
72
+ },
73
+ { immediate: true }
74
+ );
75
+
76
+ /**
77
+ * Determines the effective environment based on environment or size prop
78
+ *
79
+ * Priority: environment prop > size prop mapped to environment > default 'frontoffice'.
80
+ * The size prop is deprecated and only used for backward compatibility.
81
+ */
82
+ const effectiveEnvironment = computed((): InputEnvironment => {
83
+ if (props.environment) {
84
+ return props.environment;
85
+ }
86
+ if (props.size) {
87
+ return sizeToEnvironmentMapping[props.size];
88
+ }
89
+ return "frontoffice";
90
+ });
91
+
92
+ const model = defineModel<string>();
93
+ const containerRef: Ref<HTMLElement | null> = ref(null);
94
+ const inputRef: Ref<HTMLInputElement | null> = ref(null);
95
+ const uniqueId = generateInputId();
96
+ const isFocused = ref(false);
97
+
98
+ const {
99
+ staticContainerClass,
100
+ computedContainerClass,
101
+ computedLabelClass,
102
+ staticInputClass,
103
+ computedInputClass,
104
+ computedHelpClass,
105
+ computedErrorClass,
106
+ containerWidth,
107
+ showNormalPlaceholder,
108
+ } = useInputStyle(
109
+ toRefs(props),
110
+ containerRef,
111
+ model,
112
+ effectiveEnvironment,
113
+ isFocused
114
+ );
115
+
116
+ const slots = defineSlots<{
117
+ label?: () => unknown;
118
+ "left-icon"?: () => unknown;
119
+ "right-icon"?: () => unknown;
120
+ errorMessage?: () => unknown;
121
+ helpText?: () => unknown;
122
+ }>();
123
+
124
+ const runtimeSlots = useSlots();
125
+
126
+ /**
127
+ * Computes aria-labelledby value linking input to label element
128
+ *
129
+ * Only set when default label element is rendered. Custom label slot replaces default label,
130
+ * so the ID doesn't exist and aria-labelledby would reference a non-existent element.
131
+ */
132
+ const ariaLabelledBy = computed(() => {
133
+ const hasLabelProp = !!props.label;
134
+ const hasCustomLabelSlot = !!runtimeSlots.label;
135
+
136
+ if (hasLabelProp && !hasCustomLabelSlot) {
137
+ return `${uniqueId}-label`;
138
+ }
139
+ return undefined;
140
+ });
141
+
142
+ /**
143
+ * Computes aria-describedby value linking input to help text or error message
144
+ *
145
+ * Uses runtimeSlots (not slots from defineSlots) because defineSlots is only for TypeScript typing.
146
+ */
147
+ const ariaDescribedBy = computed(() => {
148
+ const ids: string[] = [];
149
+ if (props.error && runtimeSlots.errorMessage) {
150
+ ids.push(`${uniqueId}-error`);
151
+ }
152
+ if (!props.error && runtimeSlots.helpText) {
153
+ ids.push(`${uniqueId}-help`);
154
+ }
155
+ return ids.length > 0 ? ids.join(" ") : undefined;
156
+ });
157
+
158
+ const emit = defineEmits<{
159
+ focus: [event: FocusEvent];
160
+ blur: [event: FocusEvent];
161
+ // Other DOM events (keydown, keyup, paste, input, change, etc.) are automatically
162
+ // forwarded to the native input element via v-bind="$attrs" and don't need to be
163
+ // explicitly declared here. They will work automatically when used on FzInput.
164
+ "fzinput:left-icon-click": [];
165
+ "fzinput:right-icon-click": [];
166
+ "fzinput:second-right-icon-click": [];
167
+ }>();
168
+
169
+ /**
170
+ * Handles container interaction (click or keyboard) to focus the input
171
+ *
172
+ * Makes the entire container area clickable and keyboard-accessible for better UX,
173
+ * especially useful for floating-label variant and mobile devices.
174
+ * Respects disabled and readonly states.
175
+ */
176
+ const handleContainerInteraction = () => {
177
+ if (!props.disabled && !props.readonly && inputRef.value) {
178
+ inputRef.value.focus();
179
+ }
180
+ };
181
+
182
+ /**
183
+ * Handles keyboard events on container to focus input
184
+ *
185
+ * Supports Enter and Space keys following accessibility best practices.
186
+ * Only prevents default when event originates from container itself (not from child elements like input),
187
+ * allowing form submission when Enter is pressed inside the input field.
188
+ *
189
+ * @param e - Keyboard event
190
+ */
191
+ const handleContainerKeydown = (e: KeyboardEvent) => {
192
+ if (e.key === "Enter" || e.key === " ") {
193
+ // Only prevent default if event originated from container itself, not from child elements
194
+ // This allows Enter key presses in the input to trigger form submission
195
+ if (e.target === e.currentTarget || e.target === containerRef.value) {
196
+ e.preventDefault();
197
+ handleContainerInteraction();
198
+ }
199
+ }
200
+ };
201
+
202
+ /**
203
+ * Handles keyboard events on clickable icons
204
+ *
205
+ * Supports Enter and Space keys following accessibility best practices.
206
+ *
207
+ * @param e - Keyboard event
208
+ * @param emitEvent - Event name to emit when key is pressed
209
+ */
210
+ const handleIconKeydown = (
211
+ e: KeyboardEvent,
212
+ emitEvent:
213
+ | "fzinput:left-icon-click"
214
+ | "fzinput:right-icon-click"
215
+ | "fzinput:second-right-icon-click"
216
+ ) => {
217
+ if (e.key === "Enter" || e.key === " ") {
218
+ e.preventDefault();
219
+ if (!isReadonlyOrDisabled.value) {
220
+ if (emitEvent === "fzinput:left-icon-click") {
221
+ emit("fzinput:left-icon-click");
222
+ } else if (emitEvent === "fzinput:right-icon-click") {
223
+ emit("fzinput:right-icon-click");
224
+ } else {
225
+ emit("fzinput:second-right-icon-click");
226
+ }
227
+ }
228
+ }
229
+ };
230
+
231
+ /**
232
+ * Handles left icon click events
233
+ *
234
+ * Respects disabled and readonly states - does not emit if input is disabled or readonly.
235
+ */
236
+ const handleLeftIconClick = () => {
237
+ if (!isReadonlyOrDisabled.value) {
238
+ emit("fzinput:left-icon-click");
239
+ }
240
+ };
241
+
242
+ /**
243
+ * Handles right icon click events
244
+ *
245
+ * Respects disabled and readonly states - does not emit if input is disabled or readonly.
246
+ */
247
+ const handleRightIconClick = () => {
248
+ if (!isReadonlyOrDisabled.value) {
249
+ emit("fzinput:right-icon-click");
250
+ }
251
+ };
252
+
253
+ /**
254
+ * Handles second right icon click events
255
+ *
256
+ * Respects disabled and readonly states - does not emit if input is disabled or readonly.
257
+ */
258
+ const handleSecondRightIconClick = () => {
259
+ if (!isReadonlyOrDisabled.value) {
260
+ emit("fzinput:second-right-icon-click");
261
+ }
262
+ };
263
+
264
+ /**
265
+ * Determines if left icon is clickable (has click handler)
266
+ */
267
+ const isLeftIconClickable = computed(() => !!props.leftIcon);
268
+
269
+ /**
270
+ * Determines if left icon is keyboard-accessible (has aria-label)
271
+ *
272
+ * Icons are only accessible via keyboard when aria-label is provided.
273
+ */
274
+ const isLeftIconAccessible = computed(
275
+ () => isLeftIconClickable.value && !!props.leftIconAriaLabel
276
+ );
277
+
278
+ /**
279
+ * Determines if input is disabled or readonly
280
+ *
281
+ * Readonly inputs have the same visual styling and behavior as disabled inputs.
282
+ */
283
+ const isReadonlyOrDisabled = computed(
284
+ () => !!props.disabled || !!props.readonly
285
+ );
286
+
287
+ /**
288
+ * Determines if right icon is clickable (not rendered as button)
289
+ */
290
+ const isRightIconClickable = computed(
291
+ () => !!props.rightIcon && !props.rightIconButton
292
+ );
293
+
294
+ /**
295
+ * Determines if right icon is keyboard-accessible (has aria-label)
296
+ *
297
+ * Icons are only accessible via keyboard when aria-label is provided.
298
+ */
299
+ const isRightIconAccessible = computed(
300
+ () => isRightIconClickable.value && !!props.rightIconAriaLabel
301
+ );
302
+
303
+ /**
304
+ * Determines if second right icon is clickable (not rendered as button)
305
+ */
306
+ const isSecondRightIconClickable = computed(
307
+ () => !!props.secondRightIcon && !props.secondRightIconButton
308
+ );
309
+
310
+ /**
311
+ * Determines if second right icon is keyboard-accessible (has aria-label)
312
+ *
313
+ * Icons are only accessible via keyboard when aria-label is provided.
314
+ */
315
+ const isSecondRightIconAccessible = computed(
316
+ () => isSecondRightIconClickable.value && !!props.secondRightIconAriaLabel
317
+ );
318
+
319
+ defineExpose({
320
+ inputRef,
321
+ containerRef,
322
+ });
323
+ </script>
324
+
1
325
  <template>
2
326
  <div class="fz-input w-full flex flex-col gap-8">
3
327
  <slot name="label">
4
328
  <label
5
- :class="['text-sm', computedLabelClass]"
6
- :for="uniqueId"
7
329
  v-if="label"
330
+ :id="`${uniqueId}-label`"
331
+ :class="computedLabelClass"
332
+ :for="uniqueId"
8
333
  >
9
334
  {{ label }}{{ required ? " *" : "" }}
10
335
  </label>
@@ -12,23 +337,43 @@
12
337
  <div
13
338
  :class="[staticContainerClass, computedContainerClass]"
14
339
  ref="containerRef"
15
- @click="inputRef?.focus()"
340
+ :tabindex="isReadonlyOrDisabled ? undefined : 0"
341
+ @click="handleContainerInteraction"
342
+ @keydown="handleContainerKeydown"
16
343
  >
17
344
  <slot name="left-icon">
18
345
  <FzIcon
19
346
  v-if="leftIcon"
20
347
  :name="leftIcon"
21
- :size="size"
348
+ size="md"
22
349
  :variant="leftIconVariant"
23
- @click.stop="emit('fzinput:left-icon-click')"
350
+ :role="isLeftIconAccessible ? 'button' : undefined"
351
+ :aria-label="isLeftIconAccessible ? leftIconAriaLabel : undefined"
352
+ :aria-disabled="
353
+ isLeftIconAccessible && isReadonlyOrDisabled ? 'true' : undefined
354
+ "
355
+ :tabindex="
356
+ isLeftIconAccessible && !isReadonlyOrDisabled ? 0 : undefined
357
+ "
24
358
  :class="leftIconClass"
359
+ @click.stop="handleLeftIconClick"
360
+ @keydown="
361
+ isLeftIconAccessible
362
+ ? (e: KeyboardEvent) =>
363
+ handleIconKeydown(e, 'fzinput:left-icon-click')
364
+ : undefined
365
+ "
25
366
  />
26
367
  </slot>
27
- <div class="flex flex-col space-around min-w-0">
28
- <span v-if="!showNormalPlaceholder" class="text-xs text-gray-300 grow-0 overflow-hidden text-ellipsis whitespace-nowrap">{{ placeholder }}</span>
368
+ <div class="flex flex-col justify-around min-w-0 grow">
369
+ <span
370
+ v-if="!showNormalPlaceholder"
371
+ class="text-xs text-grey-300 grow-0 overflow-hidden text-ellipsis whitespace-nowrap"
372
+ >{{ placeholder }}</span
373
+ >
29
374
  <input
30
375
  :type="type"
31
- :required="required ? required : false"
376
+ :required="required"
32
377
  :disabled="disabled"
33
378
  :readonly="readonly"
34
379
  :placeholder="showNormalPlaceholder ? placeholder : ''"
@@ -39,53 +384,136 @@
39
384
  :pattern="pattern"
40
385
  :name
41
386
  :maxlength
42
- @blur="(e) => $emit('blur', e)"
43
- @focus="(e) => $emit('focus', e)"
44
- @paste="(e) => $emit('paste', e)"
387
+ :aria-required="required ? 'true' : 'false'"
388
+ :aria-invalid="error ? 'true' : 'false'"
389
+ :aria-disabled="isReadonlyOrDisabled ? 'true' : 'false'"
390
+ :aria-labelledby="ariaLabelledBy"
391
+ :aria-describedby="ariaDescribedBy"
392
+ v-bind="$attrs"
393
+ @blur="
394
+ (e) => {
395
+ isFocused = false;
396
+ $emit('blur', e);
397
+ }
398
+ "
399
+ @focus="
400
+ (e) => {
401
+ isFocused = true;
402
+ $emit('focus', e);
403
+ }
404
+ "
45
405
  />
46
406
  </div>
47
407
  <slot name="right-icon">
48
- <FzIcon
49
- v-if="valid"
50
- name="check"
51
- :size="size"
52
- class="text-semantic-success"
53
- />
54
- <FzIcon
55
- v-if="rightIcon && !rightIconButton"
56
- :name="rightIcon"
57
- :size="size"
58
- :variant="rightIconVariant"
59
- @click.stop="emit('fzinput:right-icon-click')"
60
- :class="rightIconClass"
61
- />
62
- <FzIconButton
63
- v-if="rightIcon && rightIconButton"
64
- :iconName="rightIcon"
65
- :size="mappedSize"
66
- :iconVariant="rightIconVariant"
67
- :variant="disabled ? 'invisible' : rightIconButtonVariant"
68
- @click.stop="emit('fzinput:right-icon-click')"
69
- :class="[{'bg-grey-100 !text-gray-300': disabled}, rightIconClass]"
70
- />
408
+ <div class="flex items-center gap-4">
409
+ <FzIcon
410
+ v-if="secondRightIcon && !secondRightIconButton"
411
+ :name="secondRightIcon"
412
+ size="md"
413
+ :variant="secondRightIconVariant"
414
+ :role="isSecondRightIconAccessible ? 'button' : undefined"
415
+ :aria-label="
416
+ isSecondRightIconAccessible ? secondRightIconAriaLabel : undefined
417
+ "
418
+ :aria-disabled="
419
+ isSecondRightIconAccessible && isReadonlyOrDisabled
420
+ ? 'true'
421
+ : undefined
422
+ "
423
+ :tabindex="
424
+ isSecondRightIconAccessible && !isReadonlyOrDisabled
425
+ ? 0
426
+ : undefined
427
+ "
428
+ :class="secondRightIconClass"
429
+ @click.stop="handleSecondRightIconClick"
430
+ @keydown="
431
+ isSecondRightIconAccessible
432
+ ? (e: KeyboardEvent) =>
433
+ handleIconKeydown(e, 'fzinput:second-right-icon-click')
434
+ : undefined
435
+ "
436
+ />
437
+ <FzIconButton
438
+ v-if="secondRightIcon && secondRightIconButton"
439
+ :iconName="secondRightIcon"
440
+ size="md"
441
+ :iconVariant="secondRightIconVariant"
442
+ :variant="
443
+ isReadonlyOrDisabled ? 'invisible' : secondRightIconButtonVariant
444
+ "
445
+ @click.stop="handleSecondRightIconClick"
446
+ :class="[
447
+ { 'bg-grey-100 !text-grey-300': isReadonlyOrDisabled },
448
+ secondRightIconClass,
449
+ ]"
450
+ />
451
+ <FzIcon
452
+ v-if="rightIcon && !rightIconButton"
453
+ :name="rightIcon"
454
+ size="md"
455
+ :variant="rightIconVariant"
456
+ :role="isRightIconAccessible ? 'button' : undefined"
457
+ :aria-label="isRightIconAccessible ? rightIconAriaLabel : undefined"
458
+ :aria-disabled="
459
+ isRightIconAccessible && isReadonlyOrDisabled ? 'true' : undefined
460
+ "
461
+ :tabindex="
462
+ isRightIconAccessible && !isReadonlyOrDisabled ? 0 : undefined
463
+ "
464
+ :class="rightIconClass"
465
+ @click.stop="handleRightIconClick"
466
+ @keydown="
467
+ isRightIconAccessible
468
+ ? (e: KeyboardEvent) =>
469
+ handleIconKeydown(e, 'fzinput:right-icon-click')
470
+ : undefined
471
+ "
472
+ />
473
+ <FzIconButton
474
+ v-if="rightIcon && rightIconButton"
475
+ :iconName="rightIcon"
476
+ size="md"
477
+ :iconVariant="rightIconVariant"
478
+ :variant="
479
+ isReadonlyOrDisabled ? 'invisible' : rightIconButtonVariant
480
+ "
481
+ @click.stop="handleRightIconClick"
482
+ :class="[
483
+ { 'bg-grey-100 !text-grey-300': isReadonlyOrDisabled },
484
+ rightIconClass,
485
+ ]"
486
+ />
487
+ <FzIcon
488
+ v-if="valid"
489
+ name="check"
490
+ size="md"
491
+ class="text-semantic-success"
492
+ aria-hidden="true"
493
+ />
494
+ </div>
71
495
  </slot>
72
496
  </div>
73
497
  <div
74
498
  v-if="error && $slots.errorMessage"
75
- class="flex gap-4"
499
+ :id="`${uniqueId}-error`"
500
+ role="alert"
501
+ class="flex items-start gap-[6px]"
76
502
  :style="{ width: containerWidth }"
77
503
  >
78
504
  <FzIcon
79
- name="triangle-exclamation"
80
- class="text-semantic-error"
81
- :size="size"
505
+ name="circle-xmark"
506
+ class="text-semantic-error-200"
507
+ size="md"
508
+ aria-hidden="true"
82
509
  />
83
- <div :class="['mt-1', computedErrorClass]">
510
+ <div :class="computedErrorClass">
84
511
  <slot name="errorMessage"></slot>
85
512
  </div>
86
513
  </div>
87
514
  <span
88
515
  v-else-if="$slots.helpText"
516
+ :id="`${uniqueId}-help`"
89
517
  :class="[computedHelpClass]"
90
518
  :style="{ width: containerWidth }"
91
519
  >
@@ -94,62 +522,4 @@
94
522
  </div>
95
523
  </template>
96
524
 
97
- <script setup lang="ts">
98
- import { computed, toRefs, Ref, ref } from "vue";
99
- import { FzInputProps } from "./types";
100
- import { FzIcon } from "@fiscozen/icons";
101
- import { FzIconButton } from "@fiscozen/button";
102
- import useInputStyle from "./useInputStyle";
103
-
104
- const props = withDefaults(defineProps<FzInputProps>(), {
105
- size: "md",
106
- error: false,
107
- type: "text",
108
- rightIconButtonVariant: 'invisible',
109
- variant: 'normal'
110
- });
111
-
112
- const model = defineModel();
113
- const containerRef: Ref<HTMLElement | null> = ref(null);
114
- const inputRef: Ref<HTMLInputElement | null> = ref(null);
115
- const uniqueId = `fz-input-${Math.random().toString(36).slice(2, 9)}`;
116
-
117
- const {
118
- staticContainerClass,
119
- computedContainerClass,
120
- computedLabelClass,
121
- staticInputClass,
122
- computedInputClass,
123
- computedHelpClass,
124
- computedErrorClass,
125
- containerWidth,
126
- showNormalPlaceholder
127
- } = useInputStyle(toRefs(props), containerRef, model);
128
-
129
-
130
- const sizeMap = {
131
- xl: 'lg',
132
- lg: 'md',
133
- md: 'sm',
134
- sm: 'xs',
135
- }
136
-
137
- const mappedSize = computed(() => {
138
- return sizeMap[props.size];
139
- });
140
-
141
- const emit = defineEmits([
142
- "input",
143
- "focus",
144
- "paste",
145
- "blur",
146
- "fzinput:left-icon-click",
147
- "fzinput:right-icon-click",
148
- ]);
149
- defineExpose({
150
- inputRef,
151
- containerRef,
152
- });
153
- </script>
154
-
155
525
  <style scoped></style>