Skip to content
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

Expanded section list items #287

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions src/components/section/SectionItem/SectionListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import classNames from 'classnames'
import IconKeyboardArrowRight from '@aboutbits/react-material-icons/dist/IconKeyboardArrowRight'
import IconArrowForwardIos from '@aboutbits/react-material-icons/dist/IconArrowForwardIos'
import { ReactNode, forwardRef } from 'react'
import {
LinkComponentProps,
useLinkComponent,
useTheme,
} from '../../../framework'
import { ClassNameProps } from '../../types'
import { SectionListItemLayout } from '../types'

export type SectionListItemProps = ClassNameProps & {
children?: ReactNode
Expand All @@ -28,67 +30,96 @@ export type SectionListItemButtonProps = ClassNameProps & {
*/
onClick: () => void
children?: ReactNode
variant?: SectionListItemLayout
badge?: ReactNode
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a better name than badge? We commonly place a badge here, but any ReactNode goes

}

export const SectionListItemButton = forwardRef<
HTMLButtonElement,
SectionListItemButtonProps
>(function SectionListItemButton(
{ children, onClick, className, ...props },
{
children,
onClick,
className,
variant = SectionListItemLayout.Compact,
badge,
...props
},
ref,
) {
const { section } = useTheme()

const Icon =
variant === SectionListItemLayout.Compact
? IconKeyboardArrowRight
: IconArrowForwardIos

return (
<button
onClick={onClick}
className={classNames(
section.listItemButton.base,
section.listItem.base,
section.listItem.layout[variant],
section.listItemButton.base,
className,
)}
ref={ref}
{...props}
>
{children}
<IconKeyboardArrowRight
width="24"
height="24"
className={section.listItemButton.icon}
/>
<div className={section.listItem.rightAlignedContainer.base}>
{badge}
<Icon width="24" height="24" className={section.listItem.icon} />
</div>
</button>
)
})

export type SectionListItemLinkProps = LinkComponentProps
export type SectionListItemLinkProps = LinkComponentProps & {
variant?: SectionListItemLayout
badge?: ReactNode
}

export const SectionListItemLink = forwardRef<
HTMLAnchorElement,
SectionListItemLinkProps
>(function SectionListItemLink(
{ children, className, internal = true, ...props },
{
variant = SectionListItemLayout.Compact,
badge,
children,
className,
internal = true,
...props
},
ref,
) {
const LinkComponent = useLinkComponent()
const { section } = useTheme()

const Icon =
variant === SectionListItemLayout.Compact
? IconKeyboardArrowRight
: IconArrowForwardIos

return (
<LinkComponent
className={classNames(
section.listItemLink.base,
section.listItem.base,
section.listItem.layout[variant],
section.listItemLink.base,
className,
)}
internal={internal}
ref={ref}
{...props}
>
{children}
<IconKeyboardArrowRight
width="24"
height="24"
className={section.listItemButton.icon}
/>
<div className={section.listItem.rightAlignedContainer.base}>
{badge}
<Icon width="24" height="24" className={section.listItem.icon} />
</div>
</LinkComponent>
)
})
Expand Down
17 changes: 14 additions & 3 deletions src/components/section/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Size } from '../types'
import { SectionContentLayout } from './Section/SectionContent/types'
import { SectionHeaderRowLayout } from './SectionHeader/SectionHeaderRow/types'
import { SectionHeaderSpacerSize } from './SectionHeader/SectionHeaderSpacer/types'
import { SectionListItemLayout } from './types'

export default {
const className = {
section: {
base: '',
},
Expand All @@ -30,7 +31,16 @@ export default {
base: 'grid xl:grid-cols-2 xl:gap-x-11 gap-y-6 px-4 md:px-6 pt-4 md:pt-6 pb-8 md:pb-9',
},
listItem: {
base: 'flex border-b border-neutral-200 last:border-0 items-center min-h-[3.5rem] bg-white px-4 md:px-6',
base: 'flex border-b border-neutral-200 last:border-0 items-center bg-white px-4 md:px-6',
layout: {
[SectionListItemLayout.Compact]: 'py-4 min-h-[1.5rem]',
[SectionListItemLayout.Expanded]:
'py-4 min-h-[6.5rem] first:pt-6 first:min-h-[7rem] last:pb-6 last:min-h-[7rem]',
},
icon: 'fill-current mt-auto',
rightAlignedContainer: {
base: 'flex flex-col items-end self-stretch justify-between',
},
},
listItemWithAction: {
base: 'justify-between space-x-4',
Expand All @@ -40,7 +50,6 @@ export default {
},
listItemButton: {
base: 'block w-full focus:outline-neutral-800 justify-between space-x-4 hover:bg-neutral-100 active:bg-neutral-100',
icon: 'fill-current',
},
listItemLink: {
base: 'block focus:outline-neutral-800 justify-between space-x-4 hover:bg-neutral-100 active:bg-neutral-100',
Expand Down Expand Up @@ -103,3 +112,5 @@ export default {
base: 'mt-4',
},
}

export default className
4 changes: 4 additions & 0 deletions src/components/section/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum SectionListItemLayout {
Compact = 'COMPACT',
Expanded = 'EXPANDED',
}
43 changes: 34 additions & 9 deletions src/examples/List.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import {
SelectField,
Tone,
Option,
SectionListItemLink,
} from '../components'
import { SearchField } from '../components/form/SearchField'
import { useFilter } from '../components/util/useFilter'
import { SectionListItemLayout } from '../components/section/types'

const meta = {
component: SectionContentList,
Expand Down Expand Up @@ -66,7 +68,15 @@ function useMockedList(numberOfTotalItems: number) {
)
}

const List = ({ numberOfTotalItems = 5 }: { numberOfTotalItems?: number }) => {
const List = ({
numberOfTotalItems = 5,
variant,
links = false,
}: {
numberOfTotalItems?: number
variant?: SectionListItemLayout
links?: boolean
}) => {
const content = useMockedList(numberOfTotalItems)
return (
<Section>
Expand All @@ -79,14 +89,21 @@ const List = ({ numberOfTotalItems = 5 }: { numberOfTotalItems?: number }) => {
/>
) : (
<SectionContentList>
{content.map((item) => (
<SectionListItemButton
key={item.name}
onClick={action('onItemClick')}
>
{`${item.name} (${item.role} - ${item.department})`}
</SectionListItemButton>
))}
{content.map((item) =>
links ? (
<SectionListItemLink key={item.name} href="#" variant={variant}>
{`${item.name} (${item.role} - ${item.department})`}
</SectionListItemLink>
) : (
<SectionListItemButton
key={item.name}
onClick={action('onItemClick')}
variant={variant}
>
{`${item.name} (${item.role} - ${item.department})`}
</SectionListItemButton>
),
)}
</SectionContentList>
)}
</SectionContainer>
Expand All @@ -98,6 +115,14 @@ export const SimpleList: Story = () => <List />

export const EmptySimpleList: Story = () => <List numberOfTotalItems={0} />

export const ExpandedButtonsList: Story = () => (
<List variant={SectionListItemLayout.Expanded} />
)

export const ExpandedLinksList: Story = () => (
<List links variant={SectionListItemLayout.Expanded} />
)

/**
* The following example shows how multiple section components and the in memory pagination are used to create an overview list with filters.
*/
Expand Down