ant-design icon indicating copy to clipboard operation
ant-design copied to clipboard

Avoid to remove tag when pressing backspace on select

Open jjalonso opened this issue 5 years ago • 15 comments

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Hi, I have been trying to intercept on keydown and preventDefault / stopPropagation / return false but nothing works, I think it will be a great feature.

What problem does this feature solve?

Avoid tag remove when pressing backspace on select, some users will not wish it

What does the proposed API look like?

removeOnBackspace={false|true}

jjalonso avatar Mar 14 '19 01:03 jjalonso

Any update on this one, or maybe a workaround?

mattwalter91 avatar May 02 '19 09:05 mattwalter91

How to avoid tag remove when pressing backspace?

boolak avatar Jul 08 '19 07:07 boolak

Facing this problem as well. Any updates?

wilbooorn avatar Jan 24 '20 01:01 wilbooorn

Me too... Even a workaround will do...

galisandlerfed avatar Apr 30 '20 02:04 galisandlerfed

Proposed api:

type RemoveMode = "focus" | "tagClose";

<Select mode="tags" removeMode={RemoveMode[] | undefined}>

// You can only remove a item by pressing delete. Before the item is removed the tag that is about to be removed is focused so the user has some warning. No close icon appears within each tag
<Select mode="tags" removeMode={["focus"]}>

// A close icon appears within each tag, backspace does nothing once search text has been removed
<Select mode="tags" removeMode={["tagClose"]}>

// You don't want the user to be able to remove items via backspace OR close X. The only way to remove a item is by unselecting it within the dropdown UI
<Select mode="tags" removeMode={undefined}>

// A close icon appears within each tag. Backspace focus/delete also works (this would be the default to match today's)
<Select mode="tags" removeMode={["focus", "tagClose"]}>

"tagClose" Shows a close "X" in default tag render "focus" replaces today's functionality, backspace focuses on the tag prior to deletion. Pressing backspace again deletes the tag. (example 1: https://www.jqueryscript.net/demo/Dynamic-Autocomplete-Tag-Input-Plugin-For-jQuery-Tokenize2/, example 2: https://blueprintjs.com/docs/#select/multi-select)

SamKirkland avatar Jul 06 '20 16:07 SamKirkland

Try this friends:

document.getElementsByClassName("ant-select-search__field")[0].addEventListener("keydown", (e) => {
    //I am checking if backspace is pressed and if current input field (used in search) has got no value
    if (e.keyCode == 8 && e.target.value == ""){
        e.stopPropagation()
    }
})

This is done considering that my current page has only one Antd select.

If u want a very specific element select then I recommend u use document.querySelector() and target that specific select.

document.querySelector("div.avatar-dropdown.ant-select.ant-select-enabled > div > div > ul > li.ant-select-search.ant-select-search--inline > div > input").addEventListener("keydown", (e) => {
    if (e.keyCode == 8 && e.target.value == ""){
        e.stopPropagation()
    }
})

AbhayShiro avatar Jul 10 '20 07:07 AbhayShiro

maybe css .ant-select-selection--multiple .ant-select-search--inline .ant-select-search__field{ display: none; } can solve this problem

silif avatar Aug 06 '20 03:08 silif

Also would be interested in a way to handle this

nabramow avatar Feb 11 '21 07:02 nabramow

Any progress on this issue? Is there a solution supported by component api rather than workarounds?

onesinghashish avatar Mar 15 '21 06:03 onesinghashish

@afc163 I would add api: keyboard: boolean | (event) => boolean?

IMO, the issue is that search text should not be coupled with selected tags at all. so even if a user presses keydown on search box and reached empty string, the event should stop there and not affect tags at all. I am not sure if the current behavior is intentional or a bug.If it is a bug, then component api may not need the change. If its a feature, then prop name should be more meaningful like 'removeTagOnDelete: Boolean' and for custom event handling there should be another prop 'onSearchBoxKeyDown: (event) => void'

onesinghashish avatar Mar 15 '21 09:03 onesinghashish

@afc163 I would add api: keyboard: boolean | (event) => boolean?

IMO, the issue is that search text should not be coupled with selected tags at all. so even if a user presses keydown on search box and reached empty string, the event should stop there and not affect tags at all. I am not sure if the current behavior is intentional or a bug.If it is a bug, then component api may not need the change. If its a feature, then prop name should be more meaningful like 'removeTagOnDelete: Boolean' and for custom event handling there should be another prop 'onSearchBoxKeyDown: (event) => void'

OK,I understand the mistake.

kerm1it avatar Mar 15 '21 10:03 kerm1it

I was able to workaround this issue using @afc163's solution in this issue: https://github.com/ant-design/ant-design/issues/29318

pranavbadami avatar Mar 26 '21 20:03 pranavbadami

I was able to workaround this issue using @afc163's solution in this issue: #29318

Thanks bro! Save my day!

RealKai42 avatar Jul 29 '21 08:07 RealKai42

I think for a simple way you just can do this:

onInputKeyDown={event => {
   if (event.key === 'Backspace') {
           return event.stopPropagation()
     }
}

After you selected multiple tags on the select component, this won't delete the tag from the backspace key. but still can delete the search value on the select component.

justirva09 avatar Nov 29 '22 09:11 justirva09

onKeyDown = {e=>{ if (e.key === 'Backspace') { return e.stopPropagation() }
}}

this worked for me for atnd Input tag

nipun-threestops avatar Dec 07 '22 11:12 nipun-threestops


  const handleKeyPress = (e: any) => {
    const inputNode: any = document.querySelector(
      " ...your path > .ant-select-selector > .ant-select-selection-overflow > .ant-select-selection-overflow-item-suffix > .ant-select-selection-search > .ant-select-selection-search-input"
    );

    if (inputNode && !inputNode?.value) {
      inputNode.addEventListener("keydown", (e: any) => {
        if (e.keyCode === 8 && e.target.value === "") {
          e.stopPropagation();
        }
      });
    }
  };

chakerioto avatar Dec 05 '23 09:12 chakerioto