Skip to content

Commit ca4d2d0

Browse files
committed
Focus
1 parent b8b8a34 commit ca4d2d0

5 files changed

Lines changed: 77 additions & 5 deletions

File tree

‎src/components/pg/nodeEditorTextArray/nodeEditorTextArray.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="labelAdd">
33
<span part="label"></span>
44
<button part="add">
5-
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="none"><path fill="currentColor" d="M7 2a1 1 0 0 1 1 1v3h3a1 1 0 1 1 0 2H8v3a1 1 0 1 1-2 0V8H3a1 1 0 0 1 0-2h3V3a1 1 0 0 1 1-1"/></svg>
5+
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14"><path fill="currentColor" d="M7 2a1 1 0 0 1 1 1v3h3a1 1 0 1 1 0 2H8v3a1 1 0 1 1-2 0V8H3a1 1 0 0 1 0-2h3V3a1 1 0 0 1 1-1"/></svg>
66
</button>
77
</div>
88
<div part="inputs"></div>

‎src/components/pg/nodeEditorTextArray/nodeEditorTextArray.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export default class PgNodeEditorTextArray extends HTMLElement {
3131

3232
@Prop() #inputs: any[] = [];
3333

34+
#focusNext = false;
35+
3436
connectedCallback() {
3537
// Map this.value to list
3638
this.value.forEach((value) => {
@@ -64,10 +66,39 @@ export default class PgNodeEditorTextArray extends HTMLElement {
6466
detail: { value: newValue },
6567
}));
6668
});
69+
$item.addEventListener('inputprevious', (e: any) => {
70+
const { index, selectionIndex } = e.detail;
71+
const $items = Array.from(this.$inputs.children);
72+
if ($items.length > 1) {
73+
if (index === 0) {
74+
($items[$items.length - 1] as any).focus();
75+
} else {
76+
($items[index - 1] as any).focus();
77+
}
78+
}
79+
});
80+
$item.addEventListener('inputnext', (e: any) => {
81+
const { index, selectionIndex } = e.detail;
82+
const $items = Array.from(this.$inputs.children);
83+
if ($items.length > 1) {
84+
if (index === $items.length - 1) {
85+
($items[0] as any).focus();
86+
} else {
87+
($items[index + 1] as any).focus();
88+
}
89+
}
90+
});
91+
},
92+
connect: ($item) => {
93+
if (this.#focusNext) {
94+
$item.focus();
95+
this.#focusNext = false;
96+
}
6797
},
6898
});
6999

70100
this.$add.addEventListener('click', () => {
101+
this.#focusNext = true;
71102
this.#inputs.push({
72103
key: uuid(),
73104
value: '',

‎src/components/pg/nodeInputText/nodeInputText.css‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,35 @@
4040
z-index: 1;
4141
}
4242

43+
[part=input]:hover + button,
44+
[part=input]:focus + button {
45+
display: flex;
46+
}
47+
4348
.hide {
4449
display: none;
4550
}
4651

52+
[part=input]:has(+ button:active) {
53+
outline-color: #cd2626;
54+
border-radius: 0.125rem;
55+
}
56+
57+
[part=input] + [part=remove]:active {
58+
color: #cd2626;
59+
}
60+
4761
[part=remove] {
62+
display: none;
4863
background: transparent;
4964
border: 0;
5065
color: rgba(255, 255, 255, 0.5);
5166
margin-left: -1rem;
5267
padding: 0;
53-
display: flex;
5468
align-items: center;
5569
z-index: 2;
5670
}
5771
[part=remove]:hover {
5872
color: rgba(255, 255, 255, 0.8);
73+
display: flex;
5974
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<input type="text" part="input">
22
<button part="remove" class="hide">
3-
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="none"><path fill="currentColor" d="M9.535 3.05a1 1 0 0 1 1.414 1.415L8.414 6.999l2.536 2.536a1 1 0 0 1-1.415 1.414L7 8.414 4.465 10.95A1.001 1.001 0 0 1 3.05 9.535L5.585 7 3.05 4.464A1 1 0 0 1 4.465 3.05L7 5.585z"/></svg>
3+
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14"><path fill="currentColor" d="M9.535 3.05a1 1 0 0 1 1.414 1.415L8.414 6.999l2.536 2.536a1 1 0 0 1-1.415 1.414L7 8.414 4.465 10.95A1.001 1.001 0 0 1 3.05 9.535L5.585 7 3.05 4.464A1 1 0 0 1 4.465 3.05L7 5.585z"/></svg>
44
</button>

‎src/components/pg/nodeInputText/nodeInputText.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import style from './nodeInputText.css';
1010
})
1111
export default class PgNodeInputText extends HTMLElement {
1212

13+
@Prop() index: number;
1314
@Prop() value: string = '';
1415
@Prop() name: string = '';
1516
@Prop() removable: boolean = false;
@@ -30,12 +31,37 @@ export default class PgNodeInputText extends HTMLElement {
3031
e.stopPropagation();
3132
this.dispatchEvent(new CustomEvent('input', {
3233
detail: {
33-
value: this.$input.value,
34+
index: this.index,
3435
}
3536
}));
3637
});
38+
this.$input.addEventListener('keydown', (e: KeyboardEvent) => {
39+
if (e.key === 'ArrowUp') {
40+
this.dispatchEvent(new CustomEvent('inputprevious', {
41+
detail: {
42+
index: this.index,
43+
selectionStart: this.$input.selectionDirection
44+
? this.$input.selectionStart
45+
: this.$input.selectionEnd,
46+
}
47+
}));
48+
} else if (e.key === 'ArrowDown') {
49+
this.dispatchEvent(new CustomEvent('inputnext', {
50+
detail: {
51+
index: this.index,
52+
selectionIndex: this.$input.selectionDirection
53+
? this.$input.selectionStart
54+
: this.$input.selectionEnd,
55+
}
56+
}));
57+
}
58+
});
3759
this.$remove.addEventListener('click', (e: any) => {
38-
this.dispatchEvent(new CustomEvent('remove'));
60+
this.dispatchEvent(new CustomEvent('remove', {
61+
detail: {
62+
index: this.index,
63+
}
64+
}));
3965
});
4066
}
4167

0 commit comments

Comments
 (0)