Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept an array of primitives #215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {newId} from "./id.js";
import {identity} from "./identity.js";
import {defined, ascending, descending} from "./sort.js";

const primitive = Symbol("primitive");
const rowHeight = 22;

export function table(data, options = {}) {
Expand Down Expand Up @@ -61,6 +62,7 @@ function initialize(
if (layout === undefined) layout = columns.length >= 12 ? "auto" : "fixed";
format = formatof(format, data, columns, locale);
align = alignof(align, data, columns);
const primitiveFormat = formatLocaleAuto(locale);

let array = [];
let index = [];
Expand Down Expand Up @@ -97,7 +99,7 @@ function initialize(

const tbody = html`<tbody>`;
const tr = html`<tr><td><input type=${multiple ? "checkbox" : "radio"} name=${multiple ? null : "radio"}></td>${columns.map(() => html`<td>`)}`;
const theadr = html`<tr><th><input type=checkbox onclick=${reselectAll} disabled=${!multiple}></th>${columns.map((column) => html`<th title=${column} onclick=${event => resort(event, column)}><span></span>${header && column in header ? header[column] : column}</th>`)}</tr>`;
const theadr = html`<tr><th><input type=checkbox onclick=${reselectAll} disabled=${!multiple}></th>${columns.map((column) => html`<th title=${column === primitive ? null : column} onclick=${event => resort(event, column)}><span></span>${header && column in header ? header[column] : column === primitive ? "•••" : column}</th>`)}</tr>`;
root.appendChild(html.fragment`<table style=${{tableLayout: layout}}>
<thead>${minlengthof(1) || columns.length ? theadr : null}</thead>
${tbody}
Expand Down Expand Up @@ -128,13 +130,19 @@ function initialize(
input.onclick = reselect;
input.checked = selected.has(i);
input.value = i;
if (d != null) for (let j = 0; j < columns.length; ++j) {
let column = columns[j];
let value = d[column];
if (!defined(value)) continue;
value = format[column](value, i, data);
if (isPrimitive(d)) {
value = primitiveFormat(d);
if (!(value instanceof Node)) value = document.createTextNode(value);
itr.childNodes[j + 1].appendChild(value);
itr.childNodes[1].appendChild(value);
} else {
for (let j = 0; j < columns.length; ++j) {
let column = columns[j];
let value = d[column];
if (!defined(value)) continue;
value = format[column](value, i, data);
if (!(value instanceof Node)) value = document.createTextNode(value);
itr.childNodes[j + 1].appendChild(value);
}
}
tbody.append(itr);
}
Expand Down Expand Up @@ -229,7 +237,7 @@ function initialize(
currentReverse = event.altKey;
}
const order = currentReverse ? descending : ascending;
compare = (a, b) => order(array[a][column], array[b][column]);
compare = (a, b) => isPrimitive(array[a]) || isPrimitive(array[b]) ? order(array[a], array[b]) : order(array[a][column], array[b][column]);
orderof(th).textContent = currentReverse ? "▾" : "▴";
}
index.sort(compare);
Expand Down Expand Up @@ -374,9 +382,18 @@ function lengthof(data) {
function columnsof(data) {
const columns = new Set();
for (const row of data) {
for (const name in row) {
columns.add(name);
if (row == null) continue;
if (row != null && isPrimitive(row)) {
columns.add(primitive);
} else {
for (const name in row) {
columns.add(name);
}
}
}
return Array.from(columns);
return Array.from(columns).sort((a, b) => (b === primitive) - (a === primitive));
}

function isPrimitive(row) {
return typeof row === "string" || typeof row === "boolean" || typeof row === "number" || row == null;
}
8 changes: 8 additions & 0 deletions test/inputs/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ export function tableCustomHeader() {
export function tableCustomHeaderHtml() {
return Inputs.table([{foo: "hello"}], {header: {foo: html`<i>Foo</i>`}});
}

export function tablePrimitives() {
return Inputs.table(["hello", "world", 1, true, false, null]);
}

export function tableWithNulls() {
return Inputs.table([{foo: "hello"}, null, undefined, {foo: 1}]);
}
37 changes: 37 additions & 0 deletions test/output/tablePrimitives.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<form class="__ns__ __ns__-table" id="__ns__-1" style="max-height: 274px;">
<table style="table-layout: fixed;">
<thead>
<tr>
<th><input type="checkbox"></th>
<th><span></span>•••</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" value="0"></td>
<td>hello</td>
</tr>
<tr>
<td><input type="checkbox" value="1"></td>
<td>world</td>
</tr>
<tr>
<td><input type="checkbox" value="2"></td>
<td>1</td>
</tr>
<tr>
<td><input type="checkbox" value="3"></td>
<td>true</td>
</tr>
<tr>
<td><input type="checkbox" value="4"></td>
<td>false</td>
</tr>
<tr>
<td><input type="checkbox" value="5"></td>
<td></td>
</tr>
</tbody>
</table>
<style></style>
</form>
29 changes: 29 additions & 0 deletions test/output/tableWithNulls.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<form class="__ns__ __ns__-table" id="__ns__-1" style="max-height: 274px;">
<table style="table-layout: fixed;">
<thead>
<tr>
<th><input type="checkbox"></th>
<th title="foo"><span></span>foo</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" value="0"></td>
<td>hello</td>
</tr>
<tr>
<td><input type="checkbox" value="1"></td>
<td></td>
</tr>
<tr>
<td><input type="checkbox" value="2"></td>
<td></td>
</tr>
<tr>
<td><input type="checkbox" value="3"></td>
<td>1</td>
</tr>
</tbody>
</table>
<style></style>
</form>