Skip to content

Commit

Permalink
[useAutocomplete] Add selectOnFocus prop (#19281)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bebersohl authored and oliviertassinari committed Jan 18, 2020
1 parent 18dc9f7 commit d165216
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/pages/api/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ You can learn more about the difference by [reading this guide](/guides/minimizi
| <span class="prop-name required">renderInput&nbsp;*</span> | <span class="prop-type">func</span> | | Render the input.<br><br>**Signature:**<br>`function(params: object) => ReactNode`<br>*params:* null |
| <span class="prop-name">renderOption</span> | <span class="prop-type">func</span> | | Render the option, use `getOptionLabel` by default.<br><br>**Signature:**<br>`function(option: T, state: object) => ReactNode`<br>*option:* The option to render.<br>*state:* The state of the component. |
| <span class="prop-name">renderTags</span> | <span class="prop-type">func</span> | | Render the selected value.<br><br>**Signature:**<br>`function(value: undefined, getTagProps: function) => ReactNode`<br>*value:* The `value` provided to the component.<br>*getTagProps:* A tag props getter. |
| <span class="prop-name">selectOnFocus</span> | <span class="prop-type">bool</span> | <span class="prop-default">!props.freeSolo</span> | If `true`, the input's text will be selected on focus. |
| <span class="prop-name">size</span> | <span class="prop-type">'medium'<br>&#124;&nbsp;'small'</span> | <span class="prop-default">'medium'</span> | The size of the autocomplete. |
| <span class="prop-name">value</span> | <span class="prop-type">any<br>&#124;&nbsp;array</span> | | The value of the autocomplete.<br>The value must have reference equality with the option in order to be selected. You can customize the equality behavior with the `getOptionSelected` prop. |

Expand Down
4 changes: 3 additions & 1 deletion docs/src/pages/components/autocomplete/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ Choose one country between 248.

## Free solo

Set `freeSolo` to true so the textbox can contain any arbitrary value.
Set `freeSolo` to true so the textbox can contain any arbitrary value. The prop is designed to cover the primary use case of a search box with suggestions, e.g. Google search.

However, if you intend to use it for a [combo box](#combo-box) like experience (an enhanced version of a select element) we recommend setting `selectOnFocus`.

{{"demo": "pages/components/autocomplete/FreeSolo.js"}}

Expand Down
5 changes: 5 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
renderInput,
renderOption: renderOptionProp,
renderTags,
selectOnFocus = !props.freeSolo,
size = 'medium',
value: valueProp,
...other
Expand Down Expand Up @@ -742,6 +743,10 @@ Autocomplete.propTypes = {
* @returns {ReactNode}
*/
renderTags: PropTypes.func,
/**
* If `true`, the input's text will be selected on focus.
*/
selectOnFocus: PropTypes.bool,
/**
* The size of the autocomplete.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ export interface UseAutocompleteCommonProps<T> {
* Array of options.
*/
options?: T[];
/**
* If `true`, the input's text will be selected on focus.
*/
selectOnFocus?: boolean;
}

export interface UseAutocompleteMultipleProps<T> extends UseAutocompleteCommonProps<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export default function useAutocomplete(props) {
onInputChange,
open: openProp,
options = [],
selectOnFocus = !props.freeSolo,
value: valueProp,
componentName = 'useAutocomplete',
} = props;
Expand Down Expand Up @@ -746,7 +747,10 @@ export default function useAutocomplete(props) {
inputRef.current.selectionEnd - inputRef.current.selectionStart === 0
) {
inputRef.current.focus();
inputRef.current.select();

if (selectOnFocus) {
inputRef.current.select();
}
}

firstFocus.current = false;
Expand Down

0 comments on commit d165216

Please sign in to comment.