This commit is contained in:
Sebastian 2021-11-24 18:00:03 -03:00
parent f49df12b44
commit db49eac6a7
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
2 changed files with 21 additions and 9 deletions

View File

@ -2,6 +2,7 @@ import { h, VNode } from "preact";
import { useLayoutEffect, useRef, useState } from "preact/hooks"; import { useLayoutEffect, useRef, useState } from "preact/hooks";
export interface TextInputProps { export interface TextInputProps {
inputType?: "text" | "number" | "multiline" | "password";
label: string; label: string;
grabFocus?: boolean; grabFocus?: boolean;
disabled?: boolean; disabled?: boolean;
@ -12,13 +13,22 @@ export interface TextInputProps {
bind: [string, (x: string) => void]; bind: [string, (x: string) => void];
} }
export function TextInput(props: TextInputProps): VNode { const TextInputType = function ({ inputType, grabFocus, ...rest }: any): VNode {
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
useLayoutEffect(() => { useLayoutEffect(() => {
if (props.grabFocus) { if (grabFocus) {
inputRef.current?.focus(); inputRef.current?.focus();
} }
}, [props.grabFocus]); }, [grabFocus]);
return inputType === "multiline" ? (
<textarea {...rest} rows={5} ref={inputRef} style={{ height: "unset" }} />
) : (
<input {...rest} type={inputType} ref={inputRef} />
);
};
export function TextInput(props: TextInputProps): VNode {
const value = props.bind[0]; const value = props.bind[0];
const [dirty, setDirty] = useState(false); const [dirty, setDirty] = useState(false);
const showError = dirty && props.error; const showError = dirty && props.error;
@ -33,21 +43,22 @@ export function TextInput(props: TextInputProps): VNode {
)} )}
</label> </label>
<div class="control has-icons-right"> <div class="control has-icons-right">
<input <TextInputType
inputType={props.inputType}
value={value} value={value}
grabFocus={props.grabFocus}
disabled={props.disabled} disabled={props.disabled}
placeholder={props.placeholder} placeholder={props.placeholder}
class={showError ? "input is-danger" : "input"} class={showError ? "input is-danger" : "input"}
onKeyPress={(e) => { onKeyPress={(e: any) => {
if (e.key === 'Enter' && props.onConfirm) { if (e.key === "Enter" && props.onConfirm) {
props.onConfirm() props.onConfirm();
} }
}} }}
onInput={(e) => { onInput={(e: any) => {
setDirty(true); setDirty(true);
props.bind[1]((e.target as HTMLInputElement).value); props.bind[1]((e.target as HTMLInputElement).value);
}} }}
ref={inputRef}
style={{ display: "block" }} style={{ display: "block" }}
/> />
</div> </div>

View File

@ -83,6 +83,7 @@ export function SecretEditorScreen(): VNode {
</div> </div>
<div class="block"> <div class="block">
<TextInput <TextInput
inputType="multiline"
disabled={!!secretFile} disabled={!!secretFile}
onConfirm={goNextIfNoErrors} onConfirm={goNextIfNoErrors}
label="Enter the secret as text:" label="Enter the secret as text:"