Skip to content

Commit abd9ff1

Browse files
author
Tomasz John
committed
- fix: CDataTable component - now with Vue Scoped Slots prop equivalent
1 parent 2132022 commit abd9ff1

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

src/CDataTable.js

+37-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { useState, useRef, useMemo } from 'react';
1+
import React, { useState, useEffect, useRef, useMemo, } from 'react';
22
import PropTypes from 'prop-types';
33
import classNames from 'classnames';
4-
import {tagPropType} from './Shared/helper.js';
4+
import {tagPropType, mapToCssModules} from './Shared/helper.js';
55
import Slot from './Shared/Slot';
6-
import { CIcon } from '@coreui/icons-react';
6+
import CIcon from '../coreui-icons/CIcon';
77
import CSpinner from './CSpinner';
88
import CPagination from './CPagination';
99
import style from './CDataTable.module.css';
@@ -26,6 +26,7 @@ const CDataTable = props=>{
2626
captionSlot,
2727
underTableSlot,
2828
colNameSlot,
29+
scopedSlots,
2930
fields,
3031
pagination,
3132
activePage,
@@ -50,8 +51,9 @@ const CDataTable = props=>{
5051
loading,
5152
change,
5253
onChange,
53-
//customContent,
54-
//...attributes
54+
customContent,
55+
sorterValue,
56+
columnFilterValue
5557
} = props;
5658

5759
//Object.assign(style, cssModule)
@@ -101,7 +103,7 @@ const CDataTable = props=>{
101103
const headerStyles = (index)=>{
102104
let style = {}
103105
if (isSortable(index)) {
104-
style['cursor']='pointer;';
106+
style['cursor']='pointer';
105107
}
106108
if (fields && fields[index] && fields[index]._style) {
107109
let r = fields[index]._style.split(':');
@@ -271,12 +273,10 @@ const CDataTable = props=>{
271273
}, [itemsPerPage]);
272274

273275
//sorterValue
274-
/*
275276
useMemo(()=>{
276277
const asc = sorterValue.asc === false ? false : true;
277278
setSorterState(Object.assign({}, { asc, column: sorterValue.column }));
278279
}, [sorterValue]);
279-
*/
280280

281281
//tableFilterValue
282282
useMemo(()=>{
@@ -285,11 +285,9 @@ const CDataTable = props=>{
285285
}, [tableFilterValue]);
286286

287287
//columnFilterValue
288-
/*
289288
useMemo(()=>{
290289
setColumnFilterState(Object.assign({}, columnFilterValue));
291290
}, [columnFilterValue]);
292-
*/
293291

294292
//items
295293
useMemo(()=>{
@@ -318,10 +316,17 @@ const CDataTable = props=>{
318316

319317
compData.firstRun = false;
320318

321-
let scopedSlots = {}; //+
319+
let paginationProps;
320+
if (typeof pagination === 'object')
321+
paginationProps = {...pagination}
322+
else {
323+
paginationProps = {}
324+
}
325+
326+
console.log(scopedSlots);
322327

323328
return (
324-
<React.Fragment>
329+
<>
325330
<div ref={innerRef}>
326331
{
327332
itemsPerPageSelect || tableFilter ?
@@ -343,7 +348,7 @@ const CDataTable = props=>{
343348
{
344349
itemsPerPageSelect ?
345350
<div
346-
className={'col-sm-6 p-0' + !tableFilter ? 'offset-sm-6' : ''}
351+
className={'col-sm-6 p-0' + (!tableFilter ? 'offset-sm-6' : '')}
347352
>
348353
<div className="form-inline justify-content-sm-end">
349354
<label className="mr-2">Items per page:</label>
@@ -437,12 +442,12 @@ const CDataTable = props=>{
437442
}
438443
</thead>
439444
<tbody
440-
style={clickableRows ? 'cursor:pointer;': null}
445+
style={clickableRows ? {cursor: 'pointer'} : null}
441446
className="position-relative"
442447
>
443448
{currentItems.map((item, itemIndex)=>{
444449
return (
445-
<React.Fragment>
450+
<>
446451
<tr
447452
className={classNames(item._classes)}
448453
tabIndex={clickableRows ? 0 : null}
@@ -454,9 +459,7 @@ const CDataTable = props=>{
454459
if (scopedSlots[colName])
455460
return(
456461
<Slot
457-
content={colNameSlot}
458-
item={item}
459-
index={itemIndex + firstItemIndex}
462+
content={scopedSlots[colName](item, itemIndex + firstItemIndex)}
460463
/>
461464
)
462465
else
@@ -475,23 +478,21 @@ const CDataTable = props=>{
475478
scopedSlots.details?
476479
<tr
477480
className="p-0"
478-
style={{border:'none !important'}}
481+
style={{border: 'none !important'}}
479482
key={'details' + itemIndex}
480483
>
481484
<td
482485
colSpan={colspan}
483486
className="p-0"
484-
style={{border:'none !important'}}
487+
style={{border: 'none !important'}}
485488
>
486489
<Slot
487-
content={detailsSlot}
488-
item={item}
489-
index={itemIndex + firstItemIndex}
490+
content={scopedSlots.details(item, itemIndex + firstItemIndex)}
490491
/>
491492
</td>
492493
</tr>:''
493494
}
494-
</React.Fragment>
495+
</>
495496
)
496497
})}
497498
{
@@ -584,15 +585,16 @@ const CDataTable = props=>{
584585

585586
{
586587
//:activePage.sync="page"
588+
//v-bind={typeof pagination === 'object' ? {...pagination} : null}
587589
pagination ?
588590
<CPagination
589591
style={{display: totalPages > 0 ? 'inline' : 'none'}}
590592
pages={totalPages}
591-
v-bind={typeof pagination === 'object' ? {...pagination} : null}
593+
{...paginationProps}
592594
/>:''
593595
}
594596

595-
</React.Fragment>
597+
</>
596598
)
597599

598600
}
@@ -606,14 +608,15 @@ CDataTable.propTypes = {
606608
//
607609
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
608610
overTableSlot: PropTypes.node,
609-
colNameSlot: PropTypes.node,
611+
colNameSlot: PropTypes.node,//?
610612
columnHeaderSlot: PropTypes.array,
611613
sortingIconSlot: PropTypes.node,
612614
columnFilterSlot: PropTypes.node,
613-
detailsSlot: PropTypes.node,
615+
detailsSlot: PropTypes.node,//?
614616
noItemViewSlot: PropTypes.node,
615617
captionSlot: PropTypes.node,
616618
underTableSlot: PropTypes.node,
619+
scopedSlots: PropTypes.object,
617620
fields: PropTypes.array,
618621
pagination: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
619622
activePage: PropTypes.number,
@@ -637,14 +640,19 @@ CDataTable.propTypes = {
637640
itemsPerPageSelect: PropTypes.bool,
638641
loading: PropTypes.bool,
639642
change: PropTypes.func, //+
640-
onChange: PropTypes.func
643+
onChange: PropTypes.func,
644+
sorterValue: PropTypes.object,
645+
columnFilterValue: PropTypes.object
641646
}
642647

643648
CDataTable.defaultProps = {
644649
itemsPerPage: 10,
645650
responsive: true,
646651
columnHeaderSlot: [],
647652
columnFilterSlot: [],
653+
sorterValue: {}
648654
}
649655

656+
CDataTable.Context = React.createContext({});
657+
650658
export default CDataTable;

0 commit comments

Comments
 (0)