Set focus on input
This commit is contained in:
parent
2403816461
commit
f7bdff1653
@ -1,8 +1,16 @@
|
|||||||
import { NextPage } from "next";
|
import { NextPage } from "next";
|
||||||
|
import { MutableRefObject } from "react";
|
||||||
import styles from "../../styles/REPL/REPLHistory.module.css";
|
import styles from "../../styles/REPL/REPLHistory.module.css";
|
||||||
|
|
||||||
const REPLHistory: NextPage<{history: string[]}> = ({history}) => {
|
interface REPLHistoryParams {
|
||||||
return <div className={styles.container}>
|
history: string[];
|
||||||
|
inputRef: MutableRefObject<HTMLInputElement|undefined>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const REPLHistory: NextPage<REPLHistoryParams> = ({history, inputRef}) => {
|
||||||
|
const focusInput = () => {if (inputRef.current) inputRef.current.focus();};
|
||||||
|
|
||||||
|
return <div className={styles.container} onClick={focusInput}>
|
||||||
{ history.map((value, idx) => <div className={styles.line} key={idx}>{value === "" ? "\u00A0" : value}</div>) }
|
{ history.map((value, idx) => <div className={styles.line} key={idx}>{value === "" ? "\u00A0" : value}</div>) }
|
||||||
</div>;
|
</div>;
|
||||||
};
|
};
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
import type { NextPage } from "next";
|
import type { NextPage } from "next";
|
||||||
import React from "react";
|
import React, { MutableRefObject } from "react";
|
||||||
import { commandCompletion, executeCommand } from "../../lib/commands";
|
import { commandCompletion, executeCommand } from "../../lib/commands";
|
||||||
import styles from "../../styles/REPL/REPLInput.module.css";
|
import styles from "../../styles/REPL/REPLInput.module.css";
|
||||||
|
|
||||||
const REPLInput: NextPage<{historyCallback: CallableFunction}> = ({historyCallback}) => {
|
interface REPLInputParams {
|
||||||
|
historyCallback: CallableFunction;
|
||||||
|
inputRef: MutableRefObject<HTMLInputElement|undefined>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const REPLInput: NextPage<REPLInputParams> = ({historyCallback, inputRef}) => {
|
||||||
const typed = React.createRef<HTMLSpanElement>();
|
const typed = React.createRef<HTMLSpanElement>();
|
||||||
const completion = React.createRef<HTMLSpanElement>();
|
const completion = React.createRef<HTMLSpanElement>();
|
||||||
const [currentCmd, setCurrentCmd] = React.useState<string[]>([]);
|
const [currentCmd, setCurrentCmd] = React.useState<string[]>([]);
|
||||||
@ -41,7 +46,7 @@ const REPLInput: NextPage<{historyCallback: CallableFunction}> = ({historyCallba
|
|||||||
return <div className={styles.wrapperwrapper}>
|
return <div className={styles.wrapperwrapper}>
|
||||||
<span className={styles.inputstart}>$ </span>
|
<span className={styles.inputstart}>$ </span>
|
||||||
<div className={styles.wrapper}>
|
<div className={styles.wrapper}>
|
||||||
<input className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={tabComplete} spellCheck={"false"} autoFocus />
|
<input ref={inputRef as MutableRefObject<HTMLInputElement>} className={styles.in} type={"text"} onChange={replinOnChange} onKeyDown={tabComplete} spellCheck={"false"} autoFocus />
|
||||||
<span className={styles.completionWrapper}><span ref={typed} className={styles.typed}></span><span ref={completion} className={styles.completion}></span></span>
|
<span className={styles.completionWrapper}><span ref={typed} className={styles.typed}></span><span ref={completion} className={styles.completion}></span></span>
|
||||||
</div>
|
</div>
|
||||||
</div>;
|
</div>;
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
import { useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
import REPLInput from "./REPLInput";
|
import REPLInput from "./REPLInput";
|
||||||
import REPLHistory from "./REPLHistory";
|
import REPLHistory from "./REPLHistory";
|
||||||
|
|
||||||
const REPL = () => {
|
const REPL = () => {
|
||||||
const [history, manipulateHistory] = useState<string[]>([]);
|
const [history, manipulateHistory] = useState<string[]>([]);
|
||||||
|
const inputRef = useRef<HTMLInputElement>();
|
||||||
const onCommandExecuted = (result: string[]) => manipulateHistory(result.reverse().concat(history).slice(0, 1000));
|
const onCommandExecuted = (result: string[]) => manipulateHistory(result.reverse().concat(history).slice(0, 1000));
|
||||||
|
|
||||||
return (<>
|
return (<>
|
||||||
<REPLHistory history={history} />
|
<REPLHistory history={history} inputRef={inputRef} />
|
||||||
<REPLInput historyCallback={onCommandExecuted} />
|
<REPLInput historyCallback={onCommandExecuted} inputRef={inputRef} />
|
||||||
</>);
|
</>);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user