Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoComplete:<li> cannot appear as a descendant of <li> when using ListItem in renderOption #19827

Closed
jeromeSH26 opened this issue Feb 23, 2020 · 9 comments
Labels
component: autocomplete This is the name of the generic UI component, not the React module! discussion

Comments

@jeromeSH26
Copy link

Hi guys,
I have this boring warning when using a ListItem object as renderOption:
Warning: validateDOMNesting(...): <li> cannot appear as a descendant of <li>.

Can't find how to bypass it. Any idea?

import {
	ListItem as MUIListItem,
	ListItemProps,
	TextField,
} from "@material-ui/core";
import RemoveIcon from "@material-ui/icons/RemoveCircleOutlineSharp";
import Autocomplete from "@material-ui/lab/Autocomplete";
import React, { useState } from "react";
import { ITextFieldProps } from "../InputField/BaseInputField";
import Chip from "./../Chip";

export interface IOptionsValue {
	value: string;
	label: string;
}

interface IMultiSelectChipsProps {
	options: IOptionsValue[];
}

export interface IListItem extends ListItemProps {
	option: IOptionsValue;
}

const ListItem: React.FC<IListItem> = (props: IListItem): JSX.Element => {
	return (
		<MUIListItem key={props.option.value} value={props.option.value}>
			{props.option.label}
		</MUIListItem>
	);
};

const MultiSelectChips: React.FC<IMultiSelectChipsProps> = (
	props: IMultiSelectChipsProps
) => {
	const [val, setVal] = useState<IOptionsValue[]>([]);

	return (
		<div style={{ width: 500 }}>
			<Autocomplete
				multiple
				id="tags-standard"
				freeSolo
				filterSelectedOptions
				options={props.options}
				onChange={(_, newValue: IOptionsValue[]) => setVal(newValue)}
				getOptionLabel={(option: IOptionsValue) => option.label}
				renderTags={(value: IOptionsValue[]) =>
					value.map((option: IOptionsValue, index: number) => (
						<Chip
							key={index}
							label={option.label}
							deleteIcon={<RemoveIcon />}
							onDelete={() => {
								setVal(val.filter(entry => entry !== option));
							}}
						/>
					))
				}
				value={val}
				renderInput={(params: ITextFieldProps) => (
					<TextField
						{...params}
						variant="standard"
						placeholder="Select..."
						margin="normal"
						fullWidth
					/>
				)}
				renderOption={(option: IOptionsValue) => (
					<ListItem option={option} />
				)}
				{...props}
			/>
		</div>
	);
};

export default MultiSelectChips;

@oliviertassinari oliviertassinari added the component: autocomplete This is the name of the generic UI component, not the React module! label Feb 24, 2020
@jeromeSH26

This comment has been minimized.

@oliviertassinari
Copy link
Member

@jeromeSH26 The API was designed to prevent the usage of ListItem in this context. It's too slow.

@oliviertassinari
Copy link
Member

The alternative is to set a component="div" on the list item to clear the warning.

@ImanMahmoudinasab
Copy link
Contributor

@jeromeSH26 The API was designed to prevent the usage of ListItem in this context. It's too slow.

  1. What makes it slow?
  2. What is the recommended solution in this case?

The alternative is to set a component="div" on the list item to clear the warning.

This alternative won't help if we use <ListItemSecondaryAction> in the ListItem as it will wrap item with a <li>.

@oliviertassinari
Copy link
Member

@ImanMahmoudinasab See #19254 for a proper fix, in case you want to work on it.

@ImanMahmoudinasab
Copy link
Contributor

@oliviertassinari The changes in the mentioned issue will allow developers to use MenuItems as a Autocomplete's item. But what about the slowness you mentioned?
I like to get more insight into the slowness issue, any reference?

@oliviertassinari
Copy link
Member

I like to get more insight into the slowness issue

@ImanMahmoudinasab Best to try it out by yourselves, if you are fine with it, you can move forward.

@MikeAlvarado
Copy link

The alternative is to set a component="div" on the list item to clear the warning.

Worked for me, thanks!

@Muzzammilhusen-B
Copy link

The alternative is to set a component="div" on the list item to clear the warning.

It works .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! discussion
Projects
None yet
Development

No branches or pull requests

6 participants
@oliviertassinari @ImanMahmoudinasab @MikeAlvarado @jeromeSH26 @Muzzammilhusen-B and others