@@ -9,7 +9,7 @@ import MUITreeItem, {
99import TreeView , { TreeViewProps } from "@mui/lab/TreeView" ;
1010import {
1111 Theme ,
12- Menu ,
12+ Popover ,
1313 PopoverActions ,
1414 useEventCallback ,
1515 OutlinedInput ,
@@ -22,7 +22,7 @@ import {
2222import { makeStyles } from "@mui/styles" ;
2323import useId from "@mui/utils/useId" ;
2424import clsx from "clsx" ;
25- import { useCallback , useMemo , useState } from "react" ;
25+ import { useCallback , useMemo , useRef , useState } from "react" ;
2626import * as React from "react" ;
2727import { useEffect } from "react" ;
2828
@@ -338,7 +338,7 @@ function SelectTree({
338338 return res ;
339339 } , [ options ] ) ;
340340
341- const handleClick = useEventCallback ( ( ev : React . MouseEvent < HTMLElement > ) => {
341+ const handleOpen = useEventCallback ( ( ev : React . MouseEvent < HTMLElement > ) => {
342342 setOpenState ( true ) ;
343343 setMinMenuWidth ( ev . currentTarget . clientWidth ) ;
344344 onOpen ?.( ) ;
@@ -371,7 +371,10 @@ function SelectTree({
371371 } ) ;
372372
373373 const handleClickResetInput = useEvent ( ( ) => {
374- setInputValue ( "" ) ;
374+ const newValue = "" ;
375+ setInputValue ( newValue ) ;
376+ setFilteredOptions ( getFilteredOptions ( options , newValue ) ) ;
377+ setExpanded ( defaultExpanded ) ;
375378 } ) ;
376379
377380 const handleNodeToggle : TreeViewProps [ "onNodeToggle" ] = useEvent (
@@ -456,6 +459,14 @@ function SelectTree({
456459 [ ]
457460 ) ;
458461
462+ const treeRef = useRef ( ) ;
463+ const handleKeyDown : React . HTMLAttributes < HTMLInputElement > [ "onKeyDown" ] =
464+ useEvent ( ( ev ) => {
465+ if ( ev . key === "Enter" || ev . key == " " ) {
466+ handleOpen ( ev ) ;
467+ }
468+ } ) ;
469+
459470 useEffect ( ( ) => {
460471 const inputNode = inputRef . current ;
461472 if ( inputNode ) {
@@ -478,10 +489,11 @@ function SelectTree({
478489 ref = { inputRef }
479490 size = "small"
480491 className = { classes . input }
481- onClick = { disabled ? undefined : handleClick }
492+ onClick = { disabled ? undefined : handleOpen }
493+ onKeyDown = { handleKeyDown }
482494 endAdornment = { < Icon className = { classes . icon } name = "caretDown" /> }
483495 />
484- < Menu
496+ < Popover
485497 anchorOrigin = { {
486498 vertical : "bottom" ,
487499 horizontal : "left" ,
@@ -492,15 +504,11 @@ function SelectTree({
492504 action = { menuRef }
493505 PaperProps = { paperProps }
494506 TransitionProps = { menuTransitionProps }
495- MenuListProps = { {
496- // @ts -ignore
497- component : "div" ,
498- } }
499507 >
500508 < TextField
501509 size = "small"
502510 value = { inputValue }
503- sx = { { px : 1 , pb : 1 , width : "100%" } }
511+ sx = { { p : 1 , width : "100%" } }
504512 InputProps = { {
505513 startAdornment : < Icon name = "search" size = { 16 } color = "#555" /> ,
506514 endAdornment : (
@@ -512,18 +520,30 @@ function SelectTree({
512520 } }
513521 onChange = { handleInputChange }
514522 />
515- < TreeView
516- defaultSelected = { value }
517- expanded = { expanded }
518- onNodeToggle = { handleNodeToggle }
519- onNodeSelect = { handleNodeSelect }
520- defaultCollapseIcon = { < ExpandMoreIcon /> }
521- defaultExpandIcon = { < ChevronRightIcon /> }
522- sx = { { flexGrow : 1 , overflowY : "auto" , pb : 2 , "user-select" : "none" } }
523- >
524- { renderTreeContent ( filteredOptions ) }
525- </ TreeView >
526- </ Menu >
523+ { filteredOptions . length === 0 ? (
524+ < Typography variant = "body2" sx = { { px : 2 , py : 4 } } >
525+ < Trans id = "No results" />
526+ </ Typography >
527+ ) : (
528+ < TreeView
529+ ref = { treeRef }
530+ defaultSelected = { value }
531+ expanded = { expanded }
532+ onNodeToggle = { handleNodeToggle }
533+ onNodeSelect = { handleNodeSelect }
534+ defaultCollapseIcon = { < ExpandMoreIcon /> }
535+ defaultExpandIcon = { < ChevronRightIcon /> }
536+ sx = { {
537+ flexGrow : 1 ,
538+ overflowY : "auto" ,
539+ pb : 2 ,
540+ "user-select" : "none" ,
541+ } }
542+ >
543+ { renderTreeContent ( filteredOptions ) }
544+ </ TreeView >
545+ ) }
546+ </ Popover >
527547 </ div >
528548 ) ;
529549}
0 commit comments