-
Notifications
You must be signed in to change notification settings - Fork 0
Steve/card-box #1488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
weareoutman
wants to merge
2
commits into
master
Choose a base branch
from
steve/card-box
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Steve/card-box #1488
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 卡片项容器 | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Basic | ||
|
|
||
| ```yaml preview | ||
| brick: eo-card-box | ||
| children: | ||
| - brick: eo-avatar | ||
| slot: avatar | ||
| properties: | ||
| icon: | ||
| lib: easyops | ||
| icon: account | ||
| color: var(--theme-blue-color) | ||
| bgColor: var(--theme-blue-background) | ||
| - brick: span | ||
| slot: title | ||
| properties: | ||
| textContent: Hello | ||
| - brick: span | ||
| slot: description | ||
| properties: | ||
| textContent: World | ||
| - brick: eo-tag-list | ||
| slot: footer | ||
| properties: | ||
| size: small | ||
| list: | ||
| - text: IT 资源管理 | ||
| key: IT_resource_management | ||
| color: gray | ||
| - text: 资源套餐 | ||
| key: resource_package | ||
| color: gray | ||
| - text: 存储设备 | ||
| key: storage_device | ||
| color: gray | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // Merge bricks | ||
| import "./button/index.js"; | ||
| import "./link/index.js"; | ||
| import "./tooltip/index.js"; | ||
| import "./button/index.js"; | ||
| import "./data-providers/show-dialog/show-dialog.js"; | ||
| import "./data-providers/show-notification/show-notification.js"; | ||
| import "./tooltip/index.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, test, expect, jest } from "@jest/globals"; | ||
| import { act } from "react-dom/test-utils"; | ||
| import { createHistory } from "@next-core/runtime"; | ||
| import "../link/index"; | ||
| import "./"; | ||
| import type { CardBox } from "./index.js"; | ||
|
|
||
| jest.mock("@next-core/theme", () => ({})); | ||
|
|
||
| createHistory(); | ||
|
|
||
| describe("eo-card-box", () => { | ||
| test("basic usage", async () => { | ||
| const element = document.createElement("eo-card-box") as CardBox; | ||
| element.href = "/test"; | ||
|
|
||
| const slots = ["avatar", "title", "description", "", "footer"]; | ||
| const classNames = [ | ||
| "avatar", | ||
| "title", | ||
| "description", | ||
| "content", | ||
| "footer", | ||
| "body", | ||
| "detail", | ||
| ]; | ||
| for (const slot of slots) { | ||
| const slotElement = document.createElement("div"); | ||
| slotElement.slot = slot; | ||
| element.appendChild(slotElement); | ||
| } | ||
|
|
||
| act(() => { | ||
| document.body.appendChild(element); | ||
| }); | ||
|
|
||
| expect( | ||
| element.shadowRoot?.querySelector("eo-link")?.getAttribute("href") | ||
| ).toBe("/test"); | ||
| expect( | ||
| element.shadowRoot | ||
| ?.querySelector("eo-link") | ||
| ?.classList.contains("clickable") | ||
| ).toBe(true); | ||
|
|
||
| for (const className of classNames) { | ||
| expect( | ||
| element.shadowRoot | ||
| ?.querySelector(`.${className}`) | ||
| ?.classList.contains("hidden") | ||
| ).toBe(false); | ||
| } | ||
|
|
||
| // Remove all children | ||
| await act(async () => { | ||
| element.replaceChildren(); | ||
| }); | ||
|
|
||
| for (const className of classNames) { | ||
| expect( | ||
| element.shadowRoot | ||
| ?.querySelector(`.${className}`) | ||
| ?.classList.contains("hidden") | ||
| ).toBe(true); | ||
| } | ||
|
|
||
| // Unset href | ||
| element.href = undefined; | ||
| await act(async () => { | ||
| // wait | ||
| }); | ||
| expect( | ||
| element.shadowRoot?.querySelector("eo-link")?.getAttribute("href") | ||
| ).toBe(null); | ||
| expect( | ||
| element.shadowRoot | ||
| ?.querySelector("eo-link") | ||
| ?.classList.contains("clickable") | ||
| ).toBe(false); | ||
|
|
||
| act(() => { | ||
| document.body.removeChild(element); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import React, { useEffect, useRef, useState } from "react"; | ||
| import { createDecorators } from "@next-core/element"; | ||
| import { ReactNextElement, wrapBrick } from "@next-core/react-element"; | ||
| import classNames from "classnames"; | ||
| import "@next-core/theme"; | ||
| import type { | ||
| ExtendedLocationDescriptor, | ||
| Link, | ||
| LinkProps, | ||
| Target, | ||
| } from "../link"; | ||
| import styleText from "./styles.shadow.css"; | ||
|
|
||
| const WrappedLink = wrapBrick<Link, LinkProps>("eo-link"); | ||
|
|
||
| const { defineElement, property } = createDecorators(); | ||
|
|
||
| export interface CardBoxProps | ||
| extends Pick<LinkProps, "url" | "href" | "target"> { | ||
| // Define props here. | ||
| } | ||
|
|
||
| /** | ||
| * 卡片项容器 | ||
| * | ||
| * @slot avatar - 头像 | ||
| * @slot title - 标题 | ||
| * @slot description - 描述 | ||
| * @slot - 内容区 | ||
| * @slot footer - 底部 | ||
| */ | ||
| export | ||
| @defineElement("eo-card-box", { | ||
| styleTexts: [styleText], | ||
| }) | ||
| class CardBox extends ReactNextElement implements CardBoxProps { | ||
| /** | ||
| * 链接地址 | ||
| */ | ||
| @property({ | ||
| attribute: false, | ||
| }) | ||
| accessor url: ExtendedLocationDescriptor | undefined; | ||
|
|
||
| /** | ||
| * 设置 `href` 时将使用原生 `<a>` 标签,通常用于外链的跳转 | ||
| */ | ||
| @property() accessor href: string | undefined; | ||
|
|
||
| /** | ||
| * 链接跳转目标 | ||
| */ | ||
| @property() accessor target: Target | undefined; | ||
|
|
||
| /** 是否铺满容器 */ | ||
| @property({ type: Boolean, render: false }) | ||
| accessor fillContainer: boolean | undefined; | ||
|
|
||
| render() { | ||
| return ( | ||
| <CardBoxComponent url={this.url} href={this.href} target={this.target} /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export interface CardBoxComponentProps extends CardBoxProps { | ||
| // Define react event handlers here. | ||
| } | ||
|
|
||
| export function CardBoxComponent({ url, href, target }: CardBoxComponentProps) { | ||
| const slotsRef = useRef(new Map<string, HTMLSlotElement | null>()); | ||
| const [slotsMap, setSlotsMap] = useState(() => new Map<string, boolean>()); | ||
|
|
||
| useEffect(() => { | ||
| const slots = ["avatar", "title", "description", "", "footer"]; | ||
| const disposables: (() => void)[] = []; | ||
|
|
||
| for (const slot of slots) { | ||
| const slotElement = slotsRef.current.get(slot); | ||
| if (slotElement) { | ||
| const onSlotChange = () => { | ||
| setSlotsMap((prev) => { | ||
| const prevHas = prev.get(slot) ?? false; | ||
| const currentHas = slotElement.assignedNodes().length > 0; | ||
| return prevHas === currentHas | ||
| ? prev | ||
| : new Map(prev).set(slot, currentHas); | ||
| }); | ||
| }; | ||
| slotElement.addEventListener("slotchange", onSlotChange); | ||
| onSlotChange(); | ||
| disposables.push(() => { | ||
| slotElement.removeEventListener("slotchange", onSlotChange); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return () => { | ||
| disposables.forEach((dispose) => dispose()); | ||
| }; | ||
| }); | ||
|
|
||
| const refCallbackFactory = | ||
| (slot: string) => (element: HTMLSlotElement | null) => { | ||
| slotsRef.current.set(slot, element); | ||
| }; | ||
|
|
||
| const getSlotContainerClassName = (slot: string, className?: string) => { | ||
| return classNames(className ?? slot, { hidden: !slotsMap.get(slot) }); | ||
| }; | ||
|
|
||
| const getSlotsContainerClassName = (slots: string[], className: string) => { | ||
| return classNames(className, { | ||
| hidden: slots.every((slot) => !slotsMap.get(slot)), | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <WrappedLink | ||
| className={classNames("box", { clickable: !!(url || href) })} | ||
| type="plain" | ||
| url={url} | ||
| href={href} | ||
| target={target} | ||
| > | ||
| {/* <div className="header"> | ||
| <slot name="header" /> | ||
| </div> | ||
| <div className="cover"> | ||
| <slot name="cover" /> | ||
| </div> */} | ||
| <div | ||
| className={getSlotsContainerClassName( | ||
| ["avatar", "title", "description"], | ||
| "body" | ||
| )} | ||
| > | ||
| <div className={getSlotContainerClassName("avatar")}> | ||
| <slot name="avatar" ref={refCallbackFactory("avatar")} /> | ||
| </div> | ||
| <div | ||
| className={getSlotsContainerClassName( | ||
| ["title", "description"], | ||
| "detail" | ||
| )} | ||
| > | ||
| <div className={getSlotContainerClassName("title")}> | ||
| <slot name="title" ref={refCallbackFactory("title")} /> | ||
| </div> | ||
| <div className={getSlotContainerClassName("description")}> | ||
| <slot name="description" ref={refCallbackFactory("description")} /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <div className={getSlotContainerClassName("", "content")}> | ||
| <slot ref={refCallbackFactory("")} /> | ||
| </div> | ||
| <div className={getSlotContainerClassName("footer")}> | ||
| <slot name="footer" ref={refCallbackFactory("footer")} /> | ||
| </div> | ||
| </WrappedLink> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.