-
Notifications
You must be signed in to change notification settings - Fork 9
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 #207 from LD4P/repeatable-mandatory-input
Repeatable mandatory input for type Literal
- Loading branch information
Showing
2 changed files
with
183 additions
and
5 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 |
---|---|---|
@@ -1,16 +1,140 @@ | ||
// Copyright 2018 Stanford University see Apache2.txt for license | ||
|
||
import React, {Component} from 'react'; | ||
import React, {Component} from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
class InputLiteral extends Component { | ||
|
||
constructor(props) { | ||
super(props) | ||
this.handleChange = this.handleChange.bind(this) | ||
this.handleKeypress = this.handleKeypress.bind(this) | ||
this.handleClick = this.handleClick.bind(this) | ||
this.checkMandatoryRepeatable = this.checkMandatoryRepeatable.bind(this) | ||
this.nonRepeatableNonMandatory = this.nonRepeatableNonMandatory.bind(this) | ||
this.nonRepeatableMandatory = this.nonRepeatableMandatory.bind(this) | ||
this.addUserInput = this.addUserInput.bind(this) | ||
this.state = { | ||
content_add: "", | ||
myItems: [] | ||
} | ||
this.lastId = -1 | ||
} | ||
|
||
handleChange(event) { | ||
const usr_input = event.target.value | ||
this.setState({ content_add: usr_input }) | ||
} | ||
|
||
/** repeatable = false and mandatory = true **/ | ||
nonRepeatableMandatory(userInputArray, currentcontent) { | ||
if (userInputArray.length == 0) { | ||
this.addUserInput(userInputArray, currentcontent) | ||
} | ||
} | ||
|
||
/** repeatable = false and mandatory = false **/ | ||
nonRepeatableNonMandatory(userInputArray, currentcontent) { | ||
if (userInputArray.length < 1) { | ||
this.addUserInput(userInputArray, currentcontent) | ||
} | ||
} | ||
|
||
addUserInput(userInputArray, currentcontent) { | ||
userInputArray.push({ | ||
content: currentcontent, | ||
id: ++this.lastId | ||
}) | ||
} | ||
|
||
handleKeypress(event) { | ||
if (event.key == "Enter") { | ||
var userInputArray = this.state.myItems | ||
var currentcontent = this.state.content_add.trim() | ||
if (!currentcontent) { | ||
return | ||
} | ||
/** Input field is repeatable, add user input to array.**/ | ||
if (this.props.propertyTemplate.repeatable == "true") { | ||
this.addUserInput(userInputArray, currentcontent) | ||
/** Input field is not repeatable **/ | ||
} else if (this.props.propertyTemplate.repeatable == "false") { | ||
/** Mandatory true, means array must have only 1 item in the array **/ | ||
if (this.props.propertyTemplate.mandatory == "true") { | ||
this.nonRepeatableMandatory(userInputArray, currentcontent) | ||
/** Mandatory is false, or not defined. Array can have either 0 or 1 item in array. **/ | ||
} else { | ||
this.nonRepeatableNonMandatory(userInputArray, currentcontent) | ||
} | ||
} | ||
this.setState({ | ||
myItems: userInputArray, | ||
content_add: "", | ||
}) | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
handleClick(event) { | ||
const idToRemove = Number(event.target.dataset["item"]) | ||
const userInputArray = this.state.myItems.filter((listitem) => {return listitem.id !== idToRemove}) | ||
this.setState({ myItems: userInputArray }) | ||
} | ||
|
||
checkMandatoryRepeatable() { | ||
if (this.props.propertyTemplate.mandatory == "true") { | ||
if (this.makeAddedList().length > 0) { | ||
return false | ||
} | ||
else { | ||
return true | ||
} | ||
} | ||
else if (this.props.propertyTemplate.mandatory == "false") { | ||
return false | ||
} | ||
} | ||
|
||
makeAddedList() { | ||
const elements = this.state.myItems.map((listitem) => ( | ||
<button | ||
key={listitem.id} | ||
onClick={this.handleClick} | ||
data-item={listitem.id} | ||
> | ||
{listitem.content} | ||
</button> | ||
)) | ||
return elements | ||
} | ||
render() { | ||
return ( | ||
<div className="form-group"> | ||
<label>{this.props.propertyTemplate.propertyLabel}</label> | ||
<input className="form-control" required = { this.props.propertyTemplate.mandatory? true : null} type="text" placeholder={this.props.propertyTemplate.propertyLabel}/> | ||
<label htmlFor="typeLiteral"> | ||
{this.props.propertyTemplate.propertyLabel} | ||
<input | ||
{ ...(this.checkMandatoryRepeatable() ? {required: 'required'} : null) } | ||
className="form-control" | ||
placeholder={this.props.propertyTemplate.propertyLabel} | ||
onChange={this.handleChange} | ||
onKeyPress={this.handleKeypress} | ||
value={this.state.content_add} | ||
it="typeLiteral" | ||
/> | ||
{this.makeAddedList()} | ||
</label> | ||
|
||
</div> | ||
) | ||
} | ||
} | ||
|
||
InputLiteral.propTypes = { | ||
propertyTemplate: PropTypes.shape({ | ||
propertyLabel: PropTypes.string.isRequired, | ||
mandatory: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), | ||
repeatable: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]) | ||
}).isRequired | ||
} | ||
|
||
export default InputLiteral; |