-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(List): add option to toggle list details
- Loading branch information
Showing
12 changed files
with
251 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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
117 changes: 117 additions & 0 deletions
117
packages/components/src/components/List/hooks/useGridItemProps.tsx
This file contains 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,117 @@ | ||
import type { PropsWithChildren } from "react"; | ||
import React, { useEffect, useId, useRef, useState } from "react"; | ||
import { useLocalizedStringFormatter } from "react-aria"; | ||
import locales from "../locales/*.locale.json"; | ||
import { useList } from "@/components/List"; | ||
import type { PropsContext } from "@/lib/propsContext"; | ||
import { dynamic, PropsContextProvider } from "@/lib/propsContext"; | ||
import { | ||
IconChevronDown, | ||
IconChevronUp, | ||
} from "@/components/Icon/components/icons"; | ||
|
||
const NoOpRender = () => null; | ||
|
||
interface P extends PropsWithChildren { | ||
data: never; | ||
} | ||
|
||
export const useGridItemProps = (props: P) => { | ||
const { data, children: childrenFromProps } = props; | ||
const stringFormatter = useLocalizedStringFormatter(locales); | ||
const list = useList(); | ||
const itemView = list.itemView; | ||
const onAction = list.onAction; | ||
|
||
const [isExpanded, setIsExpanded] = useState( | ||
itemView?.defaultExpanded?.(data) ?? false, | ||
); | ||
const contentElementId = useId(); | ||
const itemRef = useRef<HTMLDivElement>(null); | ||
|
||
const accordion = list.accordion; | ||
const children = childrenFromProps ?? itemView?.render(data); | ||
|
||
useEffect(() => { | ||
if (accordion) { | ||
itemRef.current?.setAttribute("aria-expanded", String(isExpanded)); | ||
itemRef.current?.setAttribute("aria-controls", contentElementId); | ||
} | ||
}, [isExpanded, contentElementId, itemRef.current, accordion]); | ||
|
||
if (!accordion) { | ||
return { | ||
gridItemProps: { | ||
onAction: () => { | ||
onAction?.(data); | ||
}, | ||
}, | ||
children, | ||
}; | ||
} | ||
|
||
const toggleAccordion = () => { | ||
setIsExpanded((current) => !current); | ||
onAction?.(data); | ||
}; | ||
|
||
const propsContext: PropsContext = { | ||
Content: { | ||
id: dynamic((p) => (p.slot === "bottom" ? contentElementId : undefined)), | ||
wrapWith: dynamic((p) => | ||
p.slot === "bottom" ? ( | ||
isExpanded ? undefined : ( | ||
<NoOpRender /> | ||
) | ||
) : undefined, | ||
), | ||
}, | ||
|
||
Button: { | ||
children: dynamic((p) => | ||
p.slot === "toggle" ? ( | ||
isExpanded ? ( | ||
<IconChevronUp /> | ||
) : ( | ||
<IconChevronDown /> | ||
) | ||
) : undefined, | ||
), | ||
variant: dynamic((p) => (p.slot === "toggle" ? "plain" : undefined)), | ||
color: dynamic((p) => (p.slot === "toggle" ? "secondary" : undefined)), | ||
onPress: dynamic((p) => | ||
p.slot === "toggle" ? toggleAccordion : undefined, | ||
), | ||
"aria-label": dynamic((p) => | ||
p.slot === "toggle" | ||
? stringFormatter.format( | ||
isExpanded | ||
? "list.toggleExpandButton.collapse" | ||
: "list.toggleExpandButton.expand", | ||
) | ||
: undefined, | ||
), | ||
"aria-controls": dynamic((p) => | ||
p.slot === "toggle" ? contentElementId : undefined, | ||
), | ||
"aria-expanded": dynamic((p) => | ||
p.slot === "toggle" ? isExpanded : undefined, | ||
), | ||
}, | ||
}; | ||
|
||
return { | ||
gridItemProps: { | ||
ref: itemRef, | ||
onAction: toggleAccordion, | ||
}, | ||
children: ( | ||
<PropsContextProvider | ||
props={propsContext} | ||
dependencies={[contentElementId, isExpanded]} | ||
> | ||
{children} | ||
</PropsContextProvider> | ||
), | ||
}; | ||
}; |
This file contains 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 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 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 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 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 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
50 changes: 50 additions & 0 deletions
50
packages/docs/src/content/03-components/structure/list/examples/accordion.tsx
This file contains 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,50 @@ | ||
import { | ||
List, | ||
ListItem, | ||
ListItemView, | ||
ListStaticData, | ||
} from "@mittwald/flow-react-components/List"; | ||
import { | ||
type Domain, | ||
domains, | ||
} from "@/content/03-components/structure/list/examples/domainApi"; | ||
import Avatar from "@mittwald/flow-react-components/Avatar"; | ||
import Heading from "@mittwald/flow-react-components/Heading"; | ||
import Text from "@mittwald/flow-react-components/Text"; | ||
import { | ||
IconDomain, | ||
IconSubdomain, | ||
} from "@mittwald/flow-react-components/Icons"; | ||
import AlertBadge from "@mittwald/flow-react-components/AlertBadge"; | ||
import Content from "@mittwald/flow-react-components/Content"; | ||
import Button from "@mittwald/flow-react-components/Button"; | ||
|
||
<List batchSize={2} accordion> | ||
<ListStaticData data={domains} /> | ||
<ListItem<Domain>> | ||
{(domain) => ( | ||
<ListItemView> | ||
<Avatar | ||
color={domain.type === "Domain" ? "blue" : "teal"} | ||
> | ||
{domain.type === "Domain" ? ( | ||
<IconDomain /> | ||
) : ( | ||
<IconSubdomain /> | ||
)} | ||
</Avatar> | ||
<Heading> | ||
{domain.hostname} | ||
{!domain.verified && ( | ||
<AlertBadge status="warning"> | ||
Unverifiziert | ||
</AlertBadge> | ||
)} | ||
</Heading> | ||
<Text>{domain.type}</Text> | ||
<Button slot="toggle" /> | ||
<Content slot="bottom">Mehr Inhalt</Content> | ||
</ListItemView> | ||
)} | ||
</ListItem> | ||
</List>; |
Oops, something went wrong.