Skip to content

Commit

Permalink
add expanded version of section list items
Browse files Browse the repository at this point in the history
  • Loading branch information
devgioele committed Sep 19, 2023
1 parent 4056253 commit 78e3239
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 27 deletions.
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 { SectionListItemVariant } from '../types'

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

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

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

return (
<button
onClick={onClick}
className={classNames(
section.listItemButton.base,
section.listItem.base,
section.listItem.variant[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?: SectionListItemVariant
badge?: ReactNode
}

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

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

return (
<LinkComponent
className={classNames(
section.listItemLink.base,
section.listItem.base,
section.listItem.variant[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 { SectionListItemVariant } 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',
variant: {
[SectionListItemVariant.Compact]: 'py-4 min-h-[1.5rem]',
[SectionListItemVariant.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 SectionListItemVariant {
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 { SectionListItemVariant } 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?: SectionListItemVariant
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={SectionListItemVariant.Expanded} />
)

export const ExpandedLinksList: Story = () => (
<List links variant={SectionListItemVariant.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

0 comments on commit 78e3239

Please sign in to comment.