Refine photo chooser
This commit is contained in:
parent
3694fd061d
commit
bb7c393021
@ -6,8 +6,8 @@ import ImageMedium from '@/components/image/ImageMedium';
|
|||||||
import { menuSurfaceStyles } from '@/components/primitives/surface';
|
import { menuSurfaceStyles } from '@/components/primitives/surface';
|
||||||
import { GRID_SPACE_CLASSNAME } from '@/components';
|
import { GRID_SPACE_CLASSNAME } from '@/components';
|
||||||
import useDynamicPhoto from '../useDynamicPhoto';
|
import useDynamicPhoto from '../useDynamicPhoto';
|
||||||
import { IoSearch } from 'react-icons/io5';
|
import { IoClose, IoSearch } from 'react-icons/io5';
|
||||||
import { useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import usePhotoQuery from '../usePhotoQuery';
|
import usePhotoQuery from '../usePhotoQuery';
|
||||||
import Spinner from '@/components/Spinner';
|
import Spinner from '@/components/Spinner';
|
||||||
|
|
||||||
@ -26,10 +26,15 @@ export default function FieldsetPhotoChooser({
|
|||||||
photosCount?: number
|
photosCount?: number
|
||||||
photosHidden?: Photo[]
|
photosHidden?: Photo[]
|
||||||
}) {
|
}) {
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
const [showQuery, setShowQuery] = useState(true);
|
||||||
|
// TODO: Move query into hook
|
||||||
const [query, setQuery] = useState('');
|
const [query, setQuery] = useState('');
|
||||||
const {
|
const {
|
||||||
photos: photosQuery,
|
photos: photosQuery,
|
||||||
isLoading,
|
isLoading,
|
||||||
|
reset,
|
||||||
} = usePhotoQuery(query);
|
} = usePhotoQuery(query);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -40,6 +45,16 @@ export default function FieldsetPhotoChooser({
|
|||||||
photoId: value,
|
photoId: value,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showQuery) {
|
||||||
|
inputRef.current?.focus();
|
||||||
|
} else {
|
||||||
|
reset();
|
||||||
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
||||||
|
setQuery('');
|
||||||
|
}
|
||||||
|
}, [showQuery, reset]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<FieldsetWithStatus {...{ label, value, onChange, type: 'hidden' }} />
|
<FieldsetWithStatus {...{ label, value, onChange, type: 'hidden' }} />
|
||||||
@ -82,19 +97,33 @@ export default function FieldsetPhotoChooser({
|
|||||||
<div className="grow">
|
<div className="grow">
|
||||||
Choose photo
|
Choose photo
|
||||||
</div>
|
</div>
|
||||||
{isLoading ? <Spinner /> : <IoSearch size={16} />}
|
{isLoading
|
||||||
|
? <Spinner />
|
||||||
|
: showQuery
|
||||||
|
? <IoClose
|
||||||
|
size={16}
|
||||||
|
className="cursor-pointer"
|
||||||
|
onClick={() => setShowQuery(false)}
|
||||||
|
/>
|
||||||
|
: <IoSearch
|
||||||
|
size={16}
|
||||||
|
className="cursor-pointer"
|
||||||
|
onClick={() => setShowQuery(true)}
|
||||||
|
/>}
|
||||||
</div>
|
</div>
|
||||||
<FieldsetWithStatus
|
{showQuery &&
|
||||||
label="Search for a photo"
|
<input
|
||||||
|
ref={inputRef}
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Search for a photo"
|
placeholder="Search for a photo"
|
||||||
className="w-full mb-2"
|
className="block w-full m-0"
|
||||||
value={query}
|
value={query}
|
||||||
onChange={setQuery}
|
onChange={e => setQuery(e.target.value)}
|
||||||
hideLabel
|
/>}
|
||||||
/>
|
<div className={clsx(
|
||||||
<div className="grid grid-cols-3 gap-0.5">
|
'grid grid-cols-3 gap-0.5',
|
||||||
{(photosQuery.length > 0 ? photosQuery : photos).map(photo => (
|
)}>
|
||||||
|
{(showQuery && query ? photosQuery : photos).map(photo => (
|
||||||
<span
|
<span
|
||||||
key={photo.id}
|
key={photo.id}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user