new version, adding external styling via props

This commit is contained in:
Andre Landgraf 2019-06-12 12:26:27 +02:00
parent 0f725f862e
commit 7fd476ecdb
2 changed files with 49 additions and 30 deletions

View File

@ -5,16 +5,13 @@
width: 100%;
height: 100%;
}
input {
.autocomplete-input {
border: 1px solid transparent;
background-color: #f1f1f1;
width: 100%;
height: 100%;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
.datalist-items {
position: absolute;
border: 1px solid #d4d4d4;

View File

@ -133,33 +133,42 @@ class DataListInput extends React.Component {
this.props.onSelect(selectedItem);
};
renderItems = ( items, focusIndex, activeItemClassName, itemClassName) => (
<div className="datalist-items">
{items.map((item, i) => {
const isActive = focusIndex === i;
const itemActiveClasses = isActive ? `datalist-active-item ${activeItemClassName}` : ''
const itemClasses = `${itemClassName} ${itemActiveClasses};`
return (
<div onClick={this.onClickItem}
className={itemClasses}
key={item.key}>
{item.label.substr(0, this.indexOfMatch(currentInput, item))}
<strong>{item.label.substr(this.indexOfMatch(currentInput, item), currentInput.length)}</strong>
{item.label.substr(this.indexOfMatch(currentInput, item) + currentInput.length)}
<input type='hidden' value={item.key}/>
</div>
)
})}
</div>
);
renderInputField = ( placeholder, currentInput, inputClassName ) => (
<input onKeyDown={this.onHandleKeydown} onInput={this.onHandleInput} type="text"
className={ `autocomplete-input ${inputClassName}` }
placeholder={placeholder} value={currentInput}/>
)
render() {
const currentInput = this.state.currentInput;
const items = this.state.matchingItems;
const placeholder = this.props.placeholder;
const { currentInput, matchingItems, focusIndex, visible } = this.state;
const { placeholder, inputClassName, activeItemClassName, itemClassName, requiredInputLength } = this.props;
const reachedRequiredLength = currentInput.length >= requiredInputLength;
return (
<div className="datalist-input">
<input onKeyDown={this.onHandleKeydown} onInput={this.onHandleInput} type="text"
className="autocompleteInput"
placeholder={placeholder} value={currentInput}/>
{(currentInput !== "" && this.state.visible &&
<div className={"datalist-items"}>
{items.map((item, i) => {
return (
<div onClick={this.onClickItem}
className={this.state.focusIndex === i ? " datalist-active-item" : undefined}
key={item.key}>
{item.label.substr(0, this.indexOfMatch(currentInput, item))}
<strong>{item.label.substr(this.indexOfMatch(currentInput, item), currentInput.length)}</strong>
{item.label.substr(this.indexOfMatch(currentInput, item) + currentInput.length)}
<input type='hidden' value={item.key}/>
</div>
)
})}
</div>
)}
{ this.renderInputField( placeholder, valu, inputClassName ) }
{ reachedRequiredLength && visible &&
this.renderItems( matchingItems, focusIndex, activeItemClassName, itemClassName )
}
</div>
);
}
@ -169,7 +178,20 @@ DataListInput.propTypes = {
items: PropTypes.array.isRequired,
placeholder: PropTypes.string,
onSelect: PropTypes.func.isRequired,
match: PropTypes.func
match: PropTypes.func,
inputClassName: PropTypes.string,
itemClassName: PropTypes.string,
activeItemClassName: PropTypes.string,
requiredInputLength: PropTypes.number,
};
DataListInput.defaultProps = {
placeholder: '',
match: undefined,
inputClassName: '',
itemClassName: '',
activeItemClassName: '',
requiredInputLength: 1,
};
export default DataListInput;