mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Keep filter when changing view
Signed-off-by: vshakirova <vshakirova@mirantis.com>
This commit is contained in:
parent
33c405bdcf
commit
51e1baf6f2
@ -1,10 +1,11 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import debounce from "lodash/debounce";
|
import debounce from "lodash/debounce";
|
||||||
import { autorun, observable } from "mobx";
|
import { observable, reaction } from "mobx";
|
||||||
import { disposeOnUnmount, observer } from "mobx-react";
|
import { disposeOnUnmount, observer } from "mobx-react";
|
||||||
import { InputProps } from "./input";
|
import { InputProps } from "./input";
|
||||||
import { SearchInput } from "./search-input";
|
import { SearchInput } from "./search-input";
|
||||||
import { createPageParam } from "../../navigation";
|
import { createPageParam } from "../../navigation";
|
||||||
|
import { getSearchInput, setSearchInput } from "./search-input.storage";
|
||||||
|
|
||||||
export const searchUrlParam = createPageParam({
|
export const searchUrlParam = createPageParam({
|
||||||
name: "search",
|
name: "search",
|
||||||
@ -13,6 +14,7 @@ export const searchUrlParam = createPageParam({
|
|||||||
});
|
});
|
||||||
|
|
||||||
interface Props extends InputProps {
|
interface Props extends InputProps {
|
||||||
|
isEnabled?: boolean;
|
||||||
compact?: boolean; // show only search-icon when not focused
|
compact?: boolean; // show only search-icon when not focused
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,13 +22,30 @@ interface Props extends InputProps {
|
|||||||
export class SearchInputUrl extends React.Component<Props> {
|
export class SearchInputUrl extends React.Component<Props> {
|
||||||
@observable inputVal = ""; // fix: use empty string on init to avoid react warnings
|
@observable inputVal = ""; // fix: use empty string on init to avoid react warnings
|
||||||
|
|
||||||
@disposeOnUnmount
|
componentDidMount() {
|
||||||
updateInput = autorun(() => this.inputVal = searchUrlParam.get());
|
if (getSearchInput()) {
|
||||||
|
this.inputVal = getSearchInput();
|
||||||
|
searchUrlParam.set(getSearchInput());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.inputVal = searchUrlParam.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
disposeOnUnmount(this, [
|
||||||
|
reaction(() => this.props.isEnabled, () => {
|
||||||
|
if (!this.props.isEnabled) {
|
||||||
|
this.clear();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
updateUrl = debounce((val: string) => searchUrlParam.set(val), 250);
|
updateUrl = debounce((val: string) => searchUrlParam.set(val), 250);
|
||||||
|
|
||||||
setValue = (value: string) => {
|
setValue = (value: string) => {
|
||||||
this.inputVal = value;
|
this.inputVal = value;
|
||||||
this.updateUrl(value);
|
this.updateUrl(value);
|
||||||
|
setSearchInput(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
clear = () => {
|
clear = () => {
|
||||||
@ -43,14 +62,14 @@ export class SearchInputUrl extends React.Component<Props> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { inputVal } = this;
|
const { isEnabled, ...props } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SearchInput
|
<SearchInput
|
||||||
value={inputVal}
|
value={this.inputVal}
|
||||||
onChange={this.onChange}
|
onChange={this.onChange}
|
||||||
onClear={this.clear}
|
onClear={this.clear}
|
||||||
{...this.props}
|
{...props}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/renderer/components/input/search-input.storage.ts
Normal file
19
src/renderer/components/input/search-input.storage.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { createStorage } from "../../utils";
|
||||||
|
|
||||||
|
export interface SearchInputState {
|
||||||
|
searchInput: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const searchInputStorage = createStorage<SearchInputState>("search_input", {
|
||||||
|
searchInput: ""
|
||||||
|
});
|
||||||
|
|
||||||
|
export function setSearchInput(searchInput: string) {
|
||||||
|
searchInputStorage.merge(draft => {
|
||||||
|
draft.searchInput = searchInput;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSearchInput() {
|
||||||
|
return searchInputStorage.get().searchInput;
|
||||||
|
}
|
||||||
@ -344,6 +344,7 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
|
|||||||
|
|
||||||
renderHeader() {
|
renderHeader() {
|
||||||
const { showHeader, customizeHeader, renderHeaderTitle, headerClassName, isClusterScoped } = this.props;
|
const { showHeader, customizeHeader, renderHeaderTitle, headerClassName, isClusterScoped } = this.props;
|
||||||
|
const isSearchEnabled = Boolean(this.filters.find(filter => filter.type === FilterType.SEARCH));
|
||||||
|
|
||||||
if (!showHeader) return;
|
if (!showHeader) return;
|
||||||
const title = typeof renderHeaderTitle === "function" ? renderHeaderTitle(this) : renderHeaderTitle;
|
const title = typeof renderHeaderTitle === "function" ? renderHeaderTitle(this) : renderHeaderTitle;
|
||||||
@ -356,7 +357,7 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
|
|||||||
[FilterType.NAMESPACE]: true, // namespace-select used instead
|
[FilterType.NAMESPACE]: true, // namespace-select used instead
|
||||||
}}/>
|
}}/>
|
||||||
</>,
|
</>,
|
||||||
search: <SearchInputUrl/>,
|
search: <SearchInputUrl isEnabled={isSearchEnabled}/>,
|
||||||
};
|
};
|
||||||
let header = this.renderHeaderContent(placeholders);
|
let header = this.renderHeaderContent(placeholders);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user