Web Lab 2: autocomplete in preview file name input#70621
Web Lab 2: autocomplete in preview file name input#70621bencodeorg wants to merge 24 commits intostagingfrom
Conversation
| @@ -0,0 +1,18 @@ | |||
| import {MultiFileSource, ProjectFile} from '@cdo/apps/lab2/types'; | |||
|
|
|||
| // Returns a list of files matching the given language (by file.language or extension). | |||
There was a problem hiding this comment.
is this comment correct? It looks like we only check the language field, not the extension.
There was a problem hiding this comment.
Oops good catch, I removed the extension check. Will update.
| const containerRef = useOutsideClick<HTMLDivElement>(reset); | ||
|
|
||
| useEffect(() => { | ||
| if (!value || value.length < 3) { |
There was a problem hiding this comment.
why do we reset if the value's length is less than 3?
There was a problem hiding this comment.
3 is the magic number that the autocomplete doesn't start giving you options until you hit. It was producing some weird behavior where options would still be visible if you deleted your input below three characters and started typing something else:
I'll move 3 into a constant to be shared (we could also configure this via props if we wanted autocomplete to hit faster?)
Before and after video of the updated behavior in this Slack thread if you're curious.
There was a problem hiding this comment.
thanks! If you could also add a comment explaining this that would be great.
There was a problem hiding this comment.
It was producing some weird behavior where options would still be visible if you deleted your input below three characters and started typing something else:
Do you mind explaining what the behavior is you noticed? I see that you've implemented searching the substring within the file pathname. When I set the min length to 1 it seems to work as expected.
Did you consider looking for whether the filename begins with the substring in fetchHtmlOptions? Something like...
// Get the filename (last part after splitting by '/')
const segments = option.toLowerCase().split('/');
const filename = segments[segments.length - 1];
return filename.startsWith(normalizedSearch);
There was a problem hiding this comment.
The "weird behavior" thing was more about what happens if you went under 3 characters, not the number of characters that sets the limit for when we show the dropdown. If you typed more than 3 characters and cleared the input, it would retain the same set of dropdown options until your new query was 3 characters long.
Re: beginning filename/substring, I thought it was a nice feature that it would be searchable by say, folder name? Anyway, I think there are finer points like this that we can iterate on in the implementation here (a couple more in the follow-up section in the description as well).
| setPreviewViewMode: (previewViewMode: PreviewViewMode) => void; | ||
| onStopPreview: () => void; | ||
| isStopEnabled: boolean; | ||
| fetchOptions: (value: string) => Promise<string[]>; |
There was a problem hiding this comment.
nit: this variable name isn't super clear to me. Can we name it something to do with file search or something?
bethanyaconnor
left a comment
There was a problem hiding this comment.
Glad you could so easily reuse this component!
|
|
||
| import styles from './AutocompleteInput.module.scss'; | ||
|
|
||
| const FETCH_SUGGESTIONS_MIN_LENGTH = 3; |
There was a problem hiding this comment.
Curious how this value was set? And does it change the behavior of current use of AutocompleteInput in PD workshop forms where sessions are used?
There was a problem hiding this comment.
Oh - I see discussion above! Still curious about 2nd part of question.
There was a problem hiding this comment.
Yes, there's a video linked in the discussion above showing the behavior change in the PD form (it only has one use).
There was a problem hiding this comment.
Gotcha - appreciate the check-in with teacher tools team!
fisher-alice
left a comment
There was a problem hiding this comment.
Just left a couple questions/suggestions. Nice work!
| if (key === 'Enter' && onEnter && activeIndex < 0) { | ||
| e.preventDefault(); | ||
| onEnter(value); | ||
| reset(); |
There was a problem hiding this comment.
I'm noticing that if I don't select an option from dropdown and just press enter, the dropdown options are still displayed. This seems to happen when I press 'Enter' immediately after typing.
Screen.Recording.2026-02-04.at.10.04.59.AM.mov
Maybe add a call to blur to close dropdown and remove focus on enter?
There was a problem hiding this comment.
Ooh nice catch. We already close the dropdown / remove focus on enter, but it looks like this happens only if you hit enter quickly after finishing typing because there's some logic that debounces refetching the options every 300ms, and the scheduled refetch happens after you hit enter and reopens the dropdown.
I just added a commit that gates showing options on the input being focused -- does that seem reasonable to you? ie, even if we refetch options, we won't show them because of the new gate.
I also took a look at the existing use of this component, and this problem exists there too (and is fixed with this change). So seems like an improvement overall!
There was a problem hiding this comment.
NIce! thanks for the update!
There was a problem hiding this comment.
oof nvm seeing a problem with that commit, working on an update...
…l onSubmit on click on option and fix
Replaces the free text input in the Web Lab 2 preview panel with one that shows an autocompletes on user input. It re-uses and modifies an existing professional development-focused
AutocompleteInputthat is very generic. I moved it over to thetemplatesdirectory (not exactly sure what the right place is, but felt like it shouldn't be in PD if shared).A couple tricky cases:
weblab2.autocomplete.mov
Links
Testing story
Tested manually with Web Lab 2 (see video above).
I also tested the one use case I could see of the existing component (used in the location picker at /pd/workshop_dashboard/workshops/new/build_your_own_workshop) and saw no changes in behavior.
Follow-up work
We might want to tweak the styling here a bit (Moshe and I discussed a look where we display both the file name and path in the dropdown in the Slack thread linked above) and/or adjust behavior around whether to allow users to enter file paths that aren't valid, but thought I'd try and get the core functionality merged first.