Skip to content

Commit a5622f5

Browse files
committed
input styles and select arrow keys
1 parent 817063e commit a5622f5

4 files changed

Lines changed: 67 additions & 7 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:host {
2+
display: block;
3+
}
4+
5+
input {
6+
width: 100%;
7+
}

src/components/pg/inputSelect/__examples__/basic/basic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export default class XPgInputSelectBasic extends HTMLElement {
2424
this.$input.addEventListener('change', this.#handleChange.bind(this));
2525
}
2626

27-
#handleChange(e) {
27+
#handleChange(e: any) {
2828
const { value } = e.detail;
2929
this.$input.value = value;
30-
this.$value.innerText = `${value}`;
30+
this.$value.textContent = `${value}`;
3131
}
3232
}

src/components/pg/inputSelect/inputSelect.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
[part=label] {
2525
grid-row: 1;
2626
grid-column: 1;
27-
padding: var(--pg-input-select-padding, 0.5rem 0.75rem);
27+
padding: var(--pg-input-select-padding, 0.25rem 0.75rem);
2828
font-size: var(--pg-input-select-font-size, 1rem);
2929
}
3030

src/components/pg/inputSelect/inputSelect.ts

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ export default class PgInputSelect extends HTMLElement {
2525

2626
connectedCallback() {
2727
this.$button.addEventListener('click', this.#handleClick.bind(this));
28+
this.$button.addEventListener('keydown', this.#handleKeyPress.bind(this));
2829
}
2930

3031
render(changes) {
3132
if (changes.value || changes.default) {
3233
this.$label.textContent = this.value
33-
? this.value
34+
? (this.options.find(x => x.value === this.value) ?? { label: this.value }).label
3435
: this.default
3536
? this.default.label
3637
: '\u00A0'; // nbsp
@@ -45,16 +46,68 @@ export default class PgInputSelect extends HTMLElement {
4546
source: this.$button,
4647
default: this.default,
4748
value: this.options.find(x => x.value === this.value) ?? null,
48-
items: this.options
49+
items: this.options,
4950
});
5051
if (result !== undefined) {
5152
this.dispatchEvent(new CustomEvent('change', {
5253
detail: {
53-
value: result.value
54-
}
54+
value: result.value,
55+
},
5556
}));
5657
this.$label.textContent = result.label;
5758
}
5859
this.#menuOpen = false;
5960
}
61+
62+
#handleKeyPress(e: KeyboardEvent) {
63+
switch (e.key) {
64+
case 'ArrowDown':
65+
e.preventDefault();
66+
if (this.options.length <= 1) {
67+
return;
68+
}
69+
this.#handleKeyDownArrowDown();
70+
break;
71+
case 'ArrowUp':
72+
e.preventDefault();
73+
if (this.options.length <= 1) {
74+
return;
75+
}
76+
this.#handleKeyDownArrowUp();
77+
break;
78+
}
79+
}
80+
81+
#handleKeyDownArrowDown() {
82+
const index = this.options.findIndex(x => x.value === this.value);
83+
let newValue = '';
84+
if (index === -1 || index === this.options.length - 1) {
85+
// select first or loop to top
86+
newValue = this.options[0].value;
87+
} else {
88+
newValue = this.options[index + 1].value;
89+
}
90+
this.dispatchEvent(new CustomEvent('change', {
91+
detail: {
92+
value: newValue,
93+
},
94+
}));
95+
}
96+
97+
#handleKeyDownArrowUp() {
98+
const index = this.options.findIndex(x => x.value === this.value);
99+
const normalizeIndex = index === -1 ? 0 : index;
100+
let newValue = '';
101+
if (normalizeIndex === 0) {
102+
// loop to bottom
103+
newValue = this.options[this.options.length - 1].value;
104+
} else {
105+
newValue = this.options[normalizeIndex - 1].value;
106+
}
107+
this.dispatchEvent(new CustomEvent('change', {
108+
detail: {
109+
value: newValue,
110+
},
111+
}));
112+
}
60113
}

0 commit comments

Comments
 (0)