How to use the @inquirer/core/hooks.createPrompt function in @inquirer/core

To help you get started, we’ve selected a few @inquirer/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SBoudrias / Inquirer.js / packages / input / index.js View on Github external
const { createPrompt, useState, useKeypress } = require('@inquirer/core/hooks');
const { usePrefix } = require('@inquirer/core/lib/prefix');
const { isEnterKey, isBackspaceKey } = require('@inquirer/core/lib/key');
const chalk = require('chalk');

module.exports = createPrompt((config, done) => {
  const [status, setStatus] = useState('pending');
  const [defaultValue, setDefaultValue] = useState(config.default);
  const [errorMsg, setError] = useState();
  const [value, setValue] = useState('');

  const isLoading = status === 'loading';
  const prefix = usePrefix(isLoading);

  useKeypress(async (key, rl) => {
    // Ignore keypress while our prompt is doing other processing.
    if (status !== 'pending') {
      return;
    }

    if (isEnterKey(key)) {
      const answer = value || defaultValue || '';
github SBoudrias / Inquirer.js / packages / confirm / index.js View on Github external
const chalk = require('chalk');
const { createPrompt, useState, useKeypress } = require('@inquirer/core/hooks');
const { isEnterKey } = require('@inquirer/core/lib/key');
const { usePrefix } = require('@inquirer/core/lib/prefix');

module.exports = createPrompt((config, done) => {
  const [status, setStatus] = useState('pending');
  const [value, setValue] = useState('');
  const prefix = usePrefix();

  useKeypress((key, rl) => {
    if (isEnterKey(key)) {
      const answer = value ? /^y(es)?/i.test(value) : config.default !== false;
      setValue(answer ? 'yes' : 'no');
      setStatus('done');
      done(answer);
    } else {
      setValue(rl.line);
    }
  });

  let formattedValue = value;
github SBoudrias / Inquirer.js / packages / select / index.js View on Github external
const { createPrompt, useState, useKeypress, useRef } = require('@inquirer/core/hooks');
const { usePrefix } = require('@inquirer/core/lib/prefix');
const { isEnterKey, isUpKey, isDownKey, isNumberKey } = require('@inquirer/core/lib/key');
const Paginator = require('@inquirer/core/lib/Paginator');
const chalk = require('chalk');
const figures = require('figures');
const { cursorHide } = require('ansi-escapes');

module.exports = createPrompt((config, done) => {
  const [status, setStatus] = useState('pending');
  const [cursorPosition, setCursorPos] = useState(0);
  const { choices, pageSize = 7 } = config;
  const paginator = useRef(new Paginator()).current;
  const prefix = usePrefix();

  useKeypress(key => {
    if (isEnterKey(key)) {
      setStatus('done');
      done(choices[cursorPosition].value);
    } else if (isUpKey(key) || isDownKey(key)) {
      let newCursorPosition = cursorPosition;
      const offset = isUpKey(key) ? -1 : 1;
      let selectedOption;

      while (!selectedOption || selectedOption.disabled) {
github SBoudrias / Inquirer.js / packages / expand / index.js View on Github external
const { createPrompt, useState, useKeypress } = require('@inquirer/core/hooks');
const { usePrefix } = require('@inquirer/core/lib/prefix');
const { isEnterKey } = require('@inquirer/core/lib/key');
const chalk = require('chalk');

const helpChoice = {
  key: 'h',
  name: 'Help, list all options',
  value: undefined
};

module.exports = createPrompt((config, done) => {
  const {
    choices,
    default: defaultKey = 'h',
    expanded: defaultExpandState = false
  } = config;
  const [status, setStatus] = useState('pending');
  const [value, setValue] = useState('');
  const [expanded, setExpanded] = useState(defaultExpandState);
  const [errorMsg, setError] = useState();
  const prefix = usePrefix();

  useKeypress((key, rl) => {
    if (isEnterKey(key)) {
      const answer = (value || defaultKey).toLowerCase();
      if (answer === 'h' && !expanded) {
        setExpanded(true);