react-datalist-input/README.md

175 lines
5.5 KiB
Markdown
Raw Normal View History

2018-07-16 20:02:12 +02:00
## Info
This package provides a single react component.
The component contains an input field with a drop down menu to pick a possible option based on the current input as a react component.
2019-08-14 14:55:50 +02:00
Have a look at [w3schools.com](https://www.w3schools.com/howto/howto_js_autocomplete.asp) to see how you can do something similar with pure html, css, and js.
2018-07-16 20:02:12 +02:00
For more information about react and the ecosystem see this [guide](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md).
2018-07-16 20:02:12 +02:00
## Feedback
2019-06-24 20:57:49 +02:00
Feel free to get inspired and more importantly please provide [your feedback](https://github.com/andrelandgraf/react-datalist-input/issues) on structure and style. I'm more than happy to learn how to improve my code and architecture.
2018-07-16 20:02:12 +02:00
## Installation
2018-07-16 20:02:12 +02:00
**Installation via npm**
2018-07-16 20:02:12 +02:00
```
npm install react-datalist-input --save
```
***Basic Usage***
2018-07-16 20:02:12 +02:00
```
import DataListInput from 'react-datalist-input';
2018-07-16 20:02:12 +02:00
/**
* create your own match algorithm if you want to do so
* @param currentInput String (the current user input)
* @param item (one item of the items array)
* @returns {boolean}
*/
matchCurrentInput = (currentInput, item) => {
const yourLogic = item.someAdditionalValue;
return (yourLogic.substr(0, currentInput.length).toUpperCase() === currentInput.toUpperCase());
};
2018-07-16 20:02:12 +02:00
/**
* your callback function gets called if the user selects one option out of the drop down menu
* @param selectedItem object (the selected item / option)
* @returns {*}
*/
onSelect = (selectedItem) => {
this.doSomething(selectedItem);
};
render() {
// the array you want to pass to the react-data-list component
// each element at least needs a key and a label
const items = this.props.values.map((item, i) => {
return {
// what to show to the user
label: item.id + ": " + item.name,
// key to identify the item within the array
key: item.id,
// feel free to add your own app logic to access those properties later on
someAdditionalValue: item.someAdditionalValue,
}
});
2019-06-24 20:57:49 +02:00
2018-07-19 13:22:25 +02:00
return(
<div>
<DataListInput placeholder={"Select an option from the drop down menu..."}
items={items} onSelect={this.onSelect} match={this.matchCurrentInput}/>
</div>
);
2018-07-16 20:02:12 +02:00
```
2019-06-24 20:57:49 +02:00
## Properties
2018-07-19 13:22:25 +02:00
***items***
2019-06-24 20:57:49 +02:00
- <b>Required</b> property!
- The array of options for the drop down menu.<br>
- Every item inside the array needs to have following properties:
- key : an id that identifies the item within the array
- label: the label that will be shown in the drop down menu
2018-07-19 13:22:25 +02:00
***onSelect***
2019-06-24 20:57:49 +02:00
- <b>Required</b> property!
- The callback function that will be called if the user selects one item of the drop down menu.
- Gets only called if the item changes. Selecting the same item twice will only trigger the function once (the first time).
2019-06-24 20:57:49 +02:00
- Parameter: (selectedKey)
- selectedKey: the Key Property of the item that the user selected
2018-07-19 13:22:25 +02:00
***match***
- Pass a match function as stated above for creating your own matching algorithm for the autocomplete functionality.
- Parameter: (currentInput, item)
- currentInput: String, the current user input typed into the input field
- item: Object, the item of the items array (with key and label properties)
- Default:
2018-07-16 20:02:12 +02:00
```
/**
* default function for matching the current input value (needle) and the values of the items array
* @param currentInput String (the current user input)
* @param item (one item of the items array)
* @returns {boolean}
*/
match = (currentInput, item) => {
return item.label.substr(0, currentInput.length).toUpperCase() === currentInput.toUpperCase();
};
```
***onDropdownOpen***
- The callback function that will be called after opening the drop down menu.
***onDropdownClose***
- The callback function that will be called after closing the drop down menu.
2018-07-19 13:22:25 +02:00
***placeholder***
2019-06-24 20:57:49 +02:00
- The placeholder that will be shown inside the input field.
- Default is an empty string
2019-06-12 21:15:40 +02:00
***itemClassName***
- Additional classes to style each input field in the dropdown menu.
- Default is an empty string
2019-06-21 14:28:04 +02:00
- Removes the default styling if set
2019-06-12 21:15:40 +02:00
***activeItemClassName***
- Additional classes to style the active input field.
- Default is an empty string
2019-06-21 14:28:04 +02:00
- Removes the default styling if set
2019-06-12 21:15:40 +02:00
***inputClassName***
- Additional classes to style the input field.
- Default is an empty string
2019-06-21 14:28:04 +02:00
- Removes the default styling if set
***dropdownClassName***
- Additional classes to style the dropdown box.
- Default is an empty string
- Adds on the required styling (e.g. position:absolute)
- Removes the default styling if set
2019-06-12 21:15:40 +02:00
***requiredInputLength***
- Number to specify the threshold until when the dropdown menu should appear.
- Example `requiredInputLength=3`, only if the user input is longer than 2 characters, the dropdown menu will appear.
- Default is zero.
2019-06-13 11:31:00 +02:00
***clearInputOnSelect***
- Should the input field be cleared on select on filled with selected item?
- Default is false.
***suppressReselect***
2019-06-24 20:57:49 +02:00
- If suppressReselect is set to false, selecting the same item again, it will trigger another onSelect callback call.
2019-06-13 11:31:00 +02:00
- Default is true.
2019-06-13 21:17:49 +02:00
***dropDownLength***
- Only display the first `dropDownLength` matches in the dropdown. Useful if the array is really big.
- Number to specify max length of drop down.
- Default is Infinity.
***initialValue***
- Specigy an initial value for the input field.
2019-06-24 20:57:49 +02:00
- For example, `initialValue={'hello world'}` will print `hello world` into the input field on first render.
- Default is empty string.