@fiscozen/input 3.1.0 → 3.3.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,603 +1,524 @@
1
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, useAttrs } from "vue";
15
- import { FzInputProps, type InputEnvironment } from "./types";
16
- import { FzAlert } from "@fiscozen/alert";
17
- import { FzIcon } from "@fiscozen/icons";
18
- import { FzIconButton } from "@fiscozen/button";
19
- import useInputStyle from "./useInputStyle";
20
- import { generateInputId, sizeToEnvironmentMapping } from "./utils";
21
-
22
- const props = withDefaults(defineProps<FzInputProps>(), {
23
- error: false,
24
- type: "text",
25
- rightIconButtonVariant: "invisible",
26
- secondRightIconButtonVariant: "invisible",
27
- variant: "normal",
28
- environment: "frontoffice",
29
- autocomplete: false,
30
- highlighted: false,
31
- aiReasoning: false,
32
- });
33
-
34
- defineOptions({
35
- inheritAttrs: false,
36
- });
37
-
38
- const attrs = useAttrs();
39
-
40
- /**
41
- * Attrs forwarded to the native input element.
42
- * Excludes class which are applied to the root wrapper div
43
- * so that consumers can control layout/positioning of the component.
44
- */
45
- const inputAttrs = computed(() => {
46
- return {
47
- ...attrs,
48
- class: undefined,
49
- };
50
- });
51
-
52
- const rootClass = computed(() => attrs.class);
53
-
54
- /**
55
- * Deprecation warning and normalization for size prop.
56
- * Watches the size prop and warns once on mount if it's provided.
57
- * Normalizes size values to environment for backward compatibility.
58
- */
59
- watch(
60
- () => props.size,
61
- (size) => {
62
- if (size !== undefined) {
63
- const mappedEnvironment = sizeToEnvironmentMapping[size];
64
-
65
- // Check if both environment and size are provided and conflict
66
- if (props.environment && props.environment !== mappedEnvironment) {
67
- console.warn(
68
- `[FzInput] Both "size" and "environment" props are provided. ` +
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, useAttrs } from "vue";
15
+ import { FzInputProps, type InputEnvironment } from "./types";
16
+ import { FzAlert } from "@fiscozen/alert";
17
+ import { FzIcon } from "@fiscozen/icons";
18
+ import { FzIconButton } from "@fiscozen/button";
19
+ import useInputStyle from "./useInputStyle";
20
+ import { generateInputId, sizeToEnvironmentMapping } from "./utils";
21
+
22
+ const props = withDefaults(defineProps<FzInputProps>(), {
23
+ error: false,
24
+ type: "text",
25
+ rightIconButtonVariant: "invisible",
26
+ secondRightIconButtonVariant: "invisible",
27
+ variant: "normal",
28
+ environment: "frontoffice",
29
+ autocomplete: false,
30
+ highlighted: false,
31
+ highlightedDescription: "Campo in evidenza",
32
+ aiReasoning: false,
33
+ aiReasoningDescription: "Suggerito dall'intelligenza artificiale",
34
+ disableEmphasisReset: false,
35
+ clearable: false,
36
+ clearAriaLabel: "Cancella",
37
+ });
38
+
39
+ defineOptions({
40
+ inheritAttrs: false,
41
+ });
42
+
43
+ const attrs = useAttrs();
44
+
45
+ /**
46
+ * Attrs forwarded to the native input element.
47
+ * Excludes class which are applied to the root wrapper div
48
+ * so that consumers can control layout/positioning of the component.
49
+ */
50
+ const inputAttrs = computed(() => {
51
+ return {
52
+ ...attrs,
53
+ class: undefined,
54
+ };
55
+ });
56
+
57
+ const rootClass = computed(() => attrs.class);
58
+
59
+ /**
60
+ * Deprecation warning and normalization for size prop.
61
+ * Watches the size prop and warns once on mount if it's provided.
62
+ * Normalizes size values to environment for backward compatibility.
63
+ */
64
+ watch(
65
+ () => props.size,
66
+ (size) => {
67
+ if (size !== undefined) {
68
+ const mappedEnvironment = sizeToEnvironmentMapping[size];
69
+
70
+ // Check if both environment and size are provided and conflict
71
+ if (props.environment && props.environment !== mappedEnvironment) {
72
+ console.warn(
73
+ `[FzInput] Both "size" and "environment" props are provided. ` +
69
74
  `"environment=${props.environment}" will be used and "size=${size}" will be ignored. ` +
70
75
  `Please remove the deprecated "size" prop.`,
71
- );
72
- } else {
73
- console.warn(
74
- `[FzInput] The "size" prop is deprecated and will be removed in a future version. ` +
76
+ );
77
+ } else {
78
+ console.warn(
79
+ `[FzInput] The "size" prop is deprecated and will be removed in a future version. ` +
75
80
  `Please use environment="${mappedEnvironment}" instead of size="${size}".`,
81
+ );
82
+ }
83
+ }
84
+ },
85
+ { immediate: true },
86
+ );
87
+
88
+ /**
89
+ * Deprecation warning for rightIconSize prop.
90
+ * Icons now have a fixed size of "md" and this prop is ignored.
91
+ */
92
+ watch(
93
+ () => props.rightIconSize,
94
+ (rightIconSize) => {
95
+ if (rightIconSize !== undefined) {
96
+ console.warn(
97
+ `[FzInput] The "rightIconSize" prop is deprecated and will be removed in a future version. ` +
98
+ `Icons now have a fixed size of "md". The provided value "${rightIconSize}" will be ignored.`,
76
99
  );
77
100
  }
101
+ },
102
+ { immediate: true },
103
+ );
104
+
105
+ /**
106
+ * Determines the effective environment based on environment or size prop
107
+ *
108
+ * Priority: environment prop > size prop mapped to environment > default 'frontoffice'.
109
+ * The size prop is deprecated and only used for backward compatibility.
110
+ */
111
+ const effectiveEnvironment = computed((): InputEnvironment => {
112
+ if (props.environment) {
113
+ return props.environment;
78
114
  }
79
- },
80
- { immediate: true },
81
- );
82
-
83
- /**
84
- * Deprecation warning for rightIconSize prop.
85
- * Icons now have a fixed size of "md" and this prop is ignored.
86
- */
87
- watch(
88
- () => props.rightIconSize,
89
- (rightIconSize) => {
90
- if (rightIconSize !== undefined) {
91
- console.warn(
92
- `[FzInput] The "rightIconSize" prop is deprecated and will be removed in a future version. ` +
93
- `Icons now have a fixed size of "md". The provided value "${rightIconSize}" will be ignored.`,
94
- );
115
+ if (props.size) {
116
+ return sizeToEnvironmentMapping[props.size];
117
+ }
118
+ return "frontoffice";
119
+ });
120
+
121
+ const model = defineModel<string>();
122
+ const containerRef: Ref<HTMLElement | null> = ref(null);
123
+ const inputRef: Ref<HTMLInputElement | null> = ref(null);
124
+ const uniqueId = generateInputId();
125
+ const isFocused = ref(false);
126
+
127
+ /**
128
+ * Internal visual state for emphasis props.
129
+ * These track the effective visual state which can differ from props when
130
+ * the user types into a highlighted/aiReasoning input (user input resets emphasis).
131
+ * Programmatic value changes (via v-model) do not affect these states.
132
+ */
133
+ const effectiveHighlighted = ref(props.highlighted);
134
+ const effectiveAiReasoning = ref(props.aiReasoning);
135
+
136
+ watch(
137
+ () => props.highlighted,
138
+ (val) => {
139
+ effectiveHighlighted.value = val;
140
+ },
141
+ );
142
+ watch(
143
+ () => props.aiReasoning,
144
+ (val) => {
145
+ effectiveAiReasoning.value = val;
146
+ },
147
+ );
148
+
149
+ /**
150
+ * Resets visual emphasis (highlighted/aiReasoning) when user physically types.
151
+ * Only triggered by native input events (user interaction), not programmatic v-model updates.
152
+ * Emits update events so parents using v-model:highlighted / v-model:aiReasoning stay in sync.
153
+ */
154
+ const handleUserInput = () => {
155
+ if (props.disableEmphasisReset) return;
156
+ if (effectiveHighlighted.value) {
157
+ effectiveHighlighted.value = false;
158
+ emit("update:highlighted", false);
159
+ }
160
+ if (effectiveAiReasoning.value) {
161
+ effectiveAiReasoning.value = false;
162
+ emit("update:aiReasoning", false);
163
+ }
164
+ };
165
+
166
+ const propsRefs = toRefs(props);
167
+
168
+ const {
169
+ staticContainerClass,
170
+ computedContainerClass,
171
+ computedLabelClass,
172
+ staticInputClass,
173
+ computedInputClass,
174
+ computedHelpClass,
175
+ containerWidth,
176
+ showNormalPlaceholder,
177
+ } = useInputStyle(
178
+ {
179
+ ...propsRefs,
180
+ highlighted: effectiveHighlighted,
181
+ aiReasoning: effectiveAiReasoning,
182
+ },
183
+ containerRef,
184
+ model,
185
+ effectiveEnvironment,
186
+ isFocused,
187
+ );
188
+
189
+ const slots = defineSlots<{
190
+ label?: () => unknown;
191
+ "left-icon"?: () => unknown;
192
+ "right-icon"?: () => unknown;
193
+ errorMessage?: () => unknown;
194
+ helpText?: () => unknown;
195
+ }>();
196
+
197
+ const runtimeSlots = useSlots();
198
+
199
+ /**
200
+ * Computes aria-labelledby value linking input to label element
201
+ *
202
+ * Only set when default label element is rendered. Custom label slot replaces default label,
203
+ * so the ID doesn't exist and aria-labelledby would reference a non-existent element.
204
+ */
205
+ const ariaLabelledBy = computed(() => {
206
+ const hasLabelProp = !!props.label;
207
+ const hasCustomLabelSlot = !!runtimeSlots.label;
208
+
209
+ if (hasLabelProp && !hasCustomLabelSlot) {
210
+ return `${uniqueId}-label`;
211
+ }
212
+ return undefined;
213
+ });
214
+
215
+ /**
216
+ * Computes aria-describedby value linking input to help text or error message
217
+ *
218
+ * Uses runtimeSlots (not slots from defineSlots) because defineSlots is only for TypeScript typing.
219
+ */
220
+ const ariaDescribedBy = computed(() => {
221
+ const ids: string[] = [];
222
+ if (props.error && runtimeSlots.errorMessage) {
223
+ ids.push(`${uniqueId}-error`);
95
224
  }
96
- },
97
- { immediate: true },
98
- );
99
-
100
- /**
101
- * Determines the effective environment based on environment or size prop
102
- *
103
- * Priority: environment prop > size prop mapped to environment > default 'frontoffice'.
104
- * The size prop is deprecated and only used for backward compatibility.
105
- */
106
- const effectiveEnvironment = computed((): InputEnvironment => {
107
- if (props.environment) {
108
- return props.environment;
109
- }
110
- if (props.size) {
111
- return sizeToEnvironmentMapping[props.size];
112
- }
113
- return "frontoffice";
114
- });
115
-
116
- const model = defineModel<string>();
117
- const containerRef: Ref<HTMLElement | null> = ref(null);
118
- const inputRef: Ref<HTMLInputElement | null> = ref(null);
119
- const uniqueId = generateInputId();
120
- const isFocused = ref(false);
121
-
122
- /**
123
- * Internal visual state for emphasis props.
124
- * These track the effective visual state which can differ from props when
125
- * the user types into a highlighted/aiReasoning input (user input resets emphasis).
126
- * Programmatic value changes (via v-model) do not affect these states.
127
- */
128
- const effectiveHighlighted = ref(props.highlighted);
129
- const effectiveAiReasoning = ref(props.aiReasoning);
130
-
131
- watch(
132
- () => props.highlighted,
133
- (val) => {
134
- effectiveHighlighted.value = val;
135
- },
136
- );
137
- watch(
138
- () => props.aiReasoning,
139
- (val) => {
140
- effectiveAiReasoning.value = val;
141
- },
142
- );
143
-
144
- /**
145
- * Resets visual emphasis (highlighted/aiReasoning) when user physically types.
146
- * Only triggered by native input events (user interaction), not programmatic v-model updates.
147
- * Emits update events so parents using v-model:highlighted / v-model:aiReasoning stay in sync.
148
- */
149
- const handleUserInput = () => {
150
- if (effectiveHighlighted.value) {
151
- effectiveHighlighted.value = false;
152
- emit("update:highlighted", false);
153
- }
154
- if (effectiveAiReasoning.value) {
155
- effectiveAiReasoning.value = false;
156
- emit("update:aiReasoning", false);
157
- }
158
- };
159
-
160
- const propsRefs = toRefs(props);
161
-
162
- const {
163
- staticContainerClass,
164
- computedContainerClass,
165
- computedLabelClass,
166
- staticInputClass,
167
- computedInputClass,
168
- computedHelpClass,
169
- containerWidth,
170
- showNormalPlaceholder,
171
- } = useInputStyle(
172
- {
173
- ...propsRefs,
174
- highlighted: effectiveHighlighted,
175
- aiReasoning: effectiveAiReasoning,
176
- },
177
- containerRef,
178
- model,
179
- effectiveEnvironment,
180
- isFocused,
181
- );
182
-
183
- const slots = defineSlots<{
184
- label?: () => unknown;
185
- "left-icon"?: () => unknown;
186
- "right-icon"?: () => unknown;
187
- errorMessage?: () => unknown;
188
- helpText?: () => unknown;
189
- }>();
190
-
191
- const runtimeSlots = useSlots();
192
-
193
- /**
194
- * Computes aria-labelledby value linking input to label element
195
- *
196
- * Only set when default label element is rendered. Custom label slot replaces default label,
197
- * so the ID doesn't exist and aria-labelledby would reference a non-existent element.
198
- */
199
- const ariaLabelledBy = computed(() => {
200
- const hasLabelProp = !!props.label;
201
- const hasCustomLabelSlot = !!runtimeSlots.label;
202
-
203
- if (hasLabelProp && !hasCustomLabelSlot) {
204
- return `${uniqueId}-label`;
205
- }
206
- return undefined;
207
- });
208
-
209
- /**
210
- * Computes aria-describedby value linking input to help text or error message
211
- *
212
- * Uses runtimeSlots (not slots from defineSlots) because defineSlots is only for TypeScript typing.
213
- */
214
- const ariaDescribedBy = computed(() => {
215
- const ids: string[] = [];
216
- if (props.error && runtimeSlots.errorMessage) {
217
- ids.push(`${uniqueId}-error`);
218
- }
219
- if (!props.error && runtimeSlots.helpText) {
220
- ids.push(`${uniqueId}-help`);
221
- }
222
- return ids.length > 0 ? ids.join(" ") : undefined;
223
- });
224
-
225
- const emit = defineEmits<{
226
- focus: [event: FocusEvent];
227
- blur: [event: FocusEvent];
228
- // Other DOM events (keydown, keyup, paste, input, change, etc.) are automatically
229
- // forwarded to the native input element via v-bind="$attrs" and don't need to be
230
- // explicitly declared here. They will work automatically when used on FzInput.
231
- "fzinput:left-icon-click": [];
232
- "fzinput:right-icon-click": [];
233
- "fzinput:second-right-icon-click": [];
234
- "update:highlighted": [value: boolean];
235
- "update:aiReasoning": [value: boolean];
236
- }>();
237
-
238
- /**
239
- * Handles container interaction (click or keyboard) to focus the input
240
- *
241
- * Makes the entire container area clickable and keyboard-accessible for better UX,
242
- * especially useful for floating-label variant and mobile devices.
243
- * Respects disabled and readonly states.
244
- */
245
- const handleContainerInteraction = () => {
246
- if (!props.disabled && !props.readonly && inputRef.value) {
247
- inputRef.value.focus();
248
- }
249
- };
250
-
251
- /**
252
- * Handles keyboard events on container to focus input
253
- *
254
- * Supports Enter and Space keys following accessibility best practices.
255
- * Only prevents default when event originates from container itself (not from child elements like input),
256
- * allowing form submission when Enter is pressed inside the input field.
257
- *
258
- * @param e - Keyboard event
259
- */
260
- const handleContainerKeydown = (e: KeyboardEvent) => {
261
- if (e.key === "Enter" || e.key === " ") {
262
- // Only prevent default if event originated from container itself, not from child elements
263
- // This allows Enter key presses in the input to trigger form submission
264
- if (e.target === e.currentTarget || e.target === containerRef.value) {
225
+ if (!props.error && runtimeSlots.helpText) {
226
+ ids.push(`${uniqueId}-help`);
227
+ }
228
+ return ids.length > 0 ? ids.join(" ") : undefined;
229
+ });
230
+
231
+ const emit = defineEmits<{
232
+ focus: [event: FocusEvent];
233
+ blur: [event: FocusEvent];
234
+ // Other DOM events (keydown, keyup, paste, input, change, etc.) are automatically
235
+ // forwarded to the native input element via v-bind="$attrs" and don't need to be
236
+ // explicitly declared here. They will work automatically when used on FzInput.
237
+ "fzinput:left-icon-click": [];
238
+ "fzinput:right-icon-click": [];
239
+ "fzinput:second-right-icon-click": [];
240
+ "update:highlighted": [value: boolean];
241
+ "update:aiReasoning": [value: boolean];
242
+ "fzinput:clear": [];
243
+ }>();
244
+
245
+ /**
246
+ * Handles container interaction (click or keyboard) to focus the input
247
+ *
248
+ * Makes the entire container area clickable and keyboard-accessible for better UX,
249
+ * especially useful for floating-label variant and mobile devices.
250
+ * Respects disabled and readonly states.
251
+ */
252
+ const handleContainerInteraction = () => {
253
+ if (!props.disabled && !props.readonly && inputRef.value) {
254
+ inputRef.value.focus();
255
+ }
256
+ };
257
+
258
+ /**
259
+ * Handles keyboard events on container to focus input
260
+ *
261
+ * Supports Enter and Space keys following accessibility best practices.
262
+ * Only prevents default when event originates from container itself (not from child elements like input),
263
+ * allowing form submission when Enter is pressed inside the input field.
264
+ *
265
+ * @param e - Keyboard event
266
+ */
267
+ const handleContainerKeydown = (e: KeyboardEvent) => {
268
+ if (e.key === "Enter" || e.key === " ") {
269
+ // Only prevent default if event originated from container itself, not from child elements
270
+ // This allows Enter key presses in the input to trigger form submission
271
+ if (e.target === e.currentTarget || e.target === containerRef.value) {
272
+ e.preventDefault();
273
+ handleContainerInteraction();
274
+ }
275
+ }
276
+ };
277
+
278
+ /**
279
+ * Handles keyboard events on clickable icons
280
+ *
281
+ * Supports Enter and Space keys following accessibility best practices.
282
+ *
283
+ * @param e - Keyboard event
284
+ * @param emitEvent - Event name to emit when key is pressed
285
+ */
286
+ const handleIconKeydown = (
287
+ e: KeyboardEvent,
288
+ emitEvent:
289
+ | "fzinput:left-icon-click"
290
+ | "fzinput:right-icon-click"
291
+ | "fzinput:second-right-icon-click",
292
+ ) => {
293
+ if (e.key === "Enter" || e.key === " ") {
265
294
  e.preventDefault();
266
- handleContainerInteraction();
295
+ if (!isReadonlyOrDisabled.value) {
296
+ if (emitEvent === "fzinput:left-icon-click") {
297
+ emit("fzinput:left-icon-click");
298
+ } else if (emitEvent === "fzinput:right-icon-click") {
299
+ emit("fzinput:right-icon-click");
300
+ } else {
301
+ emit("fzinput:second-right-icon-click");
302
+ }
303
+ }
267
304
  }
268
- }
269
- };
270
-
271
- /**
272
- * Handles keyboard events on clickable icons
273
- *
274
- * Supports Enter and Space keys following accessibility best practices.
275
- *
276
- * @param e - Keyboard event
277
- * @param emitEvent - Event name to emit when key is pressed
278
- */
279
- const handleIconKeydown = (
280
- e: KeyboardEvent,
281
- emitEvent:
282
- | "fzinput:left-icon-click"
283
- | "fzinput:right-icon-click"
284
- | "fzinput:second-right-icon-click",
285
- ) => {
286
- if (e.key === "Enter" || e.key === " ") {
287
- e.preventDefault();
305
+ };
306
+
307
+ /**
308
+ * Handles left icon click events
309
+ *
310
+ * Respects disabled and readonly states - does not emit if input is disabled or readonly.
311
+ */
312
+ const handleLeftIconClick = () => {
288
313
  if (!isReadonlyOrDisabled.value) {
289
- if (emitEvent === "fzinput:left-icon-click") {
290
- emit("fzinput:left-icon-click");
291
- } else if (emitEvent === "fzinput:right-icon-click") {
292
- emit("fzinput:right-icon-click");
293
- } else {
294
- emit("fzinput:second-right-icon-click");
295
- }
314
+ emit("fzinput:left-icon-click");
296
315
  }
297
- }
298
- };
299
-
300
- /**
301
- * Handles left icon click events
302
- *
303
- * Respects disabled and readonly states - does not emit if input is disabled or readonly.
304
- */
305
- const handleLeftIconClick = () => {
306
- if (!isReadonlyOrDisabled.value) {
307
- emit("fzinput:left-icon-click");
308
- }
309
- };
310
-
311
- /**
312
- * Handles right icon click events
313
- *
314
- * Respects disabled and readonly states - does not emit if input is disabled or readonly.
315
- */
316
- const handleRightIconClick = () => {
317
- if (!isReadonlyOrDisabled.value) {
318
- emit("fzinput:right-icon-click");
319
- }
320
- };
321
-
322
- /**
323
- * Handles second right icon click events
324
- *
325
- * Respects disabled and readonly states - does not emit if input is disabled or readonly.
326
- */
327
- const handleSecondRightIconClick = () => {
328
- if (!isReadonlyOrDisabled.value) {
329
- emit("fzinput:second-right-icon-click");
330
- }
331
- };
332
-
333
- /**
334
- * Determines if left icon is clickable (has click handler)
335
- */
336
- const isLeftIconClickable = computed(() => !!props.leftIcon);
337
-
338
- /**
339
- * Determines if left icon is keyboard-accessible (has aria-label)
340
- *
341
- * Icons are only accessible via keyboard when aria-label is provided.
342
- */
343
- const isLeftIconAccessible = computed(
344
- () => isLeftIconClickable.value && !!props.leftIconAriaLabel,
345
- );
346
-
347
- /**
348
- * Determines if input is disabled or readonly
349
- *
350
- * Readonly inputs have the same visual styling and behavior as disabled inputs.
351
- */
352
- const isReadonlyOrDisabled = computed(
353
- () => !!props.disabled || !!props.readonly,
354
- );
355
-
356
- /**
357
- * Computed class for the auto-rendered AI sparkles icon.
358
- * Muted when the input is in error, disabled, or readonly state.
359
- */
360
- const aiIconClass = computed(() => {
361
- if (isReadonlyOrDisabled.value || props.error) return "text-grey-300";
362
- return "text-purple-600";
363
- });
364
-
365
- /**
366
- * Determines if right icon is clickable (not rendered as button)
367
- */
368
- const isRightIconClickable = computed(
369
- () => !!props.rightIcon && !props.rightIconButton,
370
- );
371
-
372
- /**
373
- * Determines if right icon is keyboard-accessible (has aria-label)
374
- *
375
- * Icons are only accessible via keyboard when aria-label is provided.
376
- */
377
- const isRightIconAccessible = computed(
378
- () => isRightIconClickable.value && !!props.rightIconAriaLabel,
379
- );
380
-
381
- /**
382
- * Determines if second right icon is clickable (not rendered as button)
383
- */
384
- const isSecondRightIconClickable = computed(
385
- () => !!props.secondRightIcon && !props.secondRightIconButton,
386
- );
387
-
388
- /**
389
- * Determines if second right icon is keyboard-accessible (has aria-label)
390
- *
391
- * Icons are only accessible via keyboard when aria-label is provided.
392
- */
393
- const isSecondRightIconAccessible = computed(
394
- () => isSecondRightIconClickable.value && !!props.secondRightIconAriaLabel,
395
- );
396
-
397
- defineExpose({
398
- inputRef,
399
- containerRef,
400
- });
316
+ };
317
+
318
+ /**
319
+ * Handles right icon click events
320
+ *
321
+ * Respects disabled and readonly states - does not emit if input is disabled or readonly.
322
+ */
323
+ const handleRightIconClick = () => {
324
+ if (!isReadonlyOrDisabled.value) {
325
+ emit("fzinput:right-icon-click");
326
+ }
327
+ };
328
+
329
+ /**
330
+ * Handles second right icon click events
331
+ *
332
+ * Respects disabled and readonly states - does not emit if input is disabled or readonly.
333
+ */
334
+ const handleSecondRightIconClick = () => {
335
+ if (!isReadonlyOrDisabled.value) {
336
+ emit("fzinput:second-right-icon-click");
337
+ }
338
+ };
339
+
340
+ /**
341
+ * Determines if left icon is clickable (has click handler)
342
+ */
343
+ const isLeftIconClickable = computed(() => !!props.leftIcon);
344
+
345
+ /**
346
+ * Determines if left icon is keyboard-accessible (has aria-label)
347
+ *
348
+ * Icons are only accessible via keyboard when aria-label is provided.
349
+ */
350
+ const isLeftIconAccessible = computed(
351
+ () => isLeftIconClickable.value && !!props.leftIconAriaLabel,
352
+ );
353
+
354
+ /**
355
+ * Determines if input is disabled or readonly
356
+ *
357
+ * Readonly inputs have the same visual styling and behavior as disabled inputs.
358
+ */
359
+ const isReadonlyOrDisabled = computed(
360
+ () => !!props.disabled || !!props.readonly,
361
+ );
362
+
363
+ /**
364
+ * Computed class for the auto-rendered AI sparkles icon.
365
+ * Muted when the input is in error, disabled, or readonly state.
366
+ */
367
+ const aiIconClass = computed(() => {
368
+ if (isReadonlyOrDisabled.value || props.error) return "text-grey-300";
369
+ return "text-purple-600";
370
+ });
371
+
372
+ const emphasisDescription = computed(() => {
373
+ if (isReadonlyOrDisabled.value || props.error) return undefined;
374
+ if (effectiveHighlighted.value) return props.highlightedDescription;
375
+ if (effectiveAiReasoning.value) return props.aiReasoningDescription;
376
+ return undefined;
377
+ });
378
+
379
+ /**
380
+ * Determines if right icon is clickable (not rendered as button)
381
+ */
382
+ const isRightIconClickable = computed(
383
+ () => !!props.rightIcon && !props.rightIconButton,
384
+ );
385
+
386
+ /**
387
+ * Determines if right icon is keyboard-accessible (has aria-label)
388
+ *
389
+ * Icons are only accessible via keyboard when aria-label is provided.
390
+ */
391
+ const isRightIconAccessible = computed(
392
+ () => isRightIconClickable.value && !!props.rightIconAriaLabel,
393
+ );
394
+
395
+ /**
396
+ * Determines if second right icon is clickable (not rendered as button)
397
+ */
398
+ const isSecondRightIconClickable = computed(
399
+ () => !!props.secondRightIcon && !props.secondRightIconButton,
400
+ );
401
+
402
+ /**
403
+ * Determines if second right icon is keyboard-accessible (has aria-label)
404
+ *
405
+ * Icons are only accessible via keyboard when aria-label is provided.
406
+ */
407
+ const isSecondRightIconAccessible = computed(
408
+ () => isSecondRightIconClickable.value && !!props.secondRightIconAriaLabel,
409
+ );
410
+
411
+ const shouldShowClearIcon = computed(() => {
412
+ return props.clearable && !!model.value && !isReadonlyOrDisabled.value;
413
+ });
414
+
415
+ const handleClear = () => {
416
+ model.value = "";
417
+ emit("fzinput:clear");
418
+ inputRef.value?.focus();
419
+ };
420
+
421
+ defineExpose({
422
+ inputRef,
423
+ containerRef,
424
+ });
401
425
  </script>
402
426
 
403
427
  <template>
404
428
  <div class="fz-input w-full flex flex-col gap-8" :class="rootClass">
405
429
  <slot name="label">
406
- <label
407
- v-if="label"
408
- :id="`${uniqueId}-label`"
409
- :class="computedLabelClass"
410
- :for="uniqueId"
411
- >
430
+ <label v-if="label" :id="`${uniqueId}-label`" :class="computedLabelClass" :for="uniqueId">
412
431
  {{ label }}{{ required ? " *" : "" }}
413
432
  </label>
414
433
  </slot>
415
- <div
416
- :class="[staticContainerClass, computedContainerClass]"
417
- ref="containerRef"
418
- :tabindex="isReadonlyOrDisabled ? undefined : 0"
419
- @click="handleContainerInteraction"
420
- @keydown="handleContainerKeydown"
421
- >
434
+ <div :class="[staticContainerClass, computedContainerClass]" ref="containerRef"
435
+ :tabindex="isReadonlyOrDisabled ? undefined : 0" @click="handleContainerInteraction"
436
+ @keydown="handleContainerKeydown">
422
437
  <slot name="left-icon">
423
- <FzIcon
424
- v-if="leftIcon"
425
- :name="leftIcon"
426
- size="md"
427
- :variant="leftIconVariant"
438
+ <FzIcon v-if="leftIcon" :name="leftIcon" size="md" :variant="leftIconVariant"
428
439
  :role="isLeftIconAccessible ? 'button' : undefined"
429
- :aria-label="isLeftIconAccessible ? leftIconAriaLabel : undefined"
430
- :aria-disabled="
431
- isLeftIconAccessible && isReadonlyOrDisabled ? 'true' : undefined
432
- "
433
- :tabindex="
434
- isLeftIconAccessible && !isReadonlyOrDisabled ? 0 : undefined
435
- "
436
- :class="leftIconClass"
437
- @click.stop="handleLeftIconClick"
438
- @keydown="
439
- isLeftIconAccessible
440
- ? (e: KeyboardEvent) =>
441
- handleIconKeydown(e, 'fzinput:left-icon-click')
442
- : undefined
443
- "
444
- />
445
- <FzIcon
446
- v-else-if="effectiveAiReasoning"
447
- name="sparkles"
448
- variant="fas"
449
- size="md"
450
- aria-hidden="true"
451
- :class="aiIconClass"
452
- />
440
+ :aria-label="isLeftIconAccessible ? leftIconAriaLabel : undefined" :aria-disabled="isLeftIconAccessible && isReadonlyOrDisabled ? 'true' : undefined
441
+ " :tabindex="isLeftIconAccessible && !isReadonlyOrDisabled ? 0 : undefined
442
+ " :class="leftIconClass" @click.stop="handleLeftIconClick" @keydown="
443
+ isLeftIconAccessible
444
+ ? (e: KeyboardEvent) =>
445
+ handleIconKeydown(e, 'fzinput:left-icon-click')
446
+ : undefined
447
+ " />
448
+ <FzIcon v-else-if="effectiveAiReasoning && !effectiveHighlighted" name="sparkles" variant="fas" size="md"
449
+ aria-hidden="true" :class="aiIconClass" />
453
450
  </slot>
454
451
  <div class="flex flex-col justify-around min-w-0 grow">
455
- <span
456
- v-if="!showNormalPlaceholder"
457
- class="text-xs text-grey-300 grow-0 overflow-hidden text-ellipsis whitespace-nowrap"
458
- >{{ placeholder }}</span
459
- >
460
- <input
461
- :type="type"
462
- :required="required"
463
- :disabled="disabled"
464
- :readonly="readonly"
465
- :placeholder="showNormalPlaceholder ? placeholder : ''"
466
- v-model="model"
467
- :id="uniqueId"
468
- ref="inputRef"
469
- :class="[staticInputClass, computedInputClass]"
470
- :pattern="pattern"
471
- :name
472
- :maxlength
473
- :autocomplete="autocomplete ? 'on' : 'off'"
474
- :aria-required="required ? 'true' : 'false'"
475
- :aria-invalid="error ? 'true' : 'false'"
476
- :aria-disabled="isReadonlyOrDisabled ? 'true' : 'false'"
477
- :aria-labelledby="ariaLabelledBy"
478
- :aria-describedby="ariaDescribedBy"
479
- v-bind="inputAttrs"
480
- @input="handleUserInput"
481
- @blur="
452
+ <span v-if="!showNormalPlaceholder"
453
+ class="text-xs text-grey-300 grow-0 overflow-hidden text-ellipsis whitespace-nowrap">{{ placeholder }}</span>
454
+ <input :type="type" :required="required" :disabled="disabled" :readonly="readonly"
455
+ :placeholder="showNormalPlaceholder ? placeholder : ''" v-model="model" :id="uniqueId" ref="inputRef"
456
+ :class="[staticInputClass, computedInputClass]" :pattern="pattern" :name :maxlength
457
+ :autocomplete="autocomplete ? 'on' : 'off'" :aria-required="required ? 'true' : 'false'"
458
+ :aria-invalid="error ? 'true' : 'false'" :aria-disabled="isReadonlyOrDisabled ? 'true' : 'false'"
459
+ :aria-labelledby="ariaLabelledBy" :aria-describedby="ariaDescribedBy" :aria-description="emphasisDescription"
460
+ v-bind="inputAttrs" @input="handleUserInput" @blur="
482
461
  (e) => {
483
462
  isFocused = false;
484
463
  $emit('blur', e);
485
464
  }
486
- "
487
- @focus="
465
+ " @focus="
488
466
  (e) => {
489
467
  isFocused = true;
490
468
  $emit('focus', e);
491
469
  }
492
- "
493
- />
470
+ " />
494
471
  </div>
495
- <slot name="right-icon">
496
- <div class="flex items-center gap-4">
497
- <FzIcon
498
- v-if="secondRightIcon && !secondRightIconButton"
499
- :name="secondRightIcon"
500
- size="md"
501
- :variant="secondRightIconVariant"
502
- :role="isSecondRightIconAccessible ? 'button' : undefined"
503
- :aria-label="
504
- isSecondRightIconAccessible ? secondRightIconAriaLabel : undefined
505
- "
506
- :aria-disabled="
507
- isSecondRightIconAccessible && isReadonlyOrDisabled
472
+ <div class="flex items-center gap-4">
473
+ <FzIconButton v-if="shouldShowClearIcon" iconName="xmark" size="md" variant="invisible"
474
+ :ariaLabel="clearAriaLabel" @click.stop="handleClear" />
475
+ <slot name="right-icon">
476
+ <FzIcon v-if="secondRightIcon && !secondRightIconButton" :name="secondRightIcon" size="md"
477
+ :variant="secondRightIconVariant" :role="isSecondRightIconAccessible ? 'button' : undefined" :aria-label="isSecondRightIconAccessible ? secondRightIconAriaLabel : undefined
478
+ " :aria-disabled="isSecondRightIconAccessible && isReadonlyOrDisabled
508
479
  ? 'true'
509
480
  : undefined
510
- "
511
- :tabindex="
512
- isSecondRightIconAccessible && !isReadonlyOrDisabled
513
- ? 0
514
- : undefined
515
- "
516
- :class="secondRightIconClass"
517
- @click.stop="handleSecondRightIconClick"
518
- @keydown="
519
- isSecondRightIconAccessible
520
- ? (e: KeyboardEvent) =>
521
- handleIconKeydown(e, 'fzinput:second-right-icon-click')
522
- : undefined
523
- "
524
- />
525
- <FzIconButton
526
- v-if="secondRightIcon && secondRightIconButton"
527
- :iconName="secondRightIcon"
528
- size="md"
529
- :iconVariant="secondRightIconVariant"
530
- :variant="
531
- isReadonlyOrDisabled ? 'invisible' : secondRightIconButtonVariant
532
- "
533
- @click.stop="handleSecondRightIconClick"
534
- :class="[
535
- { 'bg-grey-100 !text-grey-300': isReadonlyOrDisabled },
536
- secondRightIconClass,
537
- ]"
538
- />
539
- <FzIcon
540
- v-if="rightIcon && !rightIconButton"
541
- :name="rightIcon"
542
- size="md"
543
- :variant="rightIconVariant"
481
+ " :tabindex="isSecondRightIconAccessible && !isReadonlyOrDisabled
482
+ ? 0
483
+ : undefined
484
+ " :class="secondRightIconClass" @click.stop="handleSecondRightIconClick" @keydown="
485
+ isSecondRightIconAccessible
486
+ ? (e: KeyboardEvent) =>
487
+ handleIconKeydown(e, 'fzinput:second-right-icon-click')
488
+ : undefined
489
+ " />
490
+ <FzIconButton v-if="secondRightIcon && secondRightIconButton" :iconName="secondRightIcon" size="md"
491
+ :iconVariant="secondRightIconVariant" :variant="isReadonlyOrDisabled ? 'invisible' : secondRightIconButtonVariant
492
+ " @click.stop="handleSecondRightIconClick" :class="[
493
+ { 'bg-grey-100 !text-grey-300': isReadonlyOrDisabled },
494
+ secondRightIconClass,
495
+ ]" />
496
+ <FzIcon v-if="rightIcon && !rightIconButton" :name="rightIcon" size="md" :variant="rightIconVariant"
544
497
  :role="isRightIconAccessible ? 'button' : undefined"
545
- :aria-label="isRightIconAccessible ? rightIconAriaLabel : undefined"
546
- :aria-disabled="
547
- isRightIconAccessible && isReadonlyOrDisabled ? 'true' : undefined
548
- "
549
- :tabindex="
550
- isRightIconAccessible && !isReadonlyOrDisabled ? 0 : undefined
551
- "
552
- :class="rightIconClass"
553
- @click.stop="handleRightIconClick"
554
- @keydown="
555
- isRightIconAccessible
556
- ? (e: KeyboardEvent) =>
557
- handleIconKeydown(e, 'fzinput:right-icon-click')
558
- : undefined
559
- "
560
- />
561
- <FzIconButton
562
- v-if="rightIcon && rightIconButton"
563
- :iconName="rightIcon"
564
- size="md"
565
- :iconVariant="rightIconVariant"
566
- :variant="
567
- isReadonlyOrDisabled ? 'invisible' : rightIconButtonVariant
568
- "
569
- @click.stop="handleRightIconClick"
570
- :class="[
571
- { 'bg-grey-100 !text-grey-300': isReadonlyOrDisabled },
572
- rightIconClass,
573
- ]"
574
- />
575
- <FzIcon
576
- v-if="valid"
577
- name="check"
578
- size="md"
579
- class="text-semantic-success"
580
- aria-hidden="true"
581
- />
582
- </div>
583
- </slot>
498
+ :aria-label="isRightIconAccessible ? rightIconAriaLabel : undefined" :aria-disabled="isRightIconAccessible && isReadonlyOrDisabled ? 'true' : undefined
499
+ " :tabindex="isRightIconAccessible && !isReadonlyOrDisabled ? 0 : undefined
500
+ " :class="rightIconClass" @click.stop="handleRightIconClick" @keydown="
501
+ isRightIconAccessible
502
+ ? (e: KeyboardEvent) =>
503
+ handleIconKeydown(e, 'fzinput:right-icon-click')
504
+ : undefined
505
+ " />
506
+ <FzIconButton v-if="rightIcon && rightIconButton" :iconName="rightIcon" size="md"
507
+ :iconVariant="rightIconVariant" :variant="isReadonlyOrDisabled ? 'invisible' : rightIconButtonVariant
508
+ " @click.stop="handleRightIconClick" :class="[
509
+ { 'bg-grey-100 !text-grey-300': isReadonlyOrDisabled },
510
+ rightIconClass,
511
+ ]" />
512
+ <FzIcon v-if="valid" name="check" size="md" class="text-semantic-success" aria-hidden="true" />
513
+ </slot>
514
+ </div>
584
515
  </div>
585
- <FzAlert
586
- v-if="error && $slots.errorMessage"
587
- :id="`${uniqueId}-error`"
588
- role="alert"
589
- tone="error"
590
- variant="text"
591
- :style="{ width: containerWidth }"
592
- >
516
+ <FzAlert v-if="error && $slots.errorMessage" :id="`${uniqueId}-error`" role="alert" tone="error" variant="text"
517
+ :style="{ width: containerWidth }">
593
518
  <slot name="errorMessage"></slot>
594
519
  </FzAlert>
595
- <span
596
- v-else-if="$slots.helpText"
597
- :id="`${uniqueId}-help`"
598
- :class="[computedHelpClass]"
599
- :style="{ width: containerWidth }"
600
- >
520
+ <span v-else-if="$slots.helpText" :id="`${uniqueId}-help`" :class="[computedHelpClass]"
521
+ :style="{ width: containerWidth }">
601
522
  <slot name="helpText"></slot>
602
523
  </span>
603
524
  </div>