From 666fed59f1a590f5fc75eabd815db21b1f40b329 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 7 Mar 2024 15:28:15 +0800 Subject: [PATCH] Refactor tab component --- components/ui/tab/index.tsx | 38 ++++++++++++++++++++++++++ components/ui/tab/tab.module.less | 30 +++++++++++++++++++++ popup/bili.module.less | 1 + popup/bili.tsx | 45 +++++++++++++++---------------- popup/popup.module.less | 31 --------------------- popup/read.tsx | 25 ++++++----------- 6 files changed, 98 insertions(+), 72 deletions(-) create mode 100644 components/ui/tab/index.tsx create mode 100644 components/ui/tab/tab.module.less diff --git a/components/ui/tab/index.tsx b/components/ui/tab/index.tsx new file mode 100644 index 0000000..54b8333 --- /dev/null +++ b/components/ui/tab/index.tsx @@ -0,0 +1,38 @@ +import { IconBack } from "~assets/icons"; +import type { ReactNode, PropsWithChildren } from "react"; + +import styles from "./tab.module.less"; + +interface HeaderProps { + title: ReactNode; + onBack: () => void; +} + +function Tab({ children }: PropsWithChildren) { + return ( +
+ {children} +
+ ); +} + +Tab.Header = function Header({ title, onBack }: HeaderProps) { + return ( +
+ +

{title}

+
+ ); +} + +Tab.Body = function Body({ children }: PropsWithChildren) { + return ( +
+ {children} +
+ ); +} + +export default Tab; diff --git a/components/ui/tab/tab.module.less b/components/ui/tab/tab.module.less new file mode 100644 index 0000000..f9b194c --- /dev/null +++ b/components/ui/tab/tab.module.less @@ -0,0 +1,30 @@ +.tab { + .header { + padding: .75em; + position: relative; + text-align: center; + border-bottom: 1px solid #eee; + + .back { + top: 0; + left: 0; + bottom: 0; + width: 2em; + font-size: 1.3em; + position: absolute; + transition: background-color .3s; + + &:hover { + background-color: #eee; + } + + svg { + width: 1em; + } + } + } + + .body { + padding: 1em; + } +} diff --git a/popup/bili.module.less b/popup/bili.module.less index 8fd02dc..eef7058 100644 --- a/popup/bili.module.less +++ b/popup/bili.module.less @@ -1,5 +1,6 @@ .bili { gap: 1em; + width: 600px; display: flex; .image { diff --git a/popup/bili.tsx b/popup/bili.tsx index 4299bba..9eccce8 100644 --- a/popup/bili.tsx +++ b/popup/bili.tsx @@ -1,12 +1,12 @@ import { useEffect, useState } from "react"; import { sendToBackground } from "@plasmohq/messaging"; -import Placeholder from "~components/ui/placeholder"; -import Form from "~components/ui/form"; -import { IconBack } from "~assets/icons"; -import styles from "./popup.module.less"; -import stylesB from "./bili.module.less"; import useForm from "~hooks/useForm"; +import Tab from "~components/ui/tab"; +import Form from "~components/ui/form"; import { add } from "~components/ui/message/utils"; +import Placeholder from "~components/ui/placeholder"; + +import styles from "./bili.module.less"; interface ReadProps { onBack: () => void; @@ -45,7 +45,6 @@ function Toy({ onBack }: ReadProps) { const [imgs, setImgs] = useState([]); const [currentImg, setCurrentImg] = useState(0); - const [failed, setFailed] = useState(false); const submitForm = (body: FormValue) => { sendToBackground({ @@ -74,37 +73,35 @@ function Toy({ onBack }: ReadProps) { useEffect(() => { getInfo().then((res) => { if (!res) { - setFailed(true); return; } - const { image, ...othors } = res; + const { images, ...others } = res; - setValues(othors); - setImgs(res.images); + setImgs(images); setCurrentImg(0); + + // ! 必须等组件渲染出来才能设置值,这里是个问题 + window.requestAnimationFrame(() => { + setValues(others); + }); }) }, []); return ( -
-
- -

添加手办

-
-
- -
+ + + + +
-
+
{imgs.map((item, index) => ( setCurrentImg(index)} /> ))}
-
+
@@ -141,8 +138,8 @@ function Toy({ onBack }: ReadProps) { -
-
+ + ); } diff --git a/popup/popup.module.less b/popup/popup.module.less index 1417e01..a7b8c27 100644 --- a/popup/popup.module.less +++ b/popup/popup.module.less @@ -27,34 +27,3 @@ background-color: #eee; } } - -.tab { - .header { - padding: .75em; - position: relative; - text-align: center; - border-bottom: 1px solid #eee; - - .back { - top: 0; - left: 0; - bottom: 0; - width: 2em; - font-size: 1.3em; - position: absolute; - transition: background-color .3s; - - &:hover { - background-color: #eee; - } - - svg { - width: 1em; - } - } - } - - .body { - padding: 1em; - } -} diff --git a/popup/read.tsx b/popup/read.tsx index fb36501..2349872 100644 --- a/popup/read.tsx +++ b/popup/read.tsx @@ -1,9 +1,8 @@ -import { useEffect, useState } from "react"; +import { useEffect } from "react"; import { sendToBackground } from "@plasmohq/messaging"; -import Form from "~components/ui/form"; -import { IconBack } from "~assets/icons"; -import styles from "./popup.module.less"; import useForm from "~hooks/useForm"; +import Tab from "~components/ui/tab"; +import Form from "~components/ui/form"; import { add } from "~components/ui/message/utils"; interface ReadProps { @@ -51,7 +50,6 @@ const submitForm = (body: FormValue) => { } function Read({ onBack }: ReadProps) { - const [count, setCount] = useState(0); const { bindInput, setValues, onSubmit } = useForm({ initialValues: { tags: "", @@ -69,14 +67,9 @@ function Read({ onBack }: ReadProps) { }, []); return ( -
-
- -

在看

-
-
+ + +
@@ -106,10 +99,8 @@ function Read({ onBack }: ReadProps) {
- {count} - - - + + ); }