/
KtFieldPassword.vue
83 lines (75 loc) 路 1.98 KB
/
KtFieldPassword.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<template>
<KtField
v-bind="{ field }"
:getEmptyValue="() => null"
:helpTextSlot="$slots.helpText"
>
<input v-bind="inputProps" @input="onInput" />
</KtField>
</template>
<script lang="ts">
import { defineComponent, computed } from '@vue/composition-api'
import { KtField } from '../kotti-field'
import { KOTTI_FIELD_PROPS } from '../kotti-field/constants'
import { useField, useForceUpdate } from '../kotti-field/hooks'
import { KOTTI_FIELD_PASSWORD_SUPPORTS } from './constants'
import { KottiFieldPassword } from './types'
export default defineComponent({
name: 'KtFieldPassword',
components: { KtField },
props: {
...KOTTI_FIELD_PROPS,
autoComplete: {
required: true,
type: String,
validator: (value) =>
['current-password', 'new-password'].includes(value),
},
value: { default: null, type: String },
},
setup(props: KottiFieldPassword.Props, { emit }) {
const field = useField<KottiFieldPassword.Value, string | null>({
emit,
isCorrectDataType: (value): value is KottiFieldPassword.Value =>
typeof value === 'string' || value === null,
isEmpty: (value) => value === null,
props,
supports: KOTTI_FIELD_PASSWORD_SUPPORTS,
})
const { forceUpdate, forceUpdateKey } = useForceUpdate()
return {
field,
inputProps: computed(
(): Partial<HTMLInputElement> & {
class: string[]
forceUpdateKey: number
} => ({
...field.inputProps,
autocomplete: props.autoComplete,
class: ['kt-field-password__wrapper'],
forceUpdateKey: forceUpdateKey.value,
placeholder: props.placeholder ?? undefined,
type: 'password',
size: 1,
value: field.currentValue ?? '',
}),
),
onInput: (event: { target: HTMLInputElement }) => {
const newValue = event.target.value
field.setValue(newValue === '' ? null : newValue)
forceUpdate()
},
}
},
})
</script>
<style lang="scss">
.kt-field-password__wrapper {
display: flex;
width: 100%;
padding: 0;
margin: 0;
line-height: 1.6;
border: 0;
}
</style>