Skip to content

Commit

Permalink
@inquirer/select TS migration (#1134)
Browse files Browse the repository at this point in the history
* feat: @inquirer/select to typescript

* fix: eslint fix
  • Loading branch information
TDP17 committed Jul 8, 2022
1 parent 00a900f commit 33ce11d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/select/demo.js → packages/select/demo.ts
@@ -1,4 +1,4 @@
import select from './index.js';
import select from './src/index.js';

(async () => {
let answer;
Expand Down
9 changes: 8 additions & 1 deletion packages/select/package.json
Expand Up @@ -3,7 +3,11 @@
"type": "module",
"version": "0.0.21-alpha.0",
"description": "Inquirer select/list prompt",
"main": "index.js",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist/"
],
"repository": "SBoudrias/Inquirer.js",
"keywords": [
"cli",
Expand All @@ -20,6 +24,9 @@
"chalk": "^5.0.1",
"figures": "^4.0.1"
},
"scripts": {
"tsc": "tsc"
},
"publishConfig": {
"access": "public"
}
Expand Down
14 changes: 10 additions & 4 deletions packages/select/index.js → packages/select/src/index.ts
Expand Up @@ -9,12 +9,18 @@ import {
isDownKey,
isNumberKey,
Paginator,
AsyncPromptConfig,
} from '@inquirer/core';
import chalk from 'chalk';
import figures from 'figures';
import ansiEscapes from 'ansi-escapes';

export default createPrompt((config, done) => {
type SelectConfig = AsyncPromptConfig & {
choices: { value: string; name?: string; description?: string; disabled?: boolean }[];
pageSize?: number;
};

export default createPrompt<string, SelectConfig>((config, done) => {
const [status, setStatus] = useState('pending');
const [cursorPosition, setCursorPos] = useState(0);
const { choices, pageSize = 7 } = config;
Expand All @@ -24,7 +30,7 @@ export default createPrompt((config, done) => {
useKeypress((key) => {
if (isEnterKey(key)) {
setStatus('done');
done(choices[cursorPosition].value);
done(choices[cursorPosition]!.value);
} else if (isUpKey(key) || isDownKey(key)) {
let newCursorPosition = cursorPosition;
const offset = isUpKey(key) ? -1 : 1;
Expand All @@ -42,7 +48,7 @@ export default createPrompt((config, done) => {
const newCursorPosition = Number(key.name) - 1;

// Abort if the choice doesn't exists or if disabled
if (!choices[newCursorPosition] || choices[newCursorPosition].disabled) {
if (!choices[newCursorPosition] || choices[newCursorPosition]!.disabled) {
return;
}

Expand All @@ -53,7 +59,7 @@ export default createPrompt((config, done) => {
const message = chalk.bold(config.message);

if (status === 'done') {
const choice = choices[cursorPosition];
const choice = choices[cursorPosition]!;
return `${prefix} ${message} ${chalk.cyan(choice.name || choice.value)}`;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/select/tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}

0 comments on commit 33ce11d

Please sign in to comment.