Skip to content

Commit dceb6bb

Browse files
committed
First commit for table component (#5)
1 parent 9898fc2 commit dceb6bb

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ export * from './src/components/ToggleField';
5252
export * from './src/components/RangeField';
5353
export * from './src/components/Icon';
5454
export * from './src/components/Expander';
55+
export * from './src/components/Table';
5556
export * from './src/provider';

index.scss

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ $base-margin: 1em;
4545
@import "./src/styles/_toolbar.scss";
4646
@import "./src/styles/_iframe.scss";
4747
@import "./src/styles/_expander.scss";
48+
@import "./src/styles/_table.scss";

src/components/Table.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* OS.js - JavaScript Cloud/Web Desktop Platform
3+
*
4+
* Copyright (c) 2011-2018, Anders Evenrud <[email protected]>
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice, this
11+
* list of conditions and the following disclaimer
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*
27+
* @author Anders Evenrud <[email protected]>
28+
* @licence Simplified BSD License
29+
*/
30+
31+
import {h} from 'hyperapp';
32+
import {Element} from './Element';
33+
34+
const data = c => {
35+
if (typeof c === 'undefined' || c === null) {
36+
return '';
37+
} else if (typeof c === 'function') {
38+
return c();
39+
} else if (typeof c === 'object') {
40+
return c.label;
41+
}
42+
43+
return String(c);
44+
};
45+
46+
const col = (tagName, onclick) => (c, props = {}, children = []) => h(tagName, props, h('div', {
47+
onclick
48+
}, [
49+
h('span', {}, data(c)),
50+
...children
51+
]));
52+
53+
const createHeader = ({sortBy, sortDir, columns, onsort}) => h('thead', {}, h('tr', {}, columns.map((c, i) => {
54+
const active = sortBy === i;
55+
56+
return col('th', ev => {
57+
if (typeof onsort === 'function') {
58+
const dir = !sortDir
59+
? 'asc' : (sortDir === 'asc' ? 'desc' : null);
60+
61+
onsort(ev, i, dir, c);
62+
}
63+
})(c, {
64+
'data-sort': active,
65+
'data-direction': active ? sortDir : false
66+
}, [h('span')]);
67+
})));
68+
69+
const createBody = rows => h('tbody', {}, rows.map(columns => h('tr', {}, columns.map(col('td')))));
70+
71+
const createFooter = columns => h('tfoot', {}, h('tr', {}, columns.map(col('td'))));
72+
73+
/**
74+
* A toolbar
75+
* @desc Contains entries with spacing
76+
* @param {BoxProperties} props Properties
77+
* @param {h[]} children Children
78+
*/
79+
export const Table = (props = {}, children = []) => {
80+
const newProps = Object.assign({
81+
columns: [],
82+
rows: [],
83+
footer: [],
84+
sortBy: null,
85+
sortDir: null,
86+
fixed: true,
87+
collapse: true,
88+
zebra: false
89+
}, props);
90+
91+
const boxProps = Object.assign({}, props.box || {}, {
92+
class: ['osjs-gui-table', props.class]
93+
});
94+
95+
96+
return h(Element, boxProps, h('table', {
97+
'data-collapse': newProps.collapse === true,
98+
'data-fixed': newProps.fixed === true,
99+
'data-zebra': newProps.zebra === true
100+
}, [
101+
createHeader(newProps),
102+
createBody([
103+
...newProps.rows,
104+
...children
105+
]),
106+
createFooter(newProps.footer)
107+
]));
108+
};

src/styles/_table.scss

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* OS.js - JavaScript Cloud/Web Desktop Platform
3+
*
4+
* Copyright (c) 2011-2018, Anders Evenrud <[email protected]>
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice, this
11+
* list of conditions and the following disclaimer
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*
27+
* @author Anders Evenrud <[email protected]>
28+
* @licence Simplified BSD License
29+
*/
30+
31+
.osjs-gui-table {
32+
overflow: auto;
33+
34+
table,
35+
tr,
36+
td,
37+
th, {
38+
box-sizing: border-box;
39+
}
40+
41+
& > table {
42+
min-width: 100%;
43+
44+
&[data-fixed="true"] {
45+
table-layout: fixed;
46+
}
47+
48+
&[data-collapse="true"] {
49+
border-collapse: collapse;
50+
}
51+
52+
th,
53+
td {
54+
text-align: left;
55+
font-weight: normal;
56+
padding: $base-margin / 2;
57+
}
58+
59+
th > div {
60+
display: flex;
61+
align-items: center;
62+
cursor: pointer;
63+
64+
& > span:first-child {
65+
flex: 1;
66+
}
67+
}
68+
}
69+
70+
th[data-sort="true"] {
71+
&[data-direction="asc"] > div > span:last-child::after {
72+
content: '\25BC';
73+
}
74+
75+
&[data-direction="desc"] > div > span:last-child::after {
76+
content: '\25B2';
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)