UNPKG

react-use-what-input

Version:
36 lines (31 loc) 1.26 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var react = require('react'); var whatInput = _interopDefault(require('what-input')); function useWhatInput(intent) { var _a = react.useState(whatInput.ask('input')), currentInput = _a[0], setCurrentInput = _a[1]; var _b = react.useState(whatInput.ask('intent')), currentIntent = _b[0], setCurrentIntent = _b[1]; react.useEffect(function () { if (intent !== 'intent') { setCurrentInput(whatInput.ask('input')); whatInput.registerOnChange(setCurrentInput, 'input'); } if (intent !== 'input') { setCurrentIntent(whatInput.ask('intent')); whatInput.registerOnChange(setCurrentIntent, 'intent'); } return function () { whatInput.unRegisterOnChange(setCurrentInput); whatInput.unRegisterOnChange(setCurrentIntent); }; }, [intent]); switch (intent) { case 'input': return currentInput; case 'intent': return currentIntent; default: return [currentInput, currentIntent]; } } module.exports = useWhatInput;