@@ -93,6 +93,7 @@ export default function useAutocomplete(props) {
9393 }
9494 } ) ;
9595
96+ const firstFocus = React . useRef ( true ) ;
9697 const inputRef = React . useRef ( null ) ;
9798 const listboxRef = React . useRef ( null ) ;
9899 const [ anchorEl , setAnchorEl ] = React . useState ( null ) ;
@@ -531,6 +532,14 @@ export default function useAutocomplete(props) {
531532 // We don't want to validate the form.
532533 event . preventDefault ( ) ;
533534 selectNewValue ( event , filteredOptions [ highlightedIndexRef . current ] ) ;
535+
536+ // Move the selection to the end.
537+ if ( autoComplete ) {
538+ inputRef . current . setSelectionRange (
539+ inputRef . current . value . length ,
540+ inputRef . current . value . length ,
541+ ) ;
542+ }
534543 } else if ( freeSolo && inputValue !== '' ) {
535544 selectNewValue ( event , inputValue ) ;
536545 }
@@ -572,6 +581,7 @@ export default function useAutocomplete(props) {
572581
573582 const handleBlur = event => {
574583 setFocused ( false ) ;
584+ firstFocus . current = true ;
575585
576586 if ( debug && inputValue !== '' ) {
577587 return ;
@@ -640,6 +650,30 @@ export default function useAutocomplete(props) {
640650 }
641651 } ;
642652
653+ const handleMouseDown = event => {
654+ if ( event . target . nodeName !== 'INPUT' ) {
655+ // Prevent blur
656+ event . preventDefault ( ) ;
657+ }
658+ } ;
659+
660+ const handleClick = ( ) => {
661+ if (
662+ firstFocus . current &&
663+ inputRef . current . selectionEnd - inputRef . current . selectionStart === 0
664+ ) {
665+ inputRef . current . select ( ) ;
666+ }
667+
668+ firstFocus . current = false ;
669+ } ;
670+
671+ const handleInputMouseDown = ( ) => {
672+ if ( inputValue === '' ) {
673+ handlePopupIndicator ( ) ;
674+ }
675+ } ;
676+
643677 let dirty = freeSolo && inputValue . length > 0 ;
644678 dirty = dirty || ( multiple ? value . length > 0 : value !== null ) ;
645679
@@ -663,11 +697,13 @@ export default function useAutocomplete(props) {
663697 }
664698
665699 return {
666- getComboboxProps : ( ) => ( {
700+ getRootProps : ( ) => ( {
667701 'aria-owns' : popupOpen ? `${ id } -popup` : null ,
668702 role : 'combobox' ,
669703 'aria-expanded' : popupOpen ,
670704 onKeyDown : handleKeyDown ,
705+ onMouseDown : handleMouseDown ,
706+ onClick : handleClick ,
671707 } ) ,
672708 getInputLabelProps : ( ) => ( {
673709 id : `${ id } -label` ,
@@ -678,6 +714,7 @@ export default function useAutocomplete(props) {
678714 onBlur : handleBlur ,
679715 onFocus : handleFocus ,
680716 onChange : handleInputChange ,
717+ onMouseDown : handleInputMouseDown ,
681718 // if open then this is handled imperativeley so don't let react override
682719 // only have an opinion about this when closed
683720 'aria-activedescendant' : popupOpen ? undefined : null ,
@@ -693,16 +730,10 @@ export default function useAutocomplete(props) {
693730 getClearProps : ( ) => ( {
694731 tabIndex : - 1 ,
695732 onClick : handleClear ,
696- onMouseDown : event => {
697- event . preventDefault ( ) ;
698- } ,
699733 } ) ,
700734 getPopupIndicatorProps : ( ) => ( {
701735 tabIndex : - 1 ,
702736 onClick : handlePopupIndicator ,
703- onMouseDown : event => {
704- event . preventDefault ( ) ;
705- } ,
706737 } ) ,
707738 getTagProps : ( { index } ) => ( {
708739 key : index ,
@@ -716,6 +747,7 @@ export default function useAutocomplete(props) {
716747 'aria-labelledby' : `${ id } -label` ,
717748 ref : handleListboxRef ,
718749 onMouseDown : event => {
750+ // Prevent blur
719751 event . preventDefault ( ) ;
720752 } ,
721753 } ) ,
0 commit comments