@@ -159,8 +165,8 @@ export default function SelectCollaborators({
ref: inputRef,
// preventKeyAction: showDropdown,
onBlur: () => {
- // blur: if the dropdown isn't visible, try to create a new item using inputValue
- if (!showDropdownItems) {
+ // blur: if the dropdown isn't open, try to create a new item using inputValue
+ if (!isOpen) {
addNewItem(inputValue, false)
}
},
@@ -173,8 +179,8 @@ export default function SelectCollaborators({
break
case 'Tab':
- // Tab: if the dropdown isn't visible, try to create a new item using inputValue and prevent blur if successful
- if (!showDropdownItems && addNewItem(inputValue)) {
+ // Tab: if the dropdown isn't open, try to create a new item using inputValue and prevent blur if successful
+ if (!isOpen && addNewItem(inputValue)) {
event.preventDefault()
event.stopPropagation()
}
@@ -214,9 +220,9 @@ export default function SelectCollaborators({
/>
-
+
- {showDropdownItems &&
+ {isOpen &&
filteredOptions.map((item, index) => (
', function() {
'Link sharing is off, only invited users can view this project.'
)
})
+
+ it('avoids selecting unmatched contact', async function() {
+ render()
+
+ const [inputElement] = await screen.findAllByLabelText(
+ 'Share with your collaborators'
+ )
+
+ // Wait for contacts to load
+ await waitFor(() => {
+ expect(fetchMock.called('express:/user/contacts')).to.be.true
+ })
+
+ // Enter a prefix that matches a contact
+ inputElement.focus()
+ fireEvent.change(inputElement, { target: { value: 'ptolemy' } })
+
+ // The matching contact should now be present and selected
+ await screen.findByRole('option', {
+ name: `Claudius Ptolemy `,
+ selected: true
+ })
+
+ // Keep entering text so the contact no longer matches
+ fireEvent.change(inputElement, {
+ target: { value: 'ptolemy.new@example.com' }
+ })
+
+ // The matching contact should no longer be present
+ expect(
+ screen.queryByRole('option', {
+ name: `Claudius Ptolemy `
+ })
+ ).to.be.null
+
+ // No items should be added yet
+ expect(screen.queryByRole('button', { name: 'Remove' })).to.be.null
+
+ // Pressing Tab should add the entered item
+ fireEvent.keyDown(inputElement, { key: 'Tab', code: 'Tab' })
+ await waitFor(() => {
+ expect(screen.getAllByRole('button', { name: 'Remove' })).to.have.length(
+ 1
+ )
+ })
+
+ // Blurring the input should not add another contact
+ fireEvent.blur(inputElement)
+ await waitFor(() => {
+ expect(screen.getAllByRole('button', { name: 'Remove' })).to.have.length(
+ 1
+ )
+ })
+ })
})