Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/components/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@ const name = ref('')
## Input component API

### FwbInput Props
| Name | Type | Default | Description |
| ------------ | ---------------- | ------- | ------------------------------------------------------------ |
| wrapperClass | String \| Object | `''` | Added to main component wrapper |
| labelClass | String \| Object | `''` | Added to `<label>` element. |
| class | String \| Object | `''` | Added to wrapper around `<input>` element and prefix/suffix. |
| inputClass | String \| Object | `''` | Added to `<input>` element. |
| Name | Type | Default | Description |
| ------------ | ------------------------ | ------- | ------------------------------------------------------------ |
| autocomplete | String \| CommonAutoFill | 'off' | Sets the autocomplete for forms. |
| wrapperClass | String \| Object | `''` | Added to main component wrapper |
| labelClass | String \| Object | `''` | Added to `<label>` element. |
| class | String \| Object | `''` | Added to wrapper around `<input>` element and prefix/suffix. |
| inputClass | String \| Object | `''` | Added to `<input>` element. |
22 changes: 6 additions & 16 deletions src/components/FwbInput/FwbInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div :class="wrapperClass">
<label
v-if="label"
:for="inputId"
:class="labelClass"
>{{ label }}</label>
<div :class="inputWrapperClass">
Expand All @@ -12,7 +13,7 @@
<slot name="prefix" />
</div>
<input
v-bind="$attrs"
v-bind="inputAttributes"
v-model="model"
:autocomplete="autocomplete"
:class="inputClass"
Expand Down Expand Up @@ -46,23 +47,10 @@

import { toRefs } from 'vue'

import { useInputAttributes } from './composables/useInputAttributes'
import { useInputClasses } from './composables/useInputClasses'

import type { CommonAutoFill, InputSize, InputType, ValidationStatus } from './types'

interface InputProps {
autocomplete?: CommonAutoFill
class?: string | Record<string, boolean>
disabled?: boolean
inputClass?: string | Record<string, boolean>
label?: string
labelClass?: string | Record<string, boolean>
required?: boolean
size?: InputSize
type?: InputType
validationStatus?: ValidationStatus
wrapperClass?: string | Record<string, boolean>
}
import type { InputProps } from './types'

defineOptions({
inheritAttrs: false,
Expand Down Expand Up @@ -92,4 +80,6 @@ const {
inputWrapperClass,
inputClass,
} = useInputClasses(toRefs(props))

const { inputId, inputAttributes } = useInputAttributes(props)
</script>
21 changes: 21 additions & 0 deletions src/components/FwbInput/composables/useInputAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { computed, useAttrs } from 'vue'

import type { InputProps } from '../types'

export const useInputAttributes = (props: InputProps) => {
const attrs = useAttrs()
const inputId = props?.label && props.label !== '' ? props.label.toLowerCase().trim().replace(/ /g, '-') : ''

const inputAttributes = computed(() => {
if (inputId !== '') {
return {
...attrs,
id: inputId,
}
} else {
return attrs
}
})

return { inputId, inputAttributes }
}
14 changes: 14 additions & 0 deletions src/components/FwbInput/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ export type InputType = 'button' | 'checkbox' | 'color' | 'date' | 'datetime-loc
// A simplified version of AutFill, which is to complex for TypeScript to handle
export type CommonAutoFill = 'on' | 'off' | 'email' | 'tel' | 'name' | 'username' | 'current-password' | 'country' | 'postal-code' | 'language' | 'bday'

export interface InputProps {
autocomplete?: CommonAutoFill
class?: string | Record<string, boolean>
disabled?: boolean
inputClass?: string | Record<string, boolean>
label?: string
labelClass?: string | Record<string, boolean>
required?: boolean
size?: InputSize
type?: InputType
validationStatus?: ValidationStatus
wrapperClass?: string | Record<string, boolean>
}

export const validationStatusMap = {
Error: 'error',
Success: 'success',
Expand Down