Skip to content

Commit

Permalink
Implement ItemSelection component
Browse files Browse the repository at this point in the history
This is useful for selecting multiple items from a list.
The component isn't in use right now, but it will be later
when we add support for selecting 'features' from the create
team page.
  • Loading branch information
skykanin committed Jun 14, 2024
1 parent 2de4868 commit 083da07
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/components/ItemSelection/ItemSelection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useMemo, useState } from 'react'
import { Tag } from '@statisticsnorway/ssb-component-library'
import styles from './itemSelection.module.scss'

export interface Item {
id: number

Check failure on line 6 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Delete `··`
text: string

Check failure on line 7 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Delete `··`
}

interface ItemSelectionProps {
header: string

Check failure on line 11 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Delete `··`
items: Item[]

Check failure on line 12 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Replace `····` with `··`
onSelectionChange: (selectedItems: Item[]) => void

Check failure on line 13 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Delete `··`
}
function ItemSelection({ header, items, onSelectionChange }: ItemSelectionProps) {
const [selectedItems, setSelectedItems] = useState<Item[]>([])

Check failure on line 16 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Delete `··`

const selectedItemIds = useMemo(() => selectedItems.map((item) => item.id), [selectedItems])

Check failure on line 18 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Replace `····` with `··`

const selectItem = (selectedItem: Item) => {

Check failure on line 20 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Delete `··`
const newSelectedItems = selectedItemIds.includes(selectedItem.id)

Check failure on line 21 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Delete `····`
? selectedItems.filter((item) => item.id !== selectedItem.id)

Check failure on line 22 in src/components/ItemSelection/ItemSelection.tsx

View workflow job for this annotation

GitHub Actions / build

Delete `······`
: [...selectedItems, selectedItem]

setSelectedItems(newSelectedItems)
onSelectionChange(newSelectedItems)
}

return (
<div className={styles.rootComponent}>
<div className={styles.header}>{header}</div>
<div className={styles.itemSelection}>
{items.map(({ id, text }) => (
<Tag
key={id}
onClick={(event: Event) => {
selectItem({ id, text })
event.preventDefault()
}}
className={selectedItemIds.includes(id) ? styles.selectedItem : ''}
>
{text}
</Tag>
))}
</div>
</div>
)
}

export default ItemSelection
24 changes: 24 additions & 0 deletions src/components/ItemSelection/itemSelection.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.rootComponent {
display: flex;
flex-direction: column;
gap: 2.5rem;
font-family: 'Roboto', sans-serif;
font-strect: normal;
}

.header {
font-size: 1.5rem;
font-weight: bold;
margin-bottom: 8px;
}

.itemSelection {
display: flex;
flex-flow: row wrap;
justify-content: flex-start;
gap: 1.3rem;
}
.selectedItem {
background-color: #00824d;
color: white;
}

0 comments on commit 083da07

Please sign in to comment.