fix: input element

This commit is contained in:
Juan Di Toro 2023-10-20 10:48:22 +02:00
parent 3be84cc724
commit 0e3266c3ba
2 changed files with 22 additions and 14 deletions

View File

@ -17,9 +17,7 @@ const App: React.FC = () => {
<LumeStatusProvider> <LumeStatusProvider>
<AuthProvider> <AuthProvider>
<header className="relative h-14 px-2 pl-2 py-2 w-full bg-neutral-900 flex"> <header className="relative h-14 px-2 pl-2 py-2 w-full bg-neutral-900 flex">
<div className="relative h-full w-full rounded-full bg-neutral-800 border border-neutral-700 flex items-center [>input:focus]:ring-2 [>input:focus]:ring-white">
<Navigator /> <Navigator />
</div>
<div className="w-32 flex justify-end"> <div className="w-32 flex justify-end">
<NetworksProvider> <NetworksProvider>
<Lume /> <Lume />

View File

@ -135,12 +135,16 @@ async function bootup() {
} }
} }
const NavInput = forwardRef<HTMLInputElement>((props: React.HTMLProps<HTMLInputElement>, ref) => {
return <Input ref={ref} {...props}></Input>;
});
export function Navigator() { export function Navigator() {
const { url: contextUrl, setUrl } = useBrowserState(); const { url: contextUrl, setUrl } = useBrowserState();
const [inputValue, setInputValue] = useState(contextUrl); // Local state for the input value
const { ready } = useLumeStatus(); const { ready } = useLumeStatus();
const inputEl = useRef<HTMLInputElement>();
const browse = () => { const browse = (inputValue: string) => {
let input = inputValue.trim(); let input = inputValue.trim();
// If the input doesn't contain a protocol, assume it's http // If the input doesn't contain a protocol, assume it's http
@ -160,27 +164,33 @@ export function Navigator() {
}; };
useEffect(() => { useEffect(() => {
setInputValue(contextUrl); // Update local state when context's url changes if(inputEl.current) {
inputEl.current.value = contextUrl;
}
}, [contextUrl]); }, [contextUrl]);
const NavInput = forwardRef((props: any, ref) => {
return <Input ref={ref} {...props}></Input>;
});
console.log("Navigator mounted"); console.log("Navigator mounted");
return ( return (
<> <form onSubmit={(e) => {
e.preventDefault();
const inputElement = e.target as HTMLFormElement;
const inputValue = inputElement?.elements.namedItem('url')?.value;
if (inputValue) {
browse(inputValue)
}
}} className="relative h-full w-full rounded-full bg-neutral-800 border border-neutral-700 flex items-center [>input:focus]:ring-2 [>input:focus]:ring-white">
<NavInput <NavInput
value={inputValue} ref={inputEl}
onChange={(e: any) => setInputValue(e.target.value)}
disabled={!ready} disabled={!ready}
className="rounded-l-full border-none"
name="url"
/> />
<Button onClick={browse} disabled={!ready}> <Button disabled={!ready} className="rounded-r-full">
Navigate Navigate
<Arrow /> <Arrow />
</Button> </Button>
</> </form>
); );
} }