A react component that renders an array as an html list. The array can consist of
primitives or objects. In the case of objects, the formatItem
function
property can be used to format the item and return either a primitive or
object. By default a string is expected. In case of an object, a custom itemComponent
is required.
Install as node dependency:
npm install react-simple-list --save
var React = require('react');
var list = require('react-simple-list');
React.render(React.createElement(list, {
items: ['Java', 'Java Flash', 'JavaScript']
}), document.querySelector('#list-1'));
Note that the value
property is used as the label by default. This allows
the usage of objects without the help of the item formatter and component.
React.render(React.createElement(list, {
items: [{
value: 'Apple'
}, {
value: 'Banana'
}, {
value: 'Cherry'
}]
}), document.querySelector('#list-2'));
var items = [
{id: 1, firstname: 'Mike', lastname: 'November'},
{id: 2, firstname: 'India', lastname: 'Juliet'},
{id: 3, firstname: 'Alpha', lastname: 'Bravo'}
];
var itemComponent = React.createClass({
propTypes: {
href: React.PropTypes.string.isRequired,
text: React.PropTypes.string.isRequired
},
render: function () {
return (
React.DOM.a({ href: this.props.href }, this.props.text)
);
}
});
function formatItem(item) {
return {
href: '#' + item.id,
text: item.firstname + ' ' + item.lastname
};
}
React.render(React.createElement(list, {
items: items,
formatItem: formatItem,
itemComponent: itemComponent,
cssClass: 'list'
}), document.querySelector('#list-2'));
items
: an array of items, where items can be an arbitrary objectformatItem
: an optional function that is called with each item and used to map the item to the properties of itemComponentitemComponent
: an optional react class that is used to display an itemcssClass
: optional css class for the listonItemClick(event, item, index)
: optional click handler. The argument contains the value of the clicked item and its index.
cssClass
: If an item object contains the propertycssClass
it will be applied to the rendered<li>
element.value
: If an item object contains the propertyvalue
, it is used as the item label if no customitemComponent
is given.
npm start & npm run watch
npm run build
- build production css and jsnpm run watch
- compile css and jsnpm start
- start static dev server
MIT