-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(writer): pass generics to
$Props
- Loading branch information
Showing
7 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<script lang="ts"> | ||
import Generics, { type DataTableHeader } from "./output"; | ||
const headers: DataTableHeader<{ name: string; port: number }>[] = [ | ||
{ | ||
key: "name", | ||
value: "Name", | ||
}, | ||
{ | ||
key: "port", | ||
value: "Port", | ||
}, | ||
]; | ||
const rows = [ | ||
{ id: 1, name: "Name 1", port: 3000 }, | ||
{ id: 2, name: "Name 2", port: 3000 }, | ||
{ id: 3, name: "Name 3", port: 3000 }, | ||
] as const; | ||
</script> | ||
|
||
<Generics {headers} {rows} let:rows> | ||
{#each rows as row} | ||
{row.name} | ||
{row.port} | ||
{/each} | ||
</Generics> | ||
|
||
<Generics | ||
headers={[ | ||
{ | ||
key: "name", | ||
value: "Name", | ||
}, | ||
{ | ||
key: "port", | ||
value: "Port", | ||
}, | ||
]} | ||
rows={[ | ||
{ id: 1, name: "Name 1", port: 3000 }, | ||
{ id: 2, name: "Name 2", port: 3000 }, | ||
{ id: 3, name: "Name 3", port: 3000 }, | ||
]} | ||
let:rows | ||
> | ||
{#each rows as row} | ||
{row.name} | ||
{/each} | ||
</Generics> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script> | ||
/**} | ||
* @typedef {{ id: string | number; [key: string]: any; }} DataTableRow | ||
* @typedef {Exclude<keyof Row, "id">} DataTableKey<Row> | ||
* @typedef {{ key: DataTableKey<Row>; value: string; }} DataTableHeader<Row=DataTableRow> | ||
* @template {DataTableRow} <Row extends DataTableRow = DataTableRow> | ||
* @generics {Row extends DataTableRow = DataTableRow} Row | ||
*/ | ||
/** @type {ReadonlyArray<DataTableHeader<Row>>} */ | ||
export let headers = []; | ||
/** @type {ReadonlyArray<Row>} */ | ||
export let rows = []; | ||
</script> | ||
|
||
<div {...$$restProps}> | ||
<slot {headers} {rows} /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { SvelteComponentTyped } from "svelte"; | ||
import type { SvelteHTMLElements } from "svelte/elements"; | ||
|
||
export interface DataTableRow { | ||
id: string | number; | ||
[key: string]: any; | ||
} | ||
|
||
export type DataTableKey<Row> = Exclude<keyof Row, "id">; | ||
|
||
export interface DataTableHeader<Row = DataTableRow> { | ||
key: DataTableKey<Row>; | ||
value: string; | ||
} | ||
|
||
type RestProps = SvelteHTMLElements["div"]; | ||
|
||
type $Props<Row> = { | ||
/** | ||
* @default [] | ||
*/ | ||
headers?: ReadonlyArray<DataTableHeader<Row>>; | ||
|
||
/** | ||
* @default [] | ||
*/ | ||
rows?: ReadonlyArray<Row>; | ||
|
||
[key: `data-${string}`]: any; | ||
}; | ||
|
||
export type GenericsWithRestPropsProps<Row> = Omit<RestProps, keyof $Props<Row>> & $Props<Row>; | ||
|
||
export default class GenericsWithRestProps<Row extends DataTableRow = DataTableRow> extends SvelteComponentTyped< | ||
GenericsWithRestPropsProps<Row>, | ||
Record<string, any>, | ||
{ default: { headers: ReadonlyArray<DataTableHeader<Row>>; rows: ReadonlyArray<Row> } } | ||
> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"props": [ | ||
{ | ||
"name": "headers", | ||
"kind": "let", | ||
"type": "ReadonlyArray<DataTableHeader<Row>>", | ||
"value": "[]", | ||
"isFunction": false, | ||
"isFunctionDeclaration": false, | ||
"isRequired": false, | ||
"constant": false, | ||
"reactive": false | ||
}, | ||
{ | ||
"name": "rows", | ||
"kind": "let", | ||
"type": "ReadonlyArray<Row>", | ||
"value": "[]", | ||
"isFunction": false, | ||
"isFunctionDeclaration": false, | ||
"isRequired": false, | ||
"constant": false, | ||
"reactive": false | ||
} | ||
], | ||
"moduleExports": [], | ||
"slots": [ | ||
{ | ||
"name": "__default__", | ||
"default": true, | ||
"slot_props": "{ headers: ReadonlyArray<DataTableHeader<Row>>, rows: ReadonlyArray<Row> }" | ||
} | ||
], | ||
"events": [], | ||
"typedefs": [ | ||
{ | ||
"type": "{ id: string | number; [key: string]: any; }", | ||
"name": "DataTableRow", | ||
"ts": "interface DataTableRow { id: string | number; [key: string]: any; }" | ||
}, | ||
{ | ||
"type": "Exclude<keyof Row, \"id\">", | ||
"name": "DataTableKey<Row>", | ||
"ts": "type DataTableKey<Row> = Exclude<keyof Row, \"id\">" | ||
}, | ||
{ | ||
"type": "{ key: DataTableKey<Row>; value: string; }", | ||
"name": "DataTableHeader<Row=DataTableRow>", | ||
"ts": "interface DataTableHeader<Row=DataTableRow> { key: DataTableKey<Row>; value: string; }" | ||
} | ||
], | ||
"generics": [ | ||
"Row", | ||
"Row extends DataTableRow = DataTableRow" | ||
], | ||
"rest_props": { | ||
"type": "Element", | ||
"name": "div" | ||
} | ||
} |