Skip to content

Commit

Permalink
Merge pull request #2568 from LD4P/t2564-quick-fix
Browse files Browse the repository at this point in the history
Allow user to add URI or literal in Lookup Modal
  • Loading branch information
jcoyne authored Oct 1, 2020
2 parents a2d022f + c194b21 commit 7694c2a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
13 changes: 10 additions & 3 deletions __tests__/feature/editing/selectFromLookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ describe('selecting a value from the lookup modal', () => {
await waitFor(() => expect(screen.getByLabelText('Instance of (lookup)')))

const input = screen.getByLabelText('Instance of (lookup)')
fireEvent.change(input, { target: { value: 'test' } })

fireEvent.keyUp(input, { target: { value: 'test' } })

expect(input).toHaveValue('test')

// TODO: Add additional selection testing after https://github.com/LD4P/sinopia_editor/issues/2398
// This test is limited by the selectors assigned in AsyncTypeahead and will be replaced
// Checks if the Add Literal button is available
screen.getByLabelText(/add as new literal/i)

// Adds URI to search on
fireEvent.keyUp(input, { target: { value: 'http://example.com/' } })

// Checks if the Add URI button is available
screen.getByLabelText(/add as new uri/i)
})
})
21 changes: 21 additions & 0 deletions src/components/editor/property/LookupWithMultipleAuthorities.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { newUriValue, newLiteralValue } from 'utilities/valueFactory'
import { addProperty } from 'actions/resources'
import { hideModal } from 'actions/modals'
import { selectNormValues } from 'selectors/resources'
import { isValidURI } from 'utilities/Utilities'

import RenderLookupContext from './RenderLookupContext'
import Tab from '../Tab'
Expand Down Expand Up @@ -71,6 +72,25 @@ const LookupWithMultipleAuthorities = (props) => {
})
}, [query, allAuthorities, getLookupResults])

const addUriOrLiteral = () => {
if (!query) return
const item = {}
let typeOf = 'Literal'
if (isValidURI(query)) {
item.uri = query
item.label = query
typeOf = 'URI'
} else {
item.content = query
}
const ariaLabel = `Add as new ${typeOf}`
return (<button onClick={() => selectionChanged(item)}
aria-label={ariaLabel}
className="btn search-result">
<strong>New {typeOf}:</strong> {query}</button>
)
}

const selectionChanged = (item) => {
dispatch(hideModal())
const newProperty = { ...props.property, values }
Expand Down Expand Up @@ -167,6 +187,7 @@ const LookupWithMultipleAuthorities = (props) => {
<label htmlFor="search">{props.propertyTemplate.label}</label>
<input id="search" type="search" className="form-control"
onKeyUp={(e) => setQuery(e.target.value)}></input>
{addUriOrLiteral()}
</div>

<button type="button" className="close" onClick={close} aria-label="Close">
Expand Down

0 comments on commit 7694c2a

Please sign in to comment.