|
1 | 1 | import {
|
2 | 2 | type ObservableType,
|
3 | 3 | createCustomElement,
|
4 |
| - useObserve, |
| 4 | + observeCommonObject, |
| 5 | + ProxiedArray, |
5 | 6 | } from "@michijs/michijs";
|
6 | 7 |
|
7 | 8 | function _random(max: number) {
|
@@ -67,41 +68,45 @@ const adjectivesLength = adjectives.length;
|
67 | 68 | const coloursLength = colours.length;
|
68 | 69 | const nounsLength = nouns.length;
|
69 | 70 |
|
70 |
| -type Row = { label: string; id: number; selected?: string }; |
| 71 | +interface Row { |
| 72 | + label: ObservableType<string>; |
| 73 | + id: number; |
| 74 | + selected: ObservableType<string | undefined>; |
| 75 | +} |
71 | 76 | let nextId = 1;
|
72 |
| -let selectedItem: ObservableType<Row> | null = null; |
| 77 | +let selectedItem: Row | null = null; |
73 | 78 | function buildData(count = 1000) {
|
74 | 79 | const data = new Array<Row>(count);
|
75 | 80 | for (let i = 0; i < count; i++)
|
76 | 81 | data[i] = {
|
| 82 | + selected: observeCommonObject<string | undefined>(undefined), |
77 | 83 | id: nextId++,
|
78 |
| - label: `${adjectives[_random(adjectivesLength)]} ${ |
79 |
| - colours[_random(coloursLength)] |
80 |
| - } ${nouns[_random(nounsLength)]}`, |
| 84 | + label: observeCommonObject( |
| 85 | + `${adjectives[_random(adjectivesLength)]} ${colours[_random(coloursLength)]} ${nouns[_random(nounsLength)]}`, |
| 86 | + ), |
81 | 87 | };
|
82 | 88 | return data;
|
83 | 89 | }
|
84 |
| -const rows = useObserve<Row[]>([]); |
| 90 | +const rows = new ProxiedArray<Row>([], undefined, true); |
85 | 91 | const run = () => rows.$replace(...buildData());
|
86 | 92 | const runLots = () => rows.$replace(...buildData(10000));
|
87 | 93 | const add = () => rows.push(...buildData());
|
88 | 94 | const update = () => {
|
89 |
| - for (let i = 0; i < rows.length; i += 10) { |
90 |
| - // Will be solved on https://github.com/microsoft/TypeScript/issues/43826 |
91 |
| - // @ts-ignore |
92 |
| - rows[i].label += " !!!"; |
| 95 | + const array = rows.$value; |
| 96 | + const length = array.length; |
| 97 | + for (let i = 0; i < length; i += 10) { |
| 98 | + const label = array[i].label; |
| 99 | + label(`${label()} !!!`); |
93 | 100 | }
|
94 | 101 | };
|
95 | 102 | const clear = () => rows.$clear();
|
96 |
| -const select = (row: ObservableType<Row>) => { |
97 |
| - // Will be solved on https://github.com/microsoft/TypeScript/issues/43826 |
98 |
| - // @ts-ignore |
99 |
| - row.selected = "danger"; |
| 103 | +const select = (row: Row) => { |
| 104 | + row.selected("danger"); |
100 | 105 | if (selectedItem) selectedItem.selected(undefined);
|
101 | 106 | selectedItem = row;
|
102 | 107 | };
|
103 |
| -const deleteItem = (id: ObservableType<number>) => |
104 |
| - rows.$remove(rows.findIndex((x) => x.id === id)); |
| 108 | +const deleteItem = (id: number) => |
| 109 | + rows.$remove(rows.$value.findIndex((x) => x.id === id)); |
105 | 110 | const swapRows = () => rows.$swap(1, 998);
|
106 | 111 |
|
107 | 112 | export const Table = createCustomElement("michi-table", {
|
|
0 commit comments