diff --git a/src/components/Portal/Portal.tsx b/src/components/Portal/Portal.tsx index e6da309..329f72d 100644 --- a/src/components/Portal/Portal.tsx +++ b/src/components/Portal/Portal.tsx @@ -7,13 +7,14 @@ import { composeEventHandlers } from '@/libs/event'; import { composeRefs } from '@/libs/ref'; import { PositionType } from '@/types/position'; -interface ModalContentProps extends React.ComponentProps<'div'> { +export interface PortalProps extends React.ComponentProps<'div'> { ref: React.ForwardedRef; width?: React.CSSProperties['width']; disabledBG?: boolean; + disabledAutoFocus?: boolean; } -const Portal = ({ children, ref, width, onKeyDown, ...props }: ModalContentProps) => { +const Portal = ({ children, ref, width, onKeyDown, disabledAutoFocus, ...props }: PortalProps) => { const portalRef = React.useRef(null); const { showModal, toggleElement, reorgPos } = usePortal({ portalRef }); const [items, setItems] = useState([]); @@ -65,18 +66,21 @@ const Portal = ({ children, ref, width, onKeyDown, ...props }: ModalContentProps const handleOpen = React.useCallback(() => { if (portalRef.current) { - // portalRef.current.focus(); - setItems( - Array.from(portalRef.current.querySelectorAll('[data-focus-enabled="true"]')).filter(e => { - return e.getAttribute('disabled') == null; - }), - ); + if (!disabledAutoFocus) portalRef.current.focus(); + + const newItems = Array.from( + portalRef.current.querySelectorAll('[data-focus-enabled="true"]'), + ).filter(e => { + return e.getAttribute('disabled') == null; + }); + console.log(newItems); + setItems(newItems); } - }, []); + }, [disabledAutoFocus]); React.useEffect(() => { if (showModal && portalRef.current) handleOpen(); - }, [handleOpen, showModal]); + }, [handleOpen, showModal, children]); return ( { - ref: React.ForwardedRef; - width?: React.CSSProperties['width']; - disabledBG?: boolean; -} - -export const PortalContent = ({ children, ref, width, disabledBG = false, onKeyDown, ...props }: ModalContentProps) => { +export const PortalContent = ({ + children, + ref, + width, + disabledBG = false, + disabledAutoFocus = false, + onKeyDown, + ...props +}: PortalProps) => { const { showModal, setShowModal } = useContext(PortalContext)!; const close = () => { @@ -26,7 +27,7 @@ export const PortalContent = ({ children, ref, width, disabledBG = false, onKeyD createPortal( <> {!disabledBG && } - + {children} , diff --git a/src/components/Search/SearchContent.tsx b/src/components/Search/SearchContent.tsx index 38a5e28..4abb889 100644 --- a/src/components/Search/SearchContent.tsx +++ b/src/components/Search/SearchContent.tsx @@ -12,7 +12,7 @@ interface ModalProps extends React.ComponentPropsWithoutRef<'div'> { */ export const SearchContent = React.forwardRef(({ width, children, ...props }, ref) => { return ( - + {children} ); diff --git a/src/stories/common/Search.stories.tsx b/src/stories/common/Search.stories.tsx index 57a5bfd..f86f464 100644 --- a/src/stories/common/Search.stories.tsx +++ b/src/stories/common/Search.stories.tsx @@ -31,6 +31,24 @@ export default meta; type Story = StoryObj; +const animals = [ + 'dog', + 'cat', + 'elephant', + 'lion', + 'tiger', + 'bear', + 'giraffe', + 'zebra', + 'monkey', + 'snake', + 'rabbit', + 'horse', + 'deer', + 'fox', + 'wolf', +]; + export const Default: Story = { args: {}, decorators: [ @@ -55,27 +73,50 @@ export const Default: Story = { ], }; +const TempSearch = () => { + const [text, setText] = React.useState(''); + + return ( + <> + + Search Input / + + + { + setText(e.target.value); + }} + /> + + Social + + GitHub + ⌘+T + + Facebook + + User + @bandmators + @bmates + {animals + .filter(animal => animal.includes(text)) + .slice(0, 5) + .map(item => ( + {item} + ))} + + + + ); +}; + export const Toggle: Story = { args: {}, decorators: [ () => { return ( - - Search Input / - - - - - Social - GitHub - Facebook - - User - @bandmators - @bmates - - + ); }, @@ -93,28 +134,7 @@ export const Space: Story = { args: { align: 'start', space: 16, - children: ( - <> - - Search Input / - - - - - Social - - GitHub - ⌘+T - - Facebook - - User - @bandmators - @bmates - - - - ), + children: , }, }; @@ -128,28 +148,7 @@ export const AlignStart: Story = { }, args: { align: 'start', - children: ( - <> - - Search Input / - - - - - Social - - GitHub - ⌘+T - - Facebook - - User - @bandmators - @bmates - - - - ), + children: , }, }; @@ -163,28 +162,7 @@ export const AlignCenter: Story = { }, args: { align: 'center', - children: ( - <> - - Search Input / - - - - - Social - - GitHub - ⌘+T - - Facebook - - User - @bandmators - @bmates - - - - ), + children: , }, }; @@ -198,27 +176,6 @@ export const AlignEnd: Story = { }, args: { align: 'end', - children: ( - <> - - Search Input / - - - - - Social - - GitHub - ⌘+T - - Facebook - - User - @bandmators - @bmates - - - - ), + children: , }, };