| 1 | 'use client' |
| 2 | |
| 3 | import { Plus, Trash2 } from 'lucide-react' |
| 4 | import { Button } |
| 5 | import { Input } |
| 6 | |
| 7 | export interface LineItem |
| 8 | amount: string |
| 9 | description: string |
| 10 | quantity: string |
| 11 | title?: string |
| 12 | unitPrice: string |
| 13 | } |
| 14 | |
| 15 | interface LineItemsEditorProps { |
| 16 | currency: string |
| 17 | editable: boolean |
| 18 | items: LineItem |
| 19 | onChange: (items |
| 20 | title?: string |
| 21 | } |
| 22 | |
| 23 | export default function LineItemsEditor |
| 24 | items, |
| 25 | onChange, |
| 26 | currency, |
| 27 | editable, |
| 28 | title, |
| 29 | }: LineItemsEditorProps) |
| 30 | const updateItem = |
| 31 | const updated = |
| 32 | //@ts-expect-error - we know the index is valid |
| 33 | updated[index] = |
| 34 | |
| 35 | if (field === |
| 36 | const qty = |
| 37 | const price = |
| 38 | updated[index]!. |
| 39 | } |
| 40 | |
| 41 | onChange(updated) |
| 42 | } |
| 43 | |
| 44 | const addItem = |
| 45 | onChange([ |
| 46 | ...items, |
| 47 | { |
| 48 | title: '', |
| 49 | description: '', |
| 50 | quantity: '1', |
| 51 | unitPrice: '0', |
| 52 | amount: '0', |
| 53 | }, |
| 54 | ]) |
| 55 | } |
| 56 | |
| 57 | const removeItem = |
| 58 | onChange(items.filter |
| 59 | } |
| 60 | |
| 61 | const total = |
| 62 | |
| 63 | return ( |
| 64 | <div className='space-y-3 |
| 65 | <div className='flex items-center justify-between |
| 66 | <h3 className='font-medium text-sm |
| 67 | {editable && ( |
| 68 | <Button onClick={ |
| 69 | <Plus className=' |
| 70 | Add Item |
| 71 | </Button> |
| 72 | )} |
| 73 | </div> |
| 74 | |
| 75 | {items.length > |
| 76 | <div className='overflow-x-auto rounded-md border |
| 77 | <table className='w-full text-sm |
| 78 | <thead> |
| 79 | <tr className='border-b bg-muted/50 |
| 80 | <th className='px-3 py-2 text-left font-medium |
| 81 | <th className='w-24 px-3 py-2 text-right font-medium |
| 82 | <th className='w-32 px-3 py-2 text-right font-medium |
| 83 | Unit Price |
| 84 | </th> |
| 85 | <th className='w-32 px-3 py-2 text-right font-medium |
| 86 | Amount |
| 87 | </th> |
| 88 | {editable && <th |
| 89 | </tr> |
| 90 | </thead> |
| 91 | <tbody> |
| 92 | {items.map(( |
| 93 | <tr className='border-b last:border-b-0 |
| 94 | <td className='px-3 py-2 |
| 95 | {editable ? ( |
| 96 | <Input |
| 97 | className='h-8 border-none shadow-none focus-visible:ring-0' |
| 98 | onChange={(e) |
| 99 | updateItem(index, ' |
| 100 | updateItem(index, ' |
| 101 | }} |
| 102 | placeholder='Item description' |
| 103 | value={item.description |
| 104 | /> |
| 105 | ) : ( |
| 106 | <span>{item.description |
| 107 | )} |
| 108 | </td> |
| 109 | <td className='px-3 py-2 text-right |
| 110 | {editable ? ( |
| 111 | <Input |
| 112 | className='h-8 border-none text-right shadow-none focus-visible:ring-0' |
| 113 | onChange={(e) |
| 114 | updateItem(index, ' |
| 115 | } |
| 116 | value={item.quantity |
| 117 | /> |
| 118 | ) : ( |
| 119 | <span>{item.quantity |
| 120 | )} |
| 121 | </td> |
| 122 | <td className='px-3 py-2 text-right |
| 123 | {editable ? ( |
| 124 | <Input |
| 125 | className='h-8 border-none text-right shadow-none focus-visible:ring-0' |
| 126 | onChange={(e) |
| 127 | updateItem(index, ' |
| 128 | } |
| 129 | value={item.unitPrice |
| 130 | /> |
| 131 | ) : ( |
| 132 | <span>{item.unitPrice |
| 133 | )} |
| 134 | </td> |
| 135 | <td className='px-3 py-2 text-right font-medium |
| 136 | {Number(item.amount |
| 137 | minimumFractionDigits: 2, |
| 138 | maximumFractionDigits: 2, |
| 139 | })} |
| 140 | </td> |
| 141 | {editable && ( |
| 142 | <td className='px-2 py-2 |
| 143 | <Button |
| 144 | className='h-7 w-7 p-0 text-muted-foreground hover:text-destructive' |
| 145 | onClick={() => |
| 146 | size='sm' |
| 147 | variant='ghost' |
| 148 | > |
| 149 | <Trash2 className=' |
| 150 | </Button> |
| 151 | </td> |
| 152 | )} |
| 153 | </tr> |
| 154 | ))} |
| 155 | </tbody> |
| 156 | <tfoot> |
| 157 | <tr className='bg-muted/30 |
| 158 | <td |
| 159 | className='px-3 py-2 text-right font-semibold' |
| 160 | colSpan={editable ? |
| 161 | > |
| 162 | Total ({currency}) |
| 163 | </td> |
| 164 | <td className='px-3 py-2 text-right font-semibold |
| 165 | {total.toLocaleString(' |
| 166 | minimumFractionDigits: 2, |
| 167 | maximumFractionDigits: 2, |
| 168 | })} |
| 169 | </td> |
| 170 | {editable && <td |
| 171 | </tr> |
| 172 | </tfoot> |
| 173 | </table> |
| 174 | </div> |
| 175 | )} |
| 176 | </div> |
| 177 | ) |
| 178 | } |