Skip to content

Commit

Permalink
Improve <NamespaceSelectFilter>'s UX (#3490)
Browse files Browse the repository at this point in the history
- Display previously selected namespaces at the top of the option list
  when first opened

- Don't resort after every selection

- Automatically close the option list if no longer in multi-select mode
  and have toggled at least one namespace

Signed-off-by: Sebastian Malton <[email protected]>
  • Loading branch information
Nokel81 authored Jul 27, 2021
1 parent 91b3bd1 commit 51b53b3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 22 deletions.
95 changes: 76 additions & 19 deletions src/renderer/components/+namespaces/namespace-select-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
import "./namespace-select-filter.scss";

import React from "react";
import { observer } from "mobx-react";
import { disposeOnUnmount, observer } from "mobx-react";
import { components, PlaceholderProps } from "react-select";
import { action, computed, makeObservable, observable, reaction } from "mobx";

import { Icon } from "../icon";
import { NamespaceSelect } from "./namespace-select";
import { namespaceStore } from "./namespace.store";

import type { SelectOption, SelectProps } from "../select";
import { isLinux, isMac, isWindows } from "../../../common/vars";
import { observable } from "mobx";
import { isMac } from "../../../common/vars";

const Placeholder = observer((props: PlaceholderProps<any, boolean>) => {
const getPlaceholder = (): React.ReactNode => {
Expand Down Expand Up @@ -60,6 +60,41 @@ export class NamespaceSelectFilter extends React.Component<SelectProps> {
static isMultiSelection = observable.box(false);
static isMenuOpen = observable.box(false);

private selected = observable.set<string>();
private didToggle = false;

constructor(props: SelectProps) {
super(props);
makeObservable(this);
}

@computed get isMultiSelection() {
return NamespaceSelectFilter.isMultiSelection.get();
}

set isMultiSelection(val: boolean) {
NamespaceSelectFilter.isMultiSelection.set(val);
}

@computed get isMenuOpen() {
return NamespaceSelectFilter.isMenuOpen.get();
}

set isMenuOpen(val: boolean) {
NamespaceSelectFilter.isMenuOpen.set(val);
}

componentDidMount() {
disposeOnUnmount(this, [
reaction(() => this.isMenuOpen, newVal => {
if (newVal) { // rising edge of selection
this.selected.replace(namespaceStore.selectedNamespaces);
this.didToggle = false;
}
}),
]);
}

formatOptionLabel({ value: namespace, label }: SelectOption) {
if (namespace) {
const isSelected = namespaceStore.hasContext(namespace);
Expand All @@ -76,45 +111,66 @@ export class NamespaceSelectFilter extends React.Component<SelectProps> {
return label;
}

onChange([{ value: namespace }]: SelectOption[]) {
if (NamespaceSelectFilter.isMultiSelection.get() && namespace) {
namespaceStore.toggleContext(namespace);
} else if (!NamespaceSelectFilter.isMultiSelection.get() && namespace) {
namespaceStore.toggleSingle(namespace);
@action
onChange = ([{ value: namespace }]: SelectOption[]) => {
if (namespace) {
if (this.isMultiSelection) {
this.didToggle = true;
namespaceStore.toggleContext(namespace);
} else {
namespaceStore.toggleSingle(namespace);
}
} else {
namespaceStore.toggleAll(true); // "All namespaces" clicked
}
};

private isSelectionKey(e: React.KeyboardEvent): boolean {
if (isMac) {
return e.key === "Meta";
}

return e.key === "Control"; // windows or linux
}

onKeyDown = (e: React.KeyboardEvent<any>) => {
if (isMac && e.metaKey || (isWindows || isLinux) && e.ctrlKey) {
NamespaceSelectFilter.isMultiSelection.set(true);
@action
onKeyDown = (e: React.KeyboardEvent) => {
if (this.isSelectionKey(e)) {
this.isMultiSelection = true;
}
};

onKeyUp = (e: React.KeyboardEvent<any>) => {
if (isMac && e.key === "Meta" || (isWindows || isLinux) && e.key === "Control") {
NamespaceSelectFilter.isMultiSelection.set(false);
@action
onKeyUp = (e: React.KeyboardEvent) => {
if (this.isSelectionKey(e)) {
this.isMultiSelection = false;
}

if (!this.isMultiSelection && this.didToggle) {
this.isMenuOpen = false;
}
};

@action
onClick = () => {
if (!NamespaceSelectFilter.isMultiSelection.get()) {
NamespaceSelectFilter.isMenuOpen.set(!NamespaceSelectFilter.isMenuOpen.get());
if (!this.isMenuOpen) {
this.isMenuOpen = true;
} else if (!this.isMultiSelection) {
this.isMenuOpen = !this.isMenuOpen;
}
};

reset = () => {
NamespaceSelectFilter.isMultiSelection.set(false);
NamespaceSelectFilter.isMenuOpen.set(false);
this.isMultiSelection = false;
this.isMenuOpen = false;
};

render() {
return (
<div onKeyUp={this.onKeyUp} onKeyDown={this.onKeyDown} onClick={this.onClick}>
<NamespaceSelect
isMulti={true}
menuIsOpen={NamespaceSelectFilter.isMenuOpen.get()}
menuIsOpen={this.isMenuOpen}
components={{ Placeholder }}
showAllNamespacesOption={true}
closeMenuOnSelect={false}
Expand All @@ -124,6 +180,7 @@ export class NamespaceSelectFilter extends React.Component<SelectProps> {
onBlur={this.reset}
formatOptionLabel={this.formatOptionLabel}
className="NamespaceSelectFilter"
sort={(left, right) => +this.selected.has(right.value) - +this.selected.has(left.value)}
/>
</div>
);
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/components/+namespaces/namespace-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { kubeWatchApi } from "../../api/kube-watch-api";

interface Props extends SelectProps {
showIcons?: boolean;
sort?: (a: SelectOption<string>, b: SelectOption<string>) => number;
showAllNamespacesOption?: boolean; // show "All namespaces" option on the top (default: false)
customizeOptions?(options: SelectOption[]): SelectOption[];
}
Expand Down Expand Up @@ -59,9 +60,13 @@ export class NamespaceSelect extends React.Component<Props> {
}

@computed.struct get options(): SelectOption[] {
const { customizeOptions, showAllNamespacesOption } = this.props;
const { customizeOptions, showAllNamespacesOption, sort } = this.props;
let options: SelectOption[] = namespaceStore.items.map(ns => ({ value: ns.getName() }));

if (sort) {
options.sort(sort);
}

if (showAllNamespacesOption) {
options.unshift({ label: "All Namespaces", value: "" });
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export class Input extends React.Component<InputProps, State> {
}

getValue(): string {
const { trim, value, defaultValue = "" } = this.props;
const rawValue = value ?? this.input?.value ?? defaultValue;
const { trim, value, defaultValue } = this.props;
const rawValue = value ?? this.input?.value ?? defaultValue ?? "";

return trim ? rawValue.trim() : rawValue;
}
Expand Down

0 comments on commit 51b53b3

Please sign in to comment.