Add CRUDList.tsx
This commit is contained in:
parent
264f5aba60
commit
0f0ba81795
90
packages/client/src/crud/CRUDList.tsx
Normal file
90
packages/client/src/crud/CRUDList.tsx
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import {Button, Panel, PanelHeading, PanelBlock} from 'bloomer'
|
||||||
|
import {FaPlus, FaEdit, FaTimes} from 'react-icons/fa'
|
||||||
|
import {Link} from 'react-router-dom'
|
||||||
|
|
||||||
|
export interface ICRUDListProps<T> {
|
||||||
|
nameKey: keyof T
|
||||||
|
editLink: (item: T) => string
|
||||||
|
itemIds: number[]
|
||||||
|
itemsById: Record<number, T>
|
||||||
|
newLink: string
|
||||||
|
onRemove: (t: T) => void
|
||||||
|
title: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICRUDItemRowProps<T> {
|
||||||
|
nameKey: keyof T
|
||||||
|
editLink: string
|
||||||
|
item: T
|
||||||
|
onRemove: (t: T) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CRUDItemRow<T> extends React.PureComponent<ICRUDItemRowProps<T>> {
|
||||||
|
handleRemove = () => {
|
||||||
|
const {onRemove, item} = this.props
|
||||||
|
onRemove(item)
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
const {nameKey, editLink, item} = this.props
|
||||||
|
|
||||||
|
return (
|
||||||
|
<React.Fragment>
|
||||||
|
<div className='item-info'>
|
||||||
|
{item[nameKey]}
|
||||||
|
</div>
|
||||||
|
<div className='ml-auto'>
|
||||||
|
<Link to={editLink}>
|
||||||
|
<Button isInverted isColor='link' aria-label='Edit'>
|
||||||
|
<FaEdit />
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
aria-label='Remove'
|
||||||
|
onClick={this.handleRemove}
|
||||||
|
isColor='danger'
|
||||||
|
isInverted
|
||||||
|
>
|
||||||
|
<FaTimes />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</React.Fragment>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CRUDList<T> extends React.PureComponent<ICRUDListProps<T>> {
|
||||||
|
render() {
|
||||||
|
const {nameKey, editLink, itemIds, itemsById, newLink, title} = this.props
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Panel>
|
||||||
|
<PanelHeading>
|
||||||
|
<span className='is-flex v-centered'>
|
||||||
|
<span>{title}</span>
|
||||||
|
<Link
|
||||||
|
className='ml-auto button is-link is-small'
|
||||||
|
to={newLink}
|
||||||
|
>
|
||||||
|
<FaPlus /> New
|
||||||
|
</Link>
|
||||||
|
</span>
|
||||||
|
</PanelHeading>
|
||||||
|
{itemIds.map(itemId => {
|
||||||
|
const item = itemsById[itemId]
|
||||||
|
return (
|
||||||
|
<PanelBlock key={itemId}>
|
||||||
|
<CRUDItemRow<T>
|
||||||
|
nameKey={nameKey}
|
||||||
|
editLink={editLink(item)}
|
||||||
|
item={item}
|
||||||
|
onRemove={this.props.onRemove}
|
||||||
|
/>
|
||||||
|
</PanelBlock>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</Panel>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
export * from './CRUDActions'
|
export * from './CRUDActions'
|
||||||
|
export * from './CRUDList'
|
||||||
export * from './CRUDReducer'
|
export * from './CRUDReducer'
|
||||||
export * from './TCRUDAction'
|
export * from './TCRUDAction'
|
||||||
export * from './TCRUDMethod'
|
export * from './TCRUDMethod'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user