/
index.ts
46 lines (37 loc) · 960 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { tag, Tag } from '@storefront/core';
import List from '../list';
export const DEFAULT_ITEM_ALIAS = 'item';
export const DEFAULT_INDEX_ALIAS = 'i';
@tag('gb-list-item', '<yield/>')
class ListItem {
item: any;
i: number;
state: ListItem.State = { item: null, index: -1 };
props: ListItem.Props = {
itemAlias: DEFAULT_ITEM_ALIAS,
indexAlias: DEFAULT_INDEX_ALIAS,
};
init() {
this.provide(this.props.itemAlias, (_, { item }) => item);
this.provide(this.props.indexAlias, (_, { index }) => index);
this.updateState();
}
onUpdate() {
this.updateState();
}
updateState() {
this.state = { ...this.state, item: this.item, index: this.i };
}
}
interface ListItem extends Tag<ListItem.Props, ListItem.State> {}
namespace ListItem {
export interface Props {
itemAlias?: string;
indexAlias?: string;
}
export interface State {
item: any;
index: number;
}
}
export default ListItem;