-
-
Notifications
You must be signed in to change notification settings - Fork 261
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
fix(datatable): fix sort parameters #356
fix(datatable): fix sort parameters #356
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/carbon-svelte/carbon-components-svelte/4xa4r9s43 |
@albertms10 Could you add an example that adapts the existing "Sortable" example that demonstrates the custom sort method? |
Something like using <script lang="ts">
import { DataTable } from "carbon-components-svelte";
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import { number } from "svelte-i18n";
dayjs.extend(relativeTime); // for `dayjs().fromNow()` method
const headers = [
{
key: "name",
value: "Name",
},
{
key: "quantity",
value: "Quantity",
display: (quantity: number) => $number(quantity, { format: "EUR" }),
},
{
key: "date",
value: "Date",
display: (date: string) => dayjs(date).fromNow(),
sort: (a: string, b: string) => (dayjs(a).isBefore(dayjs(b)) ? -1 : 1),
},
];
const rows = [
{
id: 1,
name: "John",
quantity: 157.89,
date: "2020-10-21",
}
];
</script>
<DataTable {headers} {rows} /> If you find it fine, may I add it to the docs? P.S.: It would be great if the // Typing `headers` with `rows` objects data
const headers = [
...
{
key: "quantity",
value: "Quantity",
display: (quantity: rows[number]["quantity"]) => $number(quantity, { format: "EUR" }),
},
{
key: "date",
value: "Date",
display: (date: rows[number]["date"]) => dayjs(date).fromNow(),
sort: (a: rows[number]["date"], b: rows[number]["date"]) => (dayjs(a).isBefore(dayjs(b)) ? -1 : 1),
},
]; But, in order to force // Typing `rows` with a `headersList` array
const headersList = ["name", "quantity", "date"] as const;
type DataTableRows = {
[P in typeof headersList[number] | "id"]: string;
};
const rows: DataTableRows = [
{
id: 1,
nam: "John", // Will trigger a TS error
quantity: 157.89,
date: "2020-10-21",
},
]; |
To avoid adding additional dependencies, can you simplify the example even further to omit dayjs, svelte-i18n? |
Ok, what about: <script lang="ts">
import { DataTable } from "carbon-components-svelte";
const headers = [
{
key: "name",
value: "Name",
},
{
key: "quantity",
value: "Quantity",
display: (quantity: number) => `${quantity} €`,
},
{
key: "date",
value: "Date",
display: (date: string) => new Date(date).toLocaleString(),
sort: (a: string, b: string) => new Date(a) - new Date(b),
},
];
const rows = [
{
id: 1,
name: "John",
quantity: 157.89,
date: "2020-10-21",
}
];
</script>
<DataTable {headers} {rows} /> |
That works |
What do you think, @metonym, about the types that involve |
@albertms10 For the TypeScript comment, I'm not sure how it would be automatically inferred without typing it. Would typing Something like: const headers: [{key: "quantity", value: "Quantity", display: (quantity: number) => string;}] = [{
key: "quantity",
value: "Quantity",
display: (quantity: number) => `${quantity} €`,
}] |
To get things properly organized, I opened a new issue (#364) to talk about TypeScript type definitions and let this PR merge the way it should. |
fixes #355