Skip to content
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

Pre-select autocomplete file when passing file path #19108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/lib/cockpit-components-file-autocomplete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class FileAutoComplete extends React.Component {
isOpen: false,
value: this.props.value || null,
};
this.updateFiles(props.value || '/');

this.typeaheadInputValue = "";
this.allowFilesUpdate = true;
this.updateFiles = this.updateFiles.bind(this);
Expand All @@ -43,7 +43,7 @@ export class FileAutoComplete extends React.Component {
this.clearSelection = this.clearSelection.bind(this);
this.onCreateOption = this.onCreateOption.bind(this);

this.debouncedChange = debounce(300, (value) => {
this.onPathChange = (value) => {
if (!value) {
this.clearSelection();
return;
Expand Down Expand Up @@ -80,7 +80,9 @@ export class FileAutoComplete extends React.Component {
return this.updateFiles(parentDir + '/');
}
}
});
};
this.debouncedChange = debounce(300, this.onPathChange);
this.onPathChange(this.state.value);
}

componentWillUnmount() {
Expand Down
5 changes: 5 additions & 0 deletions pkg/playground/react-demo-file-autocomplete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ export function showFileAcDemo(rootElement) {
const root = createRoot(rootElement);
root.render(<FileAutoComplete id='file-autocomplete-widget' />);
}

export function showFileAcDemoPreselected(rootElement) {
const root = createRoot(rootElement);
root.render(<FileAutoComplete id='file-autocomplete-widget-preselected' value="/home/admin/newdir/file1" />);
}
1 change: 1 addition & 0 deletions pkg/playground/react-patterns.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<section class="pf-v5-c-page__main-section pf-m-light">
<h3>Select file</h3>
<div id="demo-file-ac"></div>
<div id="demo-file-ac-preselected"></div>
</section>

<section class="pf-v5-c-page__main-section pf-m-light">
Expand Down
3 changes: 2 additions & 1 deletion pkg/playground/react-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { show_modal_dialog } from "cockpit-components-dialog.jsx";

import { PatternDialogBody } from "./react-demo-dialog.jsx";
import { showCardsDemo } from "./react-demo-cards.jsx";
import { showFileAcDemo } from "./react-demo-file-autocomplete.jsx";
import { showFileAcDemo, showFileAcDemoPreselected } from "./react-demo-file-autocomplete.jsx";

/* -----------------------------------------------------------------------------
Modal Dialog
Expand Down Expand Up @@ -122,6 +122,7 @@ document.addEventListener("DOMContentLoaded", function() {
*/
// File autocomplete
showFileAcDemo(document.getElementById('demo-file-ac'));
showFileAcDemoPreselected(document.getElementById('demo-file-ac-preselected'));

// Cards
showCardsDemo(document.getElementById('demo-cards'));
Expand Down
17 changes: 17 additions & 0 deletions test/verify/check-pages
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ OnCalendar=daily
b = self.browser
m = self.machine

self.restore_dir('/home/admin')

stuff = os.path.join(self.vm_tmpdir, "stuff")
# prepare a directory for testing file autocomplete widget
m.execute(f"mkdir -p {stuff}/dir")
Expand Down Expand Up @@ -633,6 +635,21 @@ OnCalendar=daily
b.key_press("stuff/")
b.wait_in_text("#file-autocomplete-widget li:nth-of-type(5) button", "other")

# Create test folder with known files
m.execute("mkdir /home/admin/newdir")
m.execute("mkdir /home/admin/newdir/dir1")
m.execute("mkdir /home/admin/newdir/dir2")
m.execute("touch /home/admin/newdir/file1")
m.execute("touch /home/admin/newdir/file2")
# test pre-selected autocomplete widget
b.wait_val("div#demo-file-ac-preselected input", "/home/admin/newdir/file1")
b.click("div#demo-file-ac-preselected input")
b.click("div#demo-file-ac input")
b.click("div#demo-file-ac-preselected input")
paths = ["/home/admin/newdir", "/home/admin/newdir/dir1", "/home/admin/newdir/dir2", "/home/admin/newdir/file1", "/home/admin/newdir/file2"]
for i in range(5):
b.wait_in_text(f"#file-autocomplete-widget-preselected li:nth-of-type({i+1}) button", paths[i])

@testlib.skipOstree("No PCP available")
def testPlots(self):
b = self.browser
Expand Down