1
1
import { defineComponent , h , ref , computed , watch } from '@vue/runtime-core'
2
+ import type { PropType } from '@vue/runtime-core'
2
3
import chalk from 'chalk'
3
4
import { TuiText } from './Text'
4
5
import { onInputData } from '../composables/input'
5
6
import type { KeyDataEvent } from '../input/types'
6
7
import { useFocus } from '../focus/Focusable'
7
8
8
9
const SKIP_EVENT_KEY = [ 'ArrowUp' , 'ArrowDown' , 'Ctrl' , 'Tab' , 'Shift' ]
10
+ const PWD_FIGURE = '*'
9
11
10
12
export const TuiInput = defineComponent ( {
11
13
props : {
12
14
placeholder : {
13
15
type : String ,
14
- required : false ,
15
16
default : '' ,
16
17
} ,
17
18
modelValue : {
18
19
type : String ,
19
20
required : true ,
20
21
} ,
22
+ type : {
23
+ type : String as PropType < 'text' | 'password' > ,
24
+ default : 'text' ,
25
+ } ,
21
26
focus : {
22
27
type : Boolean ,
23
28
required : true ,
@@ -36,19 +41,29 @@ export const TuiInput = defineComponent({
36
41
props . modelValue &&
37
42
props . modelValue . length <= cursorOffset . value
38
43
) {
39
- return props . modelValue + chalk . inverse ( ' ' )
44
+ return (
45
+ ( props . type === 'text'
46
+ ? props . modelValue
47
+ : PWD_FIGURE . repeat ( props . modelValue . length ) ) +
48
+ chalk . inverse ( ' ' )
49
+ )
40
50
}
41
51
42
52
const l = props . modelValue . slice ( 0 , cursorOffset . value )
43
53
const m = chalk . inverse ( props . modelValue [ cursorOffset . value ] )
44
54
const r = props . modelValue . slice ( cursorOffset . value + 1 )
45
55
46
- return l + m + r
56
+ return props . type === 'text'
57
+ ? l + m + r
58
+ : PWD_FIGURE . repeat ( l . length ) +
59
+ chalk . inverse ( PWD_FIGURE ) +
60
+ PWD_FIGURE . repeat ( r . length )
47
61
} else {
48
62
return props . placeholder ? '' : chalk . inverse ( ' ' )
49
63
}
50
64
} else {
51
- return props . modelValue
65
+ const value = props . modelValue
66
+ return props . type === 'text' ? value : PWD_FIGURE . repeat ( value . length )
52
67
}
53
68
} )
54
69
0 commit comments