Skip to content

Commit 2bd0311

Browse files
committed
created useInputAttributes.ts to dynamically create an id attribute for the input element if a label prop has been passed and is not an empty string. preserves any attrs passed to fwb-input. lowercased, trimmed and replaced spaces on the label to create an id. updated the docs to include the autocomplete prop. moved the InputProps interface to the FwbInput/types file
1 parent 16fb867 commit 2bd0311

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 = props?.label && props.label !== '' ? props.label.toLowerCase().trim().replace(/ /g, '-') : ''
8+
9+
const inputAttributes = computed(() => {
10+
if (inputId !== '') {
11+
return {
12+
...attrs,
13+
id: inputId,
14+
}
15+
} else {
16+
return attrs
17+
}
18+
})
19+
20+
return { inputId, inputAttributes }
21+
}

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)