ldx-widgets
Version:
widgets
56 lines (46 loc) • 1.52 kB
JavaScript
(function() {
var React, span;
React = require('react');
span = require('react-dom-factories').span;
/*
Filter mixin
data siphoned through this mixin will be wrapped with highlight elements if the contents match a provided search term
@params.term - String - the search term that is fed into the f() method. Typically would be done via a store property through a render method
@params.source - String - the source term to compare against. This is the data that would be received by the API, or other searchable terms
*/
module.exports = {
f: function(source, term) {
var ch1, ch2, ch3, ra, rv, termIndex;
if (source == null) {
return '';
}
term = this.props.filterTerm || term || '';
term = term.trim();
termIndex = source.toLowerCase().indexOf(term);
if (term.toLowerCase().length > 2 && termIndex > -1) {
ra = [];
if (termIndex) {
ch1 = source.substring(0, termIndex);
ra.push(span({
key: ch1
}, ch1));
}
ch2 = source.substring(termIndex, termIndex + term.length);
ra.push(span({
key: ch2,
className: 'ft'
}, ch2));
if (termIndex + term.length < source.length) {
ch3 = source.substr(termIndex + ch2.length);
ra.push(span({
key: ch3
}, ch3));
}
rv = ra;
} else {
rv = source;
}
return rv;
}
};
}).call(this);