-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #209 from LD4P/input-lookup
Adds Input lookup component
- Loading branch information
Showing
9 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2018 Stanford University see Apache2.txt for license | ||
import React from 'react' | ||
import { shallow } from 'enzyme' | ||
import InputLookup from '../../../src/components/editor/InputLookup' | ||
|
||
const plProps = { | ||
"propertyTemplate": | ||
{ | ||
"mandatory": "false", | ||
"repeatable": "true", | ||
"type": "lookup", | ||
"resourceTemplates": [], | ||
"valueConstraint": { | ||
"valueTemplateRefs": [], | ||
"useValuesFrom": [ | ||
'lookupQaLocNames' | ||
], | ||
"valueDataType": { | ||
"dataTypeURI": "http://id.loc.gov/ontologies/bibframe/Agent" | ||
}, | ||
"defaults": [] | ||
}, | ||
"propertyURI": "http://id.loc.gov/ontologies/bflc/target", | ||
"propertyLabel": "Name Lookup" | ||
} | ||
} | ||
|
||
describe('<InputLookup />', () => { | ||
const wrapper = shallow(<InputLookup {...plProps} />) | ||
|
||
it('uses the propertyLabel from the template as the form control label', () => { | ||
expect(wrapper.find('label').text()).toMatch('Name Lookup') | ||
}) | ||
|
||
it('sets the typeahead component required attribute according to the mandatory property from the template', () => { | ||
expect(wrapper.find('#lookupComponent').props().required).toBeFalsy() | ||
}) | ||
|
||
it('sets the typeahead component multiple attribute according to the repeatable property from the template', () => { | ||
expect(wrapper.find('#lookupComponent').props().multiple).toBeTruthy() | ||
}) | ||
|
||
it('sets the typeahead component placeholder attribute to be the propertyLabel', () => { | ||
expect(wrapper.find('#lookupComponent').props().placeholder).toBe('Name Lookup') | ||
}) | ||
|
||
it('should call the onChange event and set the state with the selected option', () => { | ||
const event = (wrap) => { | ||
wrap.setState({options: ["{uri: 'URI', label: 'LABEL'}"]}) | ||
} | ||
wrapper.find('#lookupComponent').simulate('change', event(wrapper)) | ||
expect(wrapper.state().options[0]).toBe("{uri: 'URI', label: 'LABEL'}") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2018 Stanford University see Apache2.txt for license | ||
import React, {Component} from 'react' | ||
import {asyncContainer, Typeahead} from 'react-bootstrap-typeahead' | ||
import PropTypes from 'prop-types' | ||
|
||
const AsyncTypeahead = asyncContainer(Typeahead) | ||
|
||
class InputLookup extends Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
isLoading: true | ||
} | ||
} | ||
|
||
render() { | ||
const lookup_url = this.props.propertyTemplate.valueConstraint.useValuesFrom[0] | ||
const isRequired = JSON.parse(this.props.propertyTemplate.mandatory) | ||
const isRepeatable = JSON.parse(this.props.propertyTemplate.repeatable) | ||
|
||
return ( | ||
<div> | ||
<label htmlFor="lookupComponent">{this.props.propertyTemplate.propertyLabel} | ||
<AsyncTypeahead | ||
id="lookupComponent" | ||
required={isRequired} | ||
multiple={isRepeatable} | ||
placeholder={this.props.propertyTemplate.propertyLabel} | ||
useCache={true} | ||
isLoading={this.state.isLoading} | ||
onSearch={query => { | ||
this.setState({isLoading: true}); | ||
//TODO: this fetch function will be replaced with a swagger API call function (#197): | ||
fetch(`${lookup_url}?q=${query}&maxRecords=6`) | ||
.then(resp => resp.json()) | ||
.then(json => this.setState({ | ||
isLoading: false, | ||
options: json | ||
})) | ||
}} | ||
onChange={selected => { | ||
this.setState({selected}) | ||
} | ||
} | ||
options={this.state.options} | ||
selected={this.state.selected} | ||
/> | ||
</label> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
InputLookup.propTypes = { | ||
propertyTemplate: PropTypes.shape({ | ||
propertyLabel: PropTypes.string, | ||
mandatory: PropTypes.oneOfType([ PropTypes.string, PropTypes.bool]), | ||
repeatable: PropTypes.oneOfType([ PropTypes.string, PropTypes.bool]), | ||
valueConstraint: PropTypes.shape({ | ||
useValuesFrom: PropTypes.oneOfType([ PropTypes.string, PropTypes.array]) | ||
}) | ||
}).isRequired | ||
} | ||
|
||
export default InputLookup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,3 +83,6 @@ a.editor-help-resources { | |
font-size: 1.3em; | ||
color: #2F2424; | ||
} | ||
input.rbt-input-main { | ||
min-width: 300px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters