/
KtFieldText.vue
76 lines (68 loc) 路 1.77 KB
/
KtFieldText.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
<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_TEXT_SUPPORTS } from './constants'
import { KottiFieldText } from './types'
export default defineComponent({
name: 'KtFieldText',
components: { KtField },
props: {
...KOTTI_FIELD_PROPS,
value: { default: null, type: String },
},
setup(props: KottiFieldText.Props, { emit }) {
const field = useField<KottiFieldText.Value, string | null>({
emit,
isCorrectDataType: (value): value is KottiFieldText.Value =>
typeof value === 'string' || value === null,
isEmpty: (value) => value === null,
props,
supports: KOTTI_FIELD_TEXT_SUPPORTS,
})
const { forceUpdate, forceUpdateKey } = useForceUpdate()
return {
field,
inputProps: computed(
(): Partial<HTMLInputElement> & {
class: string[]
forceUpdateKey: number
} => ({
...field.inputProps,
class: ['kt-field-text__wrapper'],
forceUpdateKey: forceUpdateKey.value,
type: 'text',
size: 1,
value: field.currentValue ?? '',
placeholder: props.placeholder ?? undefined,
}),
),
onInput: (event: { target: HTMLInputElement }) => {
const newValue = event.target.value
field.setValue(newValue === '' ? null : newValue)
forceUpdate()
},
}
},
})
</script>
<style lang="scss">
.kt-field-text__wrapper {
display: flex;
width: 100%;
padding: 0;
margin: 0;
line-height: 1.6;
border: 0;
}
</style>