From 7f6c405890db72abd1872d9840fc31fd76d829d8 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad <600878@stud.hvl.no> Date: Sat, 8 Apr 2023 19:24:10 +0200 Subject: [PATCH] Set ts compiler to strict, refactored a bit --- src/components/button.tsx | 17 +- src/components/card.tsx | 33 ++-- src/components/dialog.tsx | 30 ++-- src/components/footer.tsx | 12 +- src/components/header.tsx | 32 ++-- src/components/input.tsx | 81 +++++++-- src/components/layout.tsx | 27 +-- src/components/link.tsx | 19 +- src/components/output.tsx | 90 +++++----- src/components/row.tsx | 12 +- src/components/truth-table.tsx | 66 ++++--- src/truth-table.tsx | 272 +++++++++++++---------------- src/types/types.d.ts | 2 +- src/utils/dom.ts | 9 + src/{functions => utils}/export.ts | 0 src/utils/expressionUtils.ts | 16 ++ src/{functions => utils}/touch.ts | 0 tsconfig.json | 3 +- 18 files changed, 391 insertions(+), 330 deletions(-) create mode 100644 src/utils/dom.ts rename src/{functions => utils}/export.ts (100%) create mode 100644 src/utils/expressionUtils.ts rename src/{functions => utils}/touch.ts (100%) diff --git a/src/components/button.tsx b/src/components/button.tsx index e52e413..262c008 100644 --- a/src/components/button.tsx +++ b/src/components/button.tsx @@ -49,13 +49,10 @@ export const Button: Component = ( onClick, type = "button", } -) => { - return ( - - ); -}; - +) => ( + +); diff --git a/src/components/card.tsx b/src/components/card.tsx index 6b5f1af..e199a3d 100644 --- a/src/components/card.tsx +++ b/src/components/card.tsx @@ -3,21 +3,22 @@ import { type Component } from "solid-js"; import type { CardProps } from "../types/types"; import { Link } from "./link"; -const Card: Component = ({ children, className, title, to, newTab = false }) => { - return ( - <> -
-
- -

{ title }

- - { children } -
-
- - - ); -}; +const Card: Component = ( + { + children, + className, + title, + to, + newTab = false + }) => ( +
+
+ +

{ title }

+ + { children } +
+
+); export default Card; diff --git a/src/components/dialog.tsx b/src/components/dialog.tsx index b987a66..7355f81 100644 --- a/src/components/dialog.tsx +++ b/src/components/dialog.tsx @@ -1,22 +1,23 @@ /* @refresh reload */ import { Dialog, DialogDescription, DialogPanel, DialogTitle } from "solid-headless"; import type { TitleProps } from "../types/types"; -import { createEffect, createSignal, JSX } from "solid-js"; +import { Component, createEffect, createSignal, JSX } from "solid-js"; import { Button } from "./button"; import { Portal } from "solid-js/web"; +import { getElementById } from "../utils/dom"; interface MyDialog extends TitleProps { description?: string, button?: JSX.Element, - acceptButtonName?: string | null, + acceptButtonName?: string, acceptButtonId?: string, - cancelButtonName?: string | null, + cancelButtonName?: string, callback?: () => void, - buttonClasses?: string, + buttonClass?: string, buttonTitle?: string | null, } -export default function MyDialog( +const MyDialog: Component = ( { title, description, @@ -26,10 +27,10 @@ export default function MyDialog( children, callback, className, - buttonClasses, + buttonClass, buttonTitle, acceptButtonId, - }: MyDialog): JSX.Element { + }) => { const [isOpen, setIsOpen] = createSignal(false); @@ -48,20 +49,21 @@ export default function MyDialog( * @param e KeyboardEvent of keypress */ function click(e: KeyboardEvent): void { - if (isMounted && e.key === "Enter") { - (document.getElementById(acceptButtonId ?? "") as HTMLButtonElement | null)?.click(); + if (isMounted && e.key === "Enter" && acceptButtonId) { + getElementById(acceptButtonId)?.click(); } } if (isOpen()) { const id = "cl-6" - const el = document.getElementById(id); - el?.addEventListener("keypress", e => click(e)); + const el = getElementById(id); + el?.addEventListener("keypress", click); return () => { - el?.removeEventListener("keypress", e => click(e)); + el?.removeEventListener("keypress", click); isMounted = false; } } + else return () => undefined; } createEffect(setupKeyPress, isOpen()); @@ -69,7 +71,7 @@ export default function MyDialog( return (
- @@ -99,3 +101,5 @@ export default function MyDialog(
); } + +export default MyDialog; diff --git a/src/components/footer.tsx b/src/components/footer.tsx index 159c66b..3c627aa 100644 --- a/src/components/footer.tsx +++ b/src/components/footer.tsx @@ -3,12 +3,10 @@ import { type Component } from "solid-js"; import type { SimpleProps } from "../types/types"; import { Link } from "./link"; -const Footer: Component = ({ className }) => { - return ( -
-

Kildekode på GitHub

-
- ); -}; +const Footer: Component = ({ className }) => ( +
+

Kildekode på GitHub

+
+); export default Footer; diff --git a/src/components/header.tsx b/src/components/header.tsx index 181fbc0..dd3761e 100644 --- a/src/components/header.tsx +++ b/src/components/header.tsx @@ -5,24 +5,22 @@ import { Icon } from "solid-heroicons"; import { chevronLeft } from "solid-heroicons/solid"; import { Link } from "./link"; -const Header: Component = ({ className, title }) => { - return ( -
-
+const Header: Component = ({ className, title = "Title goes here" }) => ( +
+
- - - - - + + + + + -

{ title }

-
-
-

Av Martin Berg Alstad

-
-
- ); -}; +

{ title }

+
+
+

Av Martin Berg Alstad

+
+
+); export default Header; diff --git a/src/components/input.tsx b/src/components/input.tsx index 2d14f03..63be4df 100644 --- a/src/components/input.tsx +++ b/src/components/input.tsx @@ -1,7 +1,10 @@ /* @refresh reload */ -import { type Component, createSignal, JSX, Setter } from "solid-js"; +import { type Component, createSignal, JSX, onMount, Setter, Show } from "solid-js"; import type { InputProps } from "../types/types"; import Row from "./row"; +import { Icon } from "solid-heroicons"; +import { magnifyingGlass, xMark } from "solid-heroicons/solid"; +import { getElementById } from "../utils/dom"; function setupEventListener(id: string, setIsHover: Setter): () => void { let isMounted = true; @@ -12,7 +15,7 @@ function setupEventListener(id: string, setIsHover: Setter): () => void } } - const el = document.getElementById(id); + const el = getElementById(id); el?.addEventListener("pointerenter", () => hover(true)); el?.addEventListener("pointerleave", () => hover(false)); @@ -29,7 +32,7 @@ function setupEventListener(id: string, setIsHover: Setter): () => void */ function setSetIsText(id: string | undefined, isText: boolean, setIsText: Setter): void { if (id) { - const el = document.getElementById(id) as HTMLInputElement | HTMLTextAreaElement | null; + const el = getElementById(id); if (el && el.value !== "" !== isText) { setIsText(el.value !== ""); } @@ -43,7 +46,7 @@ interface Input extends InputProps { inputClass?: string, } -export const Input: Component> = ( +export const Input: Component> = ( // TODO remove leading and trailing from component { className, id, @@ -71,7 +74,7 @@ export const Input: Component> = ( */ const [isText, setIsText] = createSignal(false); - document.addEventListener("DOMContentLoaded", () => { + onMount(() => { if (id && title) { setupEventListener(id, setIsHover); } @@ -102,19 +105,71 @@ export const Input: Component> = ( ); } -function HoverTitle( +const HoverTitle: Component<{ title?: string, isActive?: boolean, htmlFor?: string }> = ( { title, isActive = false, htmlFor - }: { title?: string | null, isActive?: boolean, htmlFor?: string }): JSX.Element { - return ( -