Skip to content

Commit b5ef124

Browse files
committed
updated inputId as per refactor suggestion
1 parent 2bd0311 commit b5ef124

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/components/FwbInput/composables/useInputAttributes.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ import type { InputProps } from '../types'
44

55
export const useInputAttributes = (props: InputProps) => {
66
const attrs = useAttrs()
7-
const inputId = props?.label && props.label !== '' ? props.label.toLowerCase().trim().replace(/ /g, '-') : ''
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+
})
818

919
const inputAttributes = computed(() => {
10-
if (inputId !== '') {
20+
if (inputId.value !== '') {
1121
return {
1222
...attrs,
1323
id: inputId,

0 commit comments

Comments
 (0)