@pictogrammers/components 0.5.14 → 0.5.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pictogrammers/components",
3
3
  "type": "module",
4
- "version": "0.5.14",
4
+ "version": "0.5.15",
5
5
  "license": "MIT",
6
6
  "author": "Austin Andrews",
7
7
  "scripts": {
@@ -26,6 +26,16 @@ export default class PgInputNumber extends HTMLElement {
26
26
  connectedCallback() {
27
27
  this.$input.addEventListener('input', this.handleInput.bind(this));
28
28
  this.$input.addEventListener('change', this.handleChange.bind(this));
29
+ this.$input.addEventListener('beforeinput', (e: KeyboardEvent) => {
30
+ switch (e.key) {
31
+ case 'ArrowUp':
32
+ this.#triggerInput(this.value + this.step);
33
+ break;
34
+ case 'ArrowDown':
35
+ this.#triggerInput(this.value - this.step);
36
+ break;
37
+ }
38
+ });
29
39
  this.$buttonMinus.addEventListener('increment', (e: any) => {
30
40
  this.#triggerInput(this.value - this.step);
31
41
  });
@@ -33,18 +43,10 @@ export default class PgInputNumber extends HTMLElement {
33
43
  this.#triggerInput(this.value + this.step);
34
44
  });
35
45
  this.$buttonMinus.addEventListener('finish', (e: any) => {
36
- this.dispatchEvent(new CustomEvent('change', {
37
- detail: {
38
- value: this.value,
39
- },
40
- }));
46
+ this.#triggerChange(this.value);
41
47
  });
42
48
  this.$buttonPlus.addEventListener('finish', (e: any) => {
43
- this.dispatchEvent(new CustomEvent('change', {
44
- detail: {
45
- value: this.value,
46
- },
47
- }));
49
+ this.#triggerChange(this.value);
48
50
  });
49
51
  }
50
52
 
@@ -54,6 +56,12 @@ export default class PgInputNumber extends HTMLElement {
54
56
  if (changes.value && !this.skipValue) {
55
57
  this.$input.value = `${this.value}`;
56
58
  }
59
+ if (changes.min) {
60
+ this.$input.min = `${this.min}`;
61
+ }
62
+ if (changes.max) {
63
+ this.$input.max = `${this.max}`;
64
+ }
57
65
  if (changes.placeholder) {
58
66
  this.$input.placeholder = this.placeholder;
59
67
  }
@@ -64,6 +72,9 @@ export default class PgInputNumber extends HTMLElement {
64
72
  }
65
73
 
66
74
  #triggerInput(value) {
75
+ if (value < this.min || value > this.max) {
76
+ return;
77
+ }
67
78
  this.dispatchEvent(
68
79
  new CustomEvent('input', {
69
80
  detail: {
@@ -81,14 +92,21 @@ export default class PgInputNumber extends HTMLElement {
81
92
  this.#triggerInput(value);
82
93
  }
83
94
 
84
- handleChange(e) {
95
+ #triggerChange(value) {
96
+ if (value < this.min || value > this.max) {
97
+ return;
98
+ }
85
99
  this.dispatchEvent(
86
100
  new CustomEvent('change', {
87
101
  detail: {
88
- value: e.target.value,
89
- name: e.name
102
+ value: value,
103
+ name: this.name,
90
104
  }
91
105
  })
92
106
  );
93
107
  }
108
+
109
+ handleChange(e) {
110
+ this.#triggerChange(e.target.value);
111
+ }
94
112
  }
@@ -0,0 +1,7 @@
1
+ :host {
2
+ display: block;
3
+ }
4
+
5
+ input {
6
+ width: 100%;
7
+ }
@@ -24,9 +24,9 @@ export default class XPgInputSelectBasic extends HTMLElement {
24
24
  this.$input.addEventListener('change', this.#handleChange.bind(this));
25
25
  }
26
26
 
27
- #handleChange(e) {
27
+ #handleChange(e: any) {
28
28
  const { value } = e.detail;
29
29
  this.$input.value = value;
30
- this.$value.innerText = `${value}`;
30
+ this.$value.textContent = `${value}`;
31
31
  }
32
32
  }
@@ -24,7 +24,7 @@
24
24
  [part=label] {
25
25
  grid-row: 1;
26
26
  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);
28
28
  font-size: var(--pg-input-select-font-size, 1rem);
29
29
  }
30
30
 
@@ -25,12 +25,13 @@ export default class PgInputSelect extends HTMLElement {
25
25
 
26
26
  connectedCallback() {
27
27
  this.$button.addEventListener('click', this.#handleClick.bind(this));
28
+ this.$button.addEventListener('keydown', this.#handleKeyPress.bind(this));
28
29
  }
29
30
 
30
31
  render(changes) {
31
32
  if (changes.value || changes.default) {
32
33
  this.$label.textContent = this.value
33
- ? this.value
34
+ ? (this.options.find(x => x.value === this.value) ?? { label: this.value }).label
34
35
  : this.default
35
36
  ? this.default.label
36
37
  : '\u00A0'; // nbsp
@@ -45,16 +46,68 @@ export default class PgInputSelect extends HTMLElement {
45
46
  source: this.$button,
46
47
  default: this.default,
47
48
  value: this.options.find(x => x.value === this.value) ?? null,
48
- items: this.options
49
+ items: this.options,
49
50
  });
50
51
  if (result !== undefined) {
51
52
  this.dispatchEvent(new CustomEvent('change', {
52
53
  detail: {
53
- value: result.value
54
- }
54
+ value: result.value,
55
+ },
55
56
  }));
56
57
  this.$label.textContent = result.label;
57
58
  }
58
59
  this.#menuOpen = false;
59
60
  }
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
+ }
60
113
  }