UNPKG

rune

Version:

CLI to upload your games to Rune

24 lines (23 loc) 1.15 kB
import figures from "figures"; import { useInput, Box, Text } from "ink"; import React, { useState } from "react"; import { DisappearingHint } from "./DisappearingHint.js"; export function Choose({ options, onSubmit, }) { const [selectedIndex, setSelectedIndex] = useState(0); useInput((_, key) => { if (key.downArrow || key.rightArrow) { setSelectedIndex(selectedIndex < options.length - 1 ? selectedIndex + 1 : 0); } else if (key.upArrow || key.leftArrow) { setSelectedIndex(selectedIndex > 0 ? selectedIndex - 1 : options.length - 1); } else if (key.return) { onSubmit(options[selectedIndex]); } }); return (React.createElement(Box, null, options.map((option, i) => (React.createElement(Box, { key: option, paddingLeft: i > 0 ? 1 : 0 }, React.createElement(Text, { underline: i === selectedIndex, dimColor: i !== selectedIndex }, option)))), React.createElement(Box, { paddingLeft: 4 }, React.createElement(DisappearingHint, { text: `Use arrow keys ${figures.arrowLeft}${figures.arrowRight} to choose` })))); }