mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-27 08:41:51 +01:00
Reviewed changes & simplified clearing process
This commit is contained in:
parent
32c6bf12c7
commit
6cf870d246
@ -1,5 +1,5 @@
|
|||||||
import c from "classnames";
|
import c from "classnames";
|
||||||
import { forwardRef, useEffect, useRef, useState } from "react";
|
import { forwardRef, useState } from "react";
|
||||||
|
|
||||||
import { Flare } from "@/components/utils/Flare";
|
import { Flare } from "@/components/utils/Flare";
|
||||||
|
|
||||||
@ -9,38 +9,18 @@ import { TextInputControl } from "../text-inputs/TextInputControl";
|
|||||||
export interface SearchBarProps {
|
export interface SearchBarProps {
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
onChange: (value: string, force: boolean) => void;
|
onChange: (value: string, force: boolean) => void;
|
||||||
onUnFocus: () => void;
|
onUnFocus: (newSearch?: string) => void;
|
||||||
value: string;
|
value: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SearchBarInput = forwardRef<HTMLInputElement, SearchBarProps>(
|
export const SearchBarInput = forwardRef<HTMLInputElement, SearchBarProps>(
|
||||||
(props, ref) => {
|
(props, ref) => {
|
||||||
const [canClear, setCanClear] = useState(false);
|
|
||||||
const [focused, setFocused] = useState(false);
|
const [focused, setFocused] = useState(false);
|
||||||
const clearButtonRef = useRef<HTMLDivElement>(null);
|
|
||||||
|
|
||||||
function setSearch(value: string) {
|
function setSearch(value: string) {
|
||||||
props.onChange(value, false);
|
props.onChange(value, false);
|
||||||
setCanClear(value.length > 0);
|
|
||||||
clearButtonRef.current!.style.opacity = canClear ? "1" : "0";
|
|
||||||
clearButtonRef.current!.style.cursor = canClear ? "pointer" : "auto";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function refocusSearch() {
|
|
||||||
if (ref && typeof ref !== "function") {
|
|
||||||
ref.current?.blur();
|
|
||||||
setTimeout(() => ref.current?.focus(), 10);
|
|
||||||
setTimeout(() => ref.current?.blur(), 20);
|
|
||||||
setTimeout(() => ref.current?.focus(), 30);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (ref && typeof ref !== "function") {
|
|
||||||
setSearch(ref.current?.value || "");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Flare.Base
|
<Flare.Base
|
||||||
className={c({
|
className={c({
|
||||||
@ -78,21 +58,22 @@ export const SearchBarInput = forwardRef<HTMLInputElement, SearchBarProps>(
|
|||||||
placeholder={props.placeholder}
|
placeholder={props.placeholder}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div
|
{props.value.length > 0 && (
|
||||||
ref={clearButtonRef}
|
<div
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (canClear) {
|
props.onUnFocus("");
|
||||||
setSearch("");
|
if (ref && typeof ref !== "function") {
|
||||||
setTimeout(() => refocusSearch(), 10);
|
ref.current?.focus();
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
className="group/clear cursor-pointer absolute p-1 bottom-0 right-2 top-0 flex justify-center my-auto max-h-10 max-w-10 h-full w-full items-center hover:bg-search-hoverBackground active:scale-110 text-search-icon rounded-full opacity-0 transition duration-100"
|
className="cursor-pointer absolute p-1 bottom-0 right-2 top-0 flex justify-center my-auto max-h-10 max-w-10 h-full w-full items-center hover:bg-search-hoverBackground active:scale-110 text-search-icon rounded-full transition duration-100"
|
||||||
>
|
>
|
||||||
<Icon
|
<Icon
|
||||||
icon={Icons.X}
|
icon={Icons.X}
|
||||||
className="group-hover/clear:text-white transition duration-100"
|
className="hover:text-white transition duration-100"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</Flare.Child>
|
</Flare.Child>
|
||||||
</Flare.Base>
|
</Flare.Base>
|
||||||
);
|
);
|
||||||
|
@ -33,8 +33,10 @@ export function useSearchQuery(): [
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onUnFocus = () => {
|
const onUnFocus = (newSearch?: string) => {
|
||||||
updateParams(search, true);
|
// eslint-disable-next-line eqeqeq
|
||||||
|
const updated = newSearch == undefined ? search : newSearch;
|
||||||
|
updateParams(updated, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
return [search, updateParams, onUnFocus];
|
return [search, updateParams, onUnFocus];
|
||||||
|
@ -111,6 +111,7 @@ export default createTheme({
|
|||||||
|
|
||||||
search: {
|
search: {
|
||||||
background: tokens.shade.c500,
|
background: tokens.shade.c500,
|
||||||
|
hoverBackground: tokens.shade.c600,
|
||||||
focused: tokens.shade.c400,
|
focused: tokens.shade.c400,
|
||||||
placeholder: tokens.shade.c100,
|
placeholder: tokens.shade.c100,
|
||||||
icon: tokens.shade.c100
|
icon: tokens.shade.c100
|
||||||
|
@ -111,6 +111,7 @@ export default createTheme({
|
|||||||
|
|
||||||
search: {
|
search: {
|
||||||
background: tokens.shade.c500,
|
background: tokens.shade.c500,
|
||||||
|
hoverBackground: tokens.shade.c600,
|
||||||
focused: tokens.shade.c400,
|
focused: tokens.shade.c400,
|
||||||
placeholder: tokens.shade.c100,
|
placeholder: tokens.shade.c100,
|
||||||
icon: tokens.shade.c100
|
icon: tokens.shade.c100
|
||||||
|
@ -111,6 +111,7 @@ export default createTheme({
|
|||||||
|
|
||||||
search: {
|
search: {
|
||||||
background: tokens.shade.c500,
|
background: tokens.shade.c500,
|
||||||
|
hoverBackground: tokens.shade.c600,
|
||||||
focused: tokens.shade.c400,
|
focused: tokens.shade.c400,
|
||||||
placeholder: tokens.shade.c100,
|
placeholder: tokens.shade.c100,
|
||||||
icon: tokens.shade.c100
|
icon: tokens.shade.c100
|
||||||
|
@ -111,6 +111,7 @@ export default createTheme({
|
|||||||
|
|
||||||
search: {
|
search: {
|
||||||
background: tokens.shade.c500,
|
background: tokens.shade.c500,
|
||||||
|
hoverBackground: tokens.shade.c600,
|
||||||
focused: tokens.shade.c400,
|
focused: tokens.shade.c400,
|
||||||
placeholder: tokens.shade.c100,
|
placeholder: tokens.shade.c100,
|
||||||
icon: tokens.shade.c100
|
icon: tokens.shade.c100
|
||||||
|
Loading…
Reference in New Issue
Block a user