Skip to content

Conversation

pat270
Copy link
Member

@pat270 pat270 commented Jun 27, 2025

https://liferay.atlassian.net/browse/LPD-54854

This is still a draft PR. I'm having issues with the React component type (controlled / uncontrolled). From my experimentation, controlled autocomplete isn't functioning, but it could be a knowledge issue here. The storybook example I added is an uncontrolled component.

This PR adds the prop onAddNewItem. It's called when a new item is added to the list via the enter key or clicking on the dropdown item button. I'm pretty sure I've implemented it incorrectly. I'm looking for some help writing it the right way.

I've added the keys allowsCustomValue and setAsHTML to the messages prop. allowsCustomValue is used to pass in the text to display for the Create New Menu Item interaction, https://liferay.atlassian.net/wiki/spaces/ENGLEXICON/pages/3811442825/Create+New+Option#%E2%9C%A8-Interaction.

The setAsHTML key is used to enable the ability to pass in markup as well as text for the Create New Menu Item interaction.

@pat270 pat270 marked this pull request as draft June 27, 2025 23:02
Copy link
Member

@ethib137 ethib137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @pat270 this is in the right direction. A few changes are needed to simplify it and clean up the api. Thanks!

}}
roleItem="option"
>
{messages.setAsHTML &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to allow setting html. The designs don't provide this flexibility so we shouldn't either..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethib137 in some of the documentation there were some differences that required flexibility.

Case 1: Has parts of the message italicized and message text after the value

case-1

Case 2: No italics and message is before the value

case-2

HTML passed in the message prop gets escaped. I believe the only way to support this is through setting HTML.

if (allowsCustomValue && items && onAddNewItem) {
onAddNewItem();

items.push(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be adding the value to the list of items. That behavior can be handled if it's a controlled component by having the parent component add it to the list when onAddNewItem is called.

key={value}
onClick={() => {
if (allowsCustomValue && items && onAddNewItem) {
onAddNewItem();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably call onAddNewItem(value); passing in value.


inputElementRef.current?.focus();

setValue('');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be setting the value to an empty string. When this is selected we are confirming the value of the input, so the value should stay.

const ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g;

const defaultMessages = {
allowsCustomValue: 'Add {0}',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be called something more like "addNewItem" or "addCustomValue"

<DropDown.Item
className=""
id={value}
key={value}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding id and key props?

if (allowsCustomValue && items && onAddNewItem) {
onAddNewItem();

items.push(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we shouldn't be adding to items.

<li className="dropdown-divider"></li>
</>
)}
<DropDown.Item
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this only render if allowsCustomValue is false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethib137 I was under the impression that allowsCustomValue === true is for adding items not on the list.

	/**
	 * Flag to allow an input value not corresponding to an item to be set.
	 */

We could make the description better.

id="clay-autocomplete-add-new-item"
messages={{
allowsCustomValue:
'<span class="text-primary">{0} <i>(Add New Category)</i></span>',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding text-primary should be in our component rather than our example.

onAddNewItem={() => {
if (items.indexOf(value) < 0) {
setItems(() => {
const newItems = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this controlled, you need to add the items prop to the autocomplete passing in your items.

Copy link

vercel bot commented Sep 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
clayui.com Ready Ready Preview Comment Sep 8, 2025 3:46pm
storybook.clayui.com Ready Ready Preview Comment Sep 8, 2025 3:46pm

@pat270
Copy link
Member Author

pat270 commented Sep 3, 2025

@ethib137 I updated this PR with my progress. I'm not sure about allowing adding items when the component is uncontrolled. Items would be handled internally and couldn't be accessed outside the component.

I left messages.allowsCustomValue because it aligns with the flag that sets whether or not you can add custom items to autocomplete https://clayui.com/docs/components/autocomplete#api-reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants