Skip to content

Commit

Permalink
Update headerCell model
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslan committed Feb 27, 2021
1 parent 6f4377b commit 707b3d0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ yarn-error.log*
.vercel

# IDLE
.idea
.idea

# DOC
Challenge.md
26 changes: 14 additions & 12 deletions components/Sheets/Content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@ import { EditableInput } from './EditableInput';
import styles from './Sheets.module.css';

export function SheetContent({ rows, cells, editableCell, setEditableCell, handleCellValue }) {
const nonEditableCells = ['id'];

/** Generate unique key. */
const generateUniqueKey = (main, child) => {
return `${main}.${child}`;
const generateUniqueKey = (key, index) => {
return `${key}.${index}`;
};

return (
<div className={styles.tableContent}>
{rows.map((row, rowIndex) => (
<div className={styles.tableContentRows} key={rowIndex}>
{cells.map((cell) => (
<div className={styles.tableContentCell} key={cell.name}>
{!nonEditableCells.includes(cell.name) &&
editableCell === generateUniqueKey(cell.name, rowIndex) ? (
<div className={styles.tableContentCell} key={cell.key}>
{cell.editable && editableCell === generateUniqueKey(cell.key, rowIndex) ? (
<EditableInput
value={row[cell.name]}
onChange={({target}) => handleCellValue(generateUniqueKey(cell.name, rowIndex), target.value)}
onFocus={(event => event.target.select())}
value={row[cell.key]}
onChange={({ target }) =>
handleCellValue(
generateUniqueKey(cell.key, rowIndex),
target.value
)
}
onFocus={(event) => event.target.select()}
/>
) : (
<div
className={styles.tableContentCellValue}
onDoubleClick={() =>
setEditableCell(generateUniqueKey(cell.name, rowIndex))
setEditableCell(generateUniqueKey(cell.key, rowIndex))
}>
{row[cell.name]}
{row[cell.key]}
</div>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion components/Sheets/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function SheetHeader({cells}) {
{
cells.map(cell => (
<div className={styles.tableHeaderCell} key={cell.name}>
{cell.name.replace(/([a-z0-9])([A-Z])/g, '$1 $2')}
{cell.name}
</div>
))
}
Expand Down
1 change: 0 additions & 1 deletion components/Sheets/Sheets.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
}

.tableHeaderCell {
text-transform: capitalize;
background: #f9f9f9;
padding: 8px;
font-weight: 700;
Expand Down
9 changes: 8 additions & 1 deletion pages/sheets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ const mockEmployees = [
employee(),
]

const headerCells = Object.keys(employee()).map((cell) => ({ name: cell }));
const headerCells = [
{key: 'id', name: 'ID', type: 'text', editable: false,filterable: true },
{key: 'name', name: 'Name', type: 'text', editable: true, filterable: true },
{key: 'surname', name: 'Surname', type: 'text', editable: true, filterable: true },
{key: 'dateOfBirth', name: 'Date of birth', type: 'text', editable: true, filterable: true },
{key: 'position', name: 'Position', type: 'text', editable: true, filterable: true },
{key: 'phone', name: 'Phone', type: 'text', editable: true, filterable: true },
];

export default function Sheets() {
const [editableCell, setEditableCell] = useState(null);
Expand Down

0 comments on commit 707b3d0

Please sign in to comment.