Grids: copy icon beside every cell value (no toast), also on the refinery claim trip tables
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
84facf2ddf
commit
03d16e742a
|
|
@ -1,10 +1,12 @@
|
||||||
import { useEffect, useMemo, useState, type ReactNode } from 'react';
|
import { useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||||
import { keepPreviousData, useQuery } from '@tanstack/react-query';
|
import { keepPreviousData, useQuery } from '@tanstack/react-query';
|
||||||
import { useLocation, useSearchParams } from 'react-router-dom';
|
import { useLocation, useSearchParams } from 'react-router-dom';
|
||||||
import {
|
import {
|
||||||
alpha, Alert, Badge, Box, Button, Chip, IconButton, ListItemIcon, ListItemText, Menu, MenuItem, Paper, Stack,
|
alpha, Alert, Badge, Box, Button, Chip, IconButton, ListItemIcon, ListItemText, Menu, MenuItem, Paper, Stack,
|
||||||
Table, TableBody, TableCell, TableContainer, TableHead, TablePagination, TableRow, Tooltip, Typography, useTheme,
|
Table, TableBody, TableCell, TableContainer, TableHead, TablePagination, TableRow, Tooltip, Typography, useTheme,
|
||||||
} from '@mui/material';
|
} from '@mui/material';
|
||||||
|
import CheckIcon from '@mui/icons-material/Check';
|
||||||
|
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
|
||||||
import FilterListIcon from '@mui/icons-material/FilterList';
|
import FilterListIcon from '@mui/icons-material/FilterList';
|
||||||
import MoreVertIcon from '@mui/icons-material/MoreVert';
|
import MoreVertIcon from '@mui/icons-material/MoreVert';
|
||||||
import EditOutlined from '@mui/icons-material/EditOutlined';
|
import EditOutlined from '@mui/icons-material/EditOutlined';
|
||||||
|
|
@ -255,7 +257,7 @@ export function DataGrid<T>({ meta }: DataGridProps<T>) {
|
||||||
{hasActions && actionsBefore && actionsCell(row)}
|
{hasActions && actionsBefore && actionsCell(row)}
|
||||||
{columns.map((c) => (
|
{columns.map((c) => (
|
||||||
<TableCell key={c.key} sx={cellSx(c)}>
|
<TableCell key={c.key} sx={cellSx(c)}>
|
||||||
<Cell col={c} row={row} />
|
{c.format === 'status' ? <Cell col={c} row={row} /> : <CopyCell><Cell col={c} row={row} /></CopyCell>}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
))}
|
))}
|
||||||
{hasActions && !actionsBefore && actionsCell(row)}
|
{hasActions && !actionsBefore && actionsCell(row)}
|
||||||
|
|
@ -326,6 +328,70 @@ function cellSx(c: ColumnMeta<never>) {
|
||||||
...(c.hideBelow ? { display: { xs: 'none', [BP[c.hideBelow]]: 'table-cell' } } : {}),
|
...(c.hideBelow ? { display: { xs: 'none', [BP[c.hideBelow]]: 'table-cell' } } : {}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
/** Writes text to the clipboard (async API, with the old execCommand path for contexts where it is unavailable). */
|
||||||
|
async function copyText(text: string) {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(text);
|
||||||
|
} catch {
|
||||||
|
const ta = document.createElement('textarea');
|
||||||
|
ta.value = text;
|
||||||
|
ta.style.position = 'fixed';
|
||||||
|
ta.style.opacity = '0';
|
||||||
|
document.body.appendChild(ta);
|
||||||
|
ta.select();
|
||||||
|
try { document.execCommand('copy'); } finally { document.body.removeChild(ta); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A grid cell value with a small copy icon (shown on hover / keyboard focus, always on touch screens). It copies the text the cell
|
||||||
|
* shows; cells without text (icon-only columns) get no icon. No toast: the icon turns into a check mark for a moment.
|
||||||
|
*/
|
||||||
|
export function CopyCell({ children }: { children: ReactNode }) {
|
||||||
|
const { t } = useUi();
|
||||||
|
const ref = useRef<HTMLSpanElement>(null);
|
||||||
|
const [hasText, setHasText] = useState(false);
|
||||||
|
const [done, setDone] = useState(false);
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
const has = (ref.current?.textContent ?? '').trim().length > 0;
|
||||||
|
setHasText((cur) => (cur === has ? cur : has));
|
||||||
|
});
|
||||||
|
useEffect(() => {
|
||||||
|
if (!done) return undefined;
|
||||||
|
const id = window.setTimeout(() => setDone(false), 1200);
|
||||||
|
return () => window.clearTimeout(id);
|
||||||
|
}, [done]);
|
||||||
|
return (
|
||||||
|
<Box
|
||||||
|
component="span"
|
||||||
|
sx={{
|
||||||
|
display: 'inline-flex', alignItems: 'center', gap: 0.5, maxWidth: '100%',
|
||||||
|
'& .copy-btn': { opacity: 0, transition: 'opacity .12s' },
|
||||||
|
'&:hover .copy-btn, & .copy-btn:focus-visible': { opacity: 1 },
|
||||||
|
'@media (hover: none)': { '& .copy-btn': { opacity: 0.6 } },
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span ref={ref}>{children}</span>
|
||||||
|
{hasText && (
|
||||||
|
<IconButton
|
||||||
|
className="copy-btn"
|
||||||
|
size="small"
|
||||||
|
title={t('common.copy')}
|
||||||
|
aria-label={t('common.copy')}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation(); // the row click (open) must not fire
|
||||||
|
const text = (ref.current?.textContent ?? '').trim();
|
||||||
|
void copyText(text).then(() => setDone(true));
|
||||||
|
}}
|
||||||
|
sx={{ p: 0.25 }}
|
||||||
|
>
|
||||||
|
{done ? <CheckIcon sx={{ fontSize: 14 }} color="success" /> : <ContentCopyIcon sx={{ fontSize: 14 }} />}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function Cell<T>({ col, row }: { col: ColumnMeta<T>; row: T }): ReactNode {
|
function Cell<T>({ col, row }: { col: ColumnMeta<T>; row: T }): ReactNode {
|
||||||
const { lang } = useUi();
|
const { lang } = useUi();
|
||||||
if (col.render) return <>{col.render(row, { lang })}</>;
|
if (col.render) return <>{col.render(row, { lang })}</>;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
export { DataGrid, gridKey } from './DataGrid';
|
export { CopyCell, DataGrid, gridKey } from './DataGrid';
|
||||||
export type { DataGridProps } from './DataGrid';
|
export type { DataGridProps } from './DataGrid';
|
||||||
export { gridReturnUrl, rememberGridUrl } from './gridReturn';
|
export { gridReturnUrl, rememberGridUrl } from './gridReturn';
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import PaymentsIcon from '@mui/icons-material/Payments';
|
||||||
import SearchIcon from '@mui/icons-material/Search';
|
import SearchIcon from '@mui/icons-material/Search';
|
||||||
import AssignmentIcon from '@mui/icons-material/Assignment';
|
import AssignmentIcon from '@mui/icons-material/Assignment';
|
||||||
import ReceiptLongIcon from '@mui/icons-material/ReceiptLong';
|
import ReceiptLongIcon from '@mui/icons-material/ReceiptLong';
|
||||||
import { DataGrid } from '@/components/DataGrid';
|
import { CopyCell, DataGrid } from '@/components/DataGrid';
|
||||||
import { LoadingBanner } from '@/components/LoadingBanner';
|
import { LoadingBanner } from '@/components/LoadingBanner';
|
||||||
import { PageHeader } from '@/components/PageHeader';
|
import { PageHeader } from '@/components/PageHeader';
|
||||||
import { StatusChip } from '@/components/StatusChip';
|
import { StatusChip } from '@/components/StatusChip';
|
||||||
|
|
@ -63,9 +63,9 @@ function WageClaimsTable(p: {
|
||||||
{selectable && (
|
{selectable && (
|
||||||
<TableCell padding="checkbox"><Checkbox checked={p.selected!.has(r.id)} onChange={() => p.onToggle?.(r.id)} /></TableCell>
|
<TableCell padding="checkbox"><Checkbox checked={p.selected!.has(r.id)} onChange={() => p.onToggle?.(r.id)} /></TableCell>
|
||||||
)}
|
)}
|
||||||
<TableCell sx={ltr}>{r.wageClaimNo}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{r.wageClaimNo}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{formatDate(r.wageClaimDate)}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{formatDate(r.wageClaimDate)}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{r.tripCnt}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{r.tripCnt}</CopyCell></TableCell>
|
||||||
<TableCell><StatusChip value={r.statusCode} statuses={claimStatuses} /></TableCell>
|
<TableCell><StatusChip value={r.statusCode} statuses={claimStatuses} /></TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
))}
|
))}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import {
|
||||||
import HistoryIcon from '@mui/icons-material/History';
|
import HistoryIcon from '@mui/icons-material/History';
|
||||||
import VisibilityOutlined from '@mui/icons-material/VisibilityOutlined';
|
import VisibilityOutlined from '@mui/icons-material/VisibilityOutlined';
|
||||||
import type { PagedResult } from '@/api/client';
|
import type { PagedResult } from '@/api/client';
|
||||||
|
import { CopyCell } from '@/components/DataGrid';
|
||||||
import { LoadingBanner } from '@/components/LoadingBanner';
|
import { LoadingBanner } from '@/components/LoadingBanner';
|
||||||
import { SectionPaper } from '@/components/SectionPaper';
|
import { SectionPaper } from '@/components/SectionPaper';
|
||||||
import { SectionTitle } from '@/components/SectionTitle';
|
import { SectionTitle } from '@/components/SectionTitle';
|
||||||
|
|
@ -60,14 +61,14 @@ export function TripsTable(p: {
|
||||||
)}
|
)}
|
||||||
{rows.map((r) => (
|
{rows.map((r) => (
|
||||||
<TableRow key={r.id} hover={!!p.onOpen} onClick={p.onOpen ? () => p.onOpen!(r) : undefined} sx={{ cursor: p.onOpen ? 'pointer' : undefined }}>
|
<TableRow key={r.id} hover={!!p.onOpen} onClick={p.onOpen ? () => p.onOpen!(r) : undefined} sx={{ cursor: p.onOpen ? 'pointer' : undefined }}>
|
||||||
<TableCell sx={ltr}>{r.pn}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{r.pn}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{formatDate(r.paymentedDate)}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{formatDate(r.paymentedDate)}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{r.loadingNo}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{r.loadingNo}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{r.dischargeAuthoriztionNo}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{r.dischargeAuthoriztionNo}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{formatDate(r.dischargeEntryDate)}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{formatDate(r.dischargeEntryDate)}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{r.tn}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{r.tn}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{r.trn}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{r.trn}</CopyCell></TableCell>
|
||||||
<TableCell sx={ltr}>{r.nnpn}</TableCell>
|
<TableCell sx={ltr}><CopyCell>{r.nnpn}</CopyCell></TableCell>
|
||||||
<TableCell><StatusChip value={r.statusCode} statuses={tripStatuses} /></TableCell>
|
<TableCell><StatusChip value={r.statusCode} statuses={tripStatuses} /></TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
))}
|
))}
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,7 @@ export const ui = {
|
||||||
'common.save': { en: 'Save', ar: 'حفظ' },
|
'common.save': { en: 'Save', ar: 'حفظ' },
|
||||||
'common.cancel': { en: 'Cancel', ar: 'إلغاء' },
|
'common.cancel': { en: 'Cancel', ar: 'إلغاء' },
|
||||||
'common.close': { en: 'Close', ar: 'إغلاق' },
|
'common.close': { en: 'Close', ar: 'إغلاق' },
|
||||||
|
'common.copy': { en: 'Copy', ar: 'نسخ' },
|
||||||
'common.back': { en: 'Back', ar: 'رجوع' },
|
'common.back': { en: 'Back', ar: 'رجوع' },
|
||||||
'common.next': { en: 'Next', ar: 'التالي' },
|
'common.next': { en: 'Next', ar: 'التالي' },
|
||||||
'common.edit': { en: 'Edit', ar: 'تعديل' },
|
'common.edit': { en: 'Edit', ar: 'تعديل' },
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue