Skip to content

Commit 9d220c2

Browse files
authored
Merge pull request #405 from ChrisWSchultz/bugfix/fix-input-label
Fix FwbInput label/input connection
2 parents 547bd7b + a34284b commit 9d220c2

File tree

4 files changed

+58
-22
lines changed

4 files changed

+58
-22
lines changed

docs/components/input.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ const name = ref('')
254254
## Input component API
255255

256256
### FwbInput Props
257-
| Name | Type | Default | Description |
258-
| ------------ | ---------------- | ------- | ------------------------------------------------------------ |
259-
| wrapperClass | String \| Object | `''` | Added to main component wrapper |
260-
| labelClass | String \| Object | `''` | Added to `<label>` element. |
261-
| class | String \| Object | `''` | Added to wrapper around `<input>` element and prefix/suffix. |
262-
| inputClass | String \| Object | `''` | Added to `<input>` element. |
257+
| Name | Type | Default | Description |
258+
| ------------ | ------------------------ | ------- | ------------------------------------------------------------ |
259+
| autocomplete | String \| CommonAutoFill | 'off' | Sets the autocomplete for forms. |
260+
| wrapperClass | String \| Object | `''` | Added to main component wrapper |
261+
| labelClass | String \| Object | `''` | Added to `<label>` element. |
262+
| class | String \| Object | `''` | Added to wrapper around `<input>` element and prefix/suffix. |
263+
| inputClass | String \| Object | `''` | Added to `<input>` element. |

src/components/FwbInput/FwbInput.vue

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div :class="wrapperClass">
33
<label
44
v-if="label"
5+
:for="inputId"
56
:class="labelClass"
67
>{{ label }}</label>
78
<div :class="inputWrapperClass">
@@ -12,7 +13,7 @@
1213
<slot name="prefix" />
1314
</div>
1415
<input
15-
v-bind="$attrs"
16+
v-bind="inputAttributes"
1617
v-model="model"
1718
:autocomplete="autocomplete"
1819
:class="inputClass"
@@ -46,23 +47,10 @@
4647
4748
import { toRefs } from 'vue'
4849
50+
import { useInputAttributes } from './composables/useInputAttributes'
4951
import { useInputClasses } from './composables/useInputClasses'
5052
51-
import type { CommonAutoFill, InputSize, InputType, ValidationStatus } from './types'
52-
53-
interface InputProps {
54-
autocomplete?: CommonAutoFill
55-
class?: string | Record<string, boolean>
56-
disabled?: boolean
57-
inputClass?: string | Record<string, boolean>
58-
label?: string
59-
labelClass?: string | Record<string, boolean>
60-
required?: boolean
61-
size?: InputSize
62-
type?: InputType
63-
validationStatus?: ValidationStatus
64-
wrapperClass?: string | Record<string, boolean>
65-
}
53+
import type { InputProps } from './types'
6654
6755
defineOptions({
6856
inheritAttrs: false,
@@ -92,4 +80,6 @@ const {
9280
inputWrapperClass,
9381
inputClass,
9482
} = useInputClasses(toRefs(props))
83+
84+
const { inputId, inputAttributes } = useInputAttributes(props)
9585
</script>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { computed, useAttrs } from 'vue'
2+
3+
import type { InputProps } from '../types'
4+
5+
export const useInputAttributes = (props: InputProps) => {
6+
const attrs = useAttrs()
7+
const inputId = computed(() => {
8+
if (!props?.label || props.label.trim() === '') {
9+
return `input-${Math.random().toString(36).slice(2, 9)}`
10+
}
11+
return props.label
12+
.toLowerCase()
13+
.trim()
14+
.replace(/[^\w\s-]/g, '') // Remove special characters
15+
.replace(/\s+/g, '-') // Replace spaces with hyphens
16+
.replace(/-+/g, '-') // Collapse multiple hyphens
17+
})
18+
19+
const inputAttributes = computed(() => {
20+
if (inputId.value !== '') {
21+
return {
22+
...attrs,
23+
id: inputId.value,
24+
}
25+
} else {
26+
return attrs
27+
}
28+
})
29+
30+
return { inputId, inputAttributes }
31+
}

src/components/FwbInput/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ export type InputType = 'button' | 'checkbox' | 'color' | 'date' | 'datetime-loc
55
// A simplified version of AutFill, which is to complex for TypeScript to handle
66
export type CommonAutoFill = 'on' | 'off' | 'email' | 'tel' | 'name' | 'username' | 'current-password' | 'country' | 'postal-code' | 'language' | 'bday'
77

8+
export interface InputProps {
9+
autocomplete?: CommonAutoFill
10+
class?: string | Record<string, boolean>
11+
disabled?: boolean
12+
inputClass?: string | Record<string, boolean>
13+
label?: string
14+
labelClass?: string | Record<string, boolean>
15+
required?: boolean
16+
size?: InputSize
17+
type?: InputType
18+
validationStatus?: ValidationStatus
19+
wrapperClass?: string | Record<string, boolean>
20+
}
21+
822
export const validationStatusMap = {
923
Error: 'error',
1024
Success: 'success',

0 commit comments

Comments
 (0)