/
index.ts
61 lines (49 loc) · 1.24 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { tag, Tag } from '@storefront/core';
import Select from '../select';
export const DEFAULT_OPTION_ALIAS = 'option';
export const DEFAULT_INDEX_ALIAS = 'i';
@tag('gb-select-option', '<yield/>')
class SelectOption {
root: HTMLOptionElement;
option: Select.Option;
i: number;
props: SelectOption.Props = {
optionAlias: DEFAULT_OPTION_ALIAS,
indexAlias: DEFAULT_INDEX_ALIAS,
};
init() {
this.provide(this.props.optionAlias, (_, { option }) => option);
this.provide(this.props.indexAlias, (_, { index }) => index);
}
onBeforeMount() {
this.updateState();
}
onMount() {
this.updateContent();
}
onUpdate() {
this.updateContent();
this.updateState();
}
updateState() {
this.state = { ...this.state, option: this.option, index: this.i };
}
updateContent() {
const textContent = this.root.textContent;
if (textContent !== this.root.label) {
this.root.label = textContent;
}
}
}
interface SelectOption extends Tag<SelectOption.Props, SelectOption.State> {}
namespace SelectOption {
export interface Props {
optionAlias: string;
indexAlias: string;
}
export interface State {
option: Select.Option;
index: number;
}
}
export default SelectOption;