react-use-what-input
Version:
React hook for what-input integration
36 lines (31 loc) • 1.26 kB
JavaScript
;
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;