Skip to content

Commit

Permalink
[Autocomplete] Forward props to renderTags() (#32637)
Browse files Browse the repository at this point in the history
Co-authored-by: Michał Dudak <[email protected]>
  • Loading branch information
emlai and michaldudak authored May 11, 2022
1 parent ed8de7f commit 6ad56c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/translations/api-docs/autocomplete/autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"renderGroup": "Render the group.<br><br><strong>Signature:</strong><br><code>function(params: AutocompleteRenderGroupParams) =&gt; ReactNode</code><br><em>params:</em> The group to render.",
"renderInput": "Render the input.<br><br><strong>Signature:</strong><br><code>function(params: object) =&gt; ReactNode</code><br>",
"renderOption": "Render the option, use <code>getOptionLabel</code> by default.<br><br><strong>Signature:</strong><br><code>function(props: object, option: T, state: object) =&gt; ReactNode</code><br><em>props:</em> The props to apply on the li element.<br><em>option:</em> The option to render.<br><em>state:</em> The state of the component.",
"renderTags": "Render the selected value.<br><br><strong>Signature:</strong><br><code>function(value: Array&lt;T&gt;, getTagProps: function) =&gt; ReactNode</code><br><em>value:</em> The <code>value</code> provided to the component.<br><em>getTagProps:</em> A tag props getter.",
"renderTags": "Render the selected value.<br><br><strong>Signature:</strong><br><code>function(value: Array&lt;T&gt;, getTagProps: function, ownerState: object) =&gt; ReactNode</code><br><em>value:</em> The <code>value</code> provided to the component.<br><em>getTagProps:</em> A tag props getter.<br><em>ownerState:</em> The state of the Autocomplete component.",
"selectOnFocus": "If <code>true</code>, the input&#39;s text is selected on focus. It helps the user clear the selected value.",
"size": "The size of the component.",
"sx": "The system prop that allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details.",
Expand Down
24 changes: 23 additions & 1 deletion packages/mui-material/src/Autocomplete/Autocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ export {
createFilterOptions,
};

export type AutocompleteOwnerState<
T,
Multiple extends boolean | undefined,
DisableClearable extends boolean | undefined,
FreeSolo extends boolean | undefined,
ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
> = AutocompleteProps<T, Multiple, DisableClearable, FreeSolo, ChipComponent> & {
disablePortal: boolean;
focused: boolean;
fullWidth: boolean;
hasClearIcon: boolean;
hasPopupIcon: boolean;
inputFocused: boolean;
popupOpen: boolean;
size: OverridableStringUnion<'small' | 'medium', AutocompletePropsSizeOverrides>;
};

export type AutocompleteRenderGetTagProps = ({ index }: { index: number }) => {
key: number;
className: string;
Expand Down Expand Up @@ -226,9 +243,14 @@ export interface AutocompleteProps<
*
* @param {T[]} value The `value` provided to the component.
* @param {function} getTagProps A tag props getter.
* @param {object} ownerState The state of the Autocomplete component.
* @returns {ReactNode}
*/
renderTags?: (value: T[], getTagProps: AutocompleteRenderGetTagProps) => React.ReactNode;
renderTags?: (
value: T[],
getTagProps: AutocompleteRenderGetTagProps,
ownerState: AutocompleteOwnerState<T, Multiple, DisableClearable, FreeSolo, ChipComponent>,
) => React.ReactNode;
/**
* The size of the component.
* @default 'medium'
Expand Down
4 changes: 3 additions & 1 deletion packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
const hasClearIcon = !disableClearable && !disabled && dirty && !readOnly;
const hasPopupIcon = (!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false;

// If you modify this, make sure to keep the `AutocompleteOwnerState` type in sync.
const ownerState = {
...props,
disablePortal,
Expand All @@ -473,7 +474,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
});

if (renderTags) {
startAdornment = renderTags(value, getCustomizedTagProps);
startAdornment = renderTags(value, getCustomizedTagProps, ownerState);
} else {
startAdornment = value.map((option, index) => (
<Chip
Expand Down Expand Up @@ -1024,6 +1025,7 @@ Autocomplete.propTypes /* remove-proptypes */ = {
*
* @param {T[]} value The `value` provided to the component.
* @param {function} getTagProps A tag props getter.
* @param {object} ownerState The state of the Autocomplete component.
* @returns {ReactNode}
*/
renderTags: PropTypes.func,
Expand Down

0 comments on commit 6ad56c2

Please sign in to comment.