-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathTextPrompt.tsx
More file actions
150 lines (136 loc) · 4.37 KB
/
TextPrompt.tsx
File metadata and controls
150 lines (136 loc) · 4.37 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {InlineToken, TokenItem, TokenizedText} from './TokenizedText.js'
import {TextInput} from './TextInput.js'
import {handleCtrlC} from '../../ui.js'
import useLayout from '../hooks/use-layout.js'
import {messageWithPunctuation} from '../utilities.js'
import {AbortSignal} from '../../../../public/node/abort.js'
import useAbortSignal from '../hooks/use-abort-signal.js'
import useDeferredUnmount from '../hooks/use-deferred-unmount.js'
import usePrompt, {PromptState} from '../hooks/use-prompt.js'
import React, {FunctionComponent, useCallback, useEffect, useState} from 'react'
import {Box, useInput, Text} from 'ink'
import figures from 'figures'
export interface TextPromptProps {
message: TokenItem
onSubmit: (value: string) => void
defaultValue?: string
password?: boolean
validate?: (value: string) => string | undefined
allowEmpty?: boolean
emptyDisplayedValue?: string
abortSignal?: AbortSignal
preview?: (value: string) => TokenItem<InlineToken>
initialAnswer?: string
}
const TextPrompt: FunctionComponent<TextPromptProps> = ({
message,
onSubmit,
validate,
defaultValue = '',
password = false,
allowEmpty = false,
emptyDisplayedValue = '(empty)',
abortSignal,
preview,
initialAnswer = '',
}) => {
if (password && defaultValue) {
throw new Error("Can't use defaultValue with password")
}
const validateAnswer = useCallback(
(value: string): string | undefined => {
if (validate) {
return validate(value)
}
if (value.length === 0 && !allowEmpty) return 'Type an answer to the prompt.'
return undefined
},
[allowEmpty, validate],
)
const {oneThird} = useLayout()
const {promptState, setPromptState, answer, setAnswer} = usePrompt<string>({
initialAnswer,
})
const answerOrDefault = answer.length > 0 ? answer : defaultValue
const displayEmptyValue = answerOrDefault === ''
const displayedAnswer = displayEmptyValue ? emptyDisplayedValue : answerOrDefault
const unmountInk = useDeferredUnmount()
const [error, setError] = useState<string | undefined>(undefined)
const color = promptState === PromptState.Error ? 'red' : 'cyan'
const underline = new Array(oneThird - 3).fill('▔')
const {isAborted} = useAbortSignal(abortSignal)
useInput((input, key) => {
handleCtrlC(input, key)
if (key.return) {
const error = validateAnswer(answerOrDefault)
if (error) {
setPromptState(PromptState.Error)
setError(error)
} else {
setPromptState(PromptState.Submitted)
}
}
})
useEffect(() => {
if (promptState === PromptState.Submitted) {
onSubmit(answerOrDefault)
unmountInk()
}
}, [answerOrDefault, onSubmit, promptState, unmountInk])
return isAborted ? null : (
<Box flexDirection="column" marginBottom={1} width={oneThird}>
<Box>
<Box marginRight={2}>
<Text>?</Text>
</Box>
<TokenizedText item={messageWithPunctuation(message)} />
</Box>
{promptState === PromptState.Submitted ? (
<Box>
<Box width={3}>
<Text color="cyan">{figures.tick}</Text>
</Box>
<Box flexGrow={1}>
<Text color="cyan" dimColor={displayEmptyValue}>
{password ? '*'.repeat(answer.length) : displayedAnswer}
</Text>
</Box>
</Box>
) : (
<Box flexDirection="column">
<Box>
<Box marginRight={2}>
<Text color={color}>{`>`}</Text>
</Box>
<Box flexGrow={1}>
<TextInput
value={answer}
onChange={(answer) => {
setAnswer(answer)
setPromptState(PromptState.Idle)
}}
defaultValue={defaultValue}
color={color}
password={password}
/>
</Box>
</Box>
<Box marginLeft={3}>
<Text color={color}>{underline}</Text>
</Box>
{promptState === PromptState.Error ? (
<Box marginLeft={3}>
<Text color={color}>{error}</Text>
</Box>
) : null}
{promptState !== PromptState.Error && preview ? (
<Box marginLeft={3}>
<TokenizedText item={preview(answerOrDefault)} />
</Box>
) : null}
</Box>
)}
</Box>
)
}
export {TextPrompt}