-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add selection
#13
base: master
Are you sure you want to change the base?
add selection
#13
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've got no issue with the breaking change. My only gripe is with the active item styles.
src/Typeahead.svelte
Outdated
@@ -228,18 +236,23 @@ | |||
background-color: #cacaca; | |||
} | |||
|
|||
.active, | |||
li:not(:last-of-type).active { | |||
border: #0f62fe 1px solid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not keen on using a border for the active state. It causes the text of an active item to shift. I'd prefer keeping the existing style of using a background color to distinguish the active but not selected state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just also pushed a change which prevents disabled items from beeing select by keyboard or as first entry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for extracting it into a function? I prefer keeping the logic in the markup
its self-referencing, so I think it has to be a function. And its also called in |
Sorry, to less sleep last night, I missed updating the |
A couple of things:
|
I increment the selectedIndex as long as the selectedItem turns out to be disabled. To prevent infinite loop I stop after I had different approaches now, first is the function way of this pr, tested and working function setNextSelectedIndex(i = 0) {
i += 1; // index for stop the itteration
selectedIndex += 1;
if (selectedIndex === results.length) {
selectedIndex = 0; // start over at the beginning of the array
}
if(results.lenght == 0 || !(selectedIndex in results) || i > results.lenght) {
selectedIndex = -1; // no result found, gone trough the whole array
} else if(results[selectedIndex].disabled) {
setNextSelectedIndex(i); // no result found, not yet gone trough the whole array
}
}; In think an while loop would look something like that, but i havent tested yet selectedIndex += 1;
if (selectedIndex === results.length) {
selectedIndex = 0;
}
let i = 0;
while(selectedIndex in results && results[selectedIndex].disabled && results.lenght > i) {
i += 1;
selectedIndex += 1;
if (selectedIndex === results.length) {
selectedIndex = 0;
}
}
if(results[selectedIndex].disabled) {
selectedIndex = -1;
} There was one approach with a lot of I found the for working pretty nice and will put this into this pr. for (selectedIndex++;(selectedIndex in results && results[selectedIndex].disabled); selectedIndex++) { }
if(!(selectedIndex in results) || (selectedIndex in results && results[selectedIndex].disabled)) {
selectedIndex = results.findIndex(result => !result.disabled);
} for the decrementation works similar: for (selectedIndex--;(selectedIndex in results && results[selectedIndex].disabled); selectedIndex--) { }
if(!(selectedIndex in results) || (selectedIndex in results && results[selectedIndex].disabled)) {
let reverseselectedIndex = results.slice().reverse().findIndex(result => !result.disabled) + 1;
selectedIndex = (reverseselectedIndex == -1) ? -1 : (results.length - reverseselectedIndex);
} In the |
it feels a bit weird to have a for-loop without an acutal function. So I don't know whether to write |
A couple of requirements:
|
Surely there's a more "Svelte" way of doing this. Just thinking a loud here. Say that you have a reactive assignment that only tracks non-disabled items:
Then when incrementing/decrementing using arrow keys, you could use |
Ok, I'll try again. Thanks for your patience, I`m pretty new to svelte. My first approach still uses
case 'ArrowDown':
e.preventDefault();
enabledIndex = enabledResults.findIndex(item => item == results[selectedIndex]);
if(enabledIndex == -1) break;
enabledIndex += 1;
if (enabledIndex === enabledResults.length) {
enabledIndex = 0;
}
selectedIndex = results.findIndex(item => item == enabledResults[enabledIndex]);
break;
case 'ArrowUp':
e.preventDefault();
enabledIndex = enabledResults.findIndex(item => item == results[selectedIndex]);
if(enabledIndex == -1) break;
enabledIndex -= 1;
if (enabledIndex < 0) {
enabledIndex = enabledResults.length - 1;
}
selectedIndex = results.findIndex(item => item == enabledResults[enabledIndex]);
break; |
I'll go with |
Introducing a filter for selecting items additional to
filter
anddisable
.This gives selected items an class, which could be styled from outside.
Actually I keept
selected
as class name, which was used for active/focused items. So this might be a breaking change and I would totally undertand if you dislike this.For the former
selected
class I introducedactive
and changed the styling to a light blue border.Here is the code where I use all functions, if you think this change is reasonable I will also write an update for the readme:
I further changed the global css classes a bit, to keep their scope in the component and its sub-components:
:global([data-svelte-search] label)
to
[data-svelte-typeahead] :global([data-svelte-search] label)