From b2d58668e0d4f7a9df5a6e58b082df6300138457 Mon Sep 17 00:00:00 2001 From: Jonathan Lehman Date: Mon, 1 Aug 2016 11:22:01 -0400 Subject: [PATCH] Add renderContainer prop to FilterResults --- README.md | 4 ++++ dist/react-fuzzy-filter.min.js | 2 +- src/FilterResults.js | 15 +++++++++------ test/FilterResults_test.js | 25 +++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 995cd7d..f4c5ca9 100644 --- a/README.md +++ b/README.md @@ -117,3 +117,7 @@ Collection of fuzzy filtered items (filtered by the `InputFilter`'s value), each ### wrapperProps `wrapperProps` is an optional object containing additional props to be passed to the `wrapper`. + +### renderContainer + +`renderContainer` is an alternative to using `wrapper` and `wrapperProps`. It is a function that is used as the render function for `FilterResults`. It receives an array of React elements (the items after they have already been transformed by `renderItem`, and should return a React element. diff --git a/dist/react-fuzzy-filter.min.js b/dist/react-fuzzy-filter.min.js index fd1752d..a011454 100644 --- a/dist/react-fuzzy-filter.min.js +++ b/dist/react-fuzzy-filter.min.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports.ReactFuzzyFilter=e(require("react")):t.ReactFuzzyFilter=e(t.React)}(this,function(t){return function(t){function e(n){if(r[n])return r[n].exports;var s=r[n]={exports:{},id:n,loaded:!1};return t[n].call(s.exports,s,s.exports,e),s.loaded=!0,s.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{"default":t}}function s(){var t=new i.Subject;return{InputFilter:(0,c["default"])(t),FilterResults:(0,a["default"])(t)}}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=s;var i=r(13),o=r(9),c=n(o),u=r(8),a=n(u)},function(t,e,r){"use strict";var n=r(20),s=r(21),i=r(5),o=r(23),c=r(4),u=r(19),a=function(){function t(t){this.isUnsubscribed=!1,t&&(this._unsubscribe=t)}return t.prototype.unsubscribe=function(){var t,e=!1;if(!this.isUnsubscribed){this.isUnsubscribed=!0;var r=this,a=r._unsubscribe,p=r._subscriptions;if(this._subscriptions=null,i.isFunction(a)){var h=o.tryCatch(a).call(this);h===c.errorObject&&(e=!0,(t=t||[]).push(c.errorObject.e))}if(n.isArray(p))for(var f=-1,l=p.length;++f1)throw new Error("Key weight has to be > 0 and <= 1");t=t.name}else this._keyMap[t]={weight:1};this._analyze(t,i(p,t,[]),p,r)}},n.prototype._analyze=function(t,e,n,s){var o,c,u,a,p,h,f,l,b,y,d,v,m,_=this.options,w=!1;if(void 0!==e&&null!==e)if(c=[],"string"==typeof e){if(o=e.split(_.tokenSeparator),_.verbose&&r("---------\nKey:",t),_.verbose&&r("Record:",o),this.options.tokenize){for(v=0;v0){if(e={item:o.item},p.indexOf("matches")!==-1)for(n=o.output,e.matches=[],r=0;rP.maxPatternLength){if(v=t.match(new RegExp(this.pattern.replace(P.tokenSeparator,"|"))),m=!!v)for(w=[],e=0,g=v.length;e=p;r--)if(b=this.patternAlphabet[t.charAt(r-1)],b&&(_[r-1]=1),0===e?f[r]=(f[r+1]<<1|1)&b:f[r]=(f[r+1]<<1|1)&b|((l[r+1]|l[r])<<1|1)|l[r+1],f[r]&this.matchmask&&(y=this._bitapScore(e,r-1),y<=i)){if(i=y,o=r-1,d.push(o),!(o>s))break;p=Math.max(1,2*s-o)}if(this._bitapScore(e+1,s)>i)break;l=f}return w=this._getMatchedIndices(_),{isMatch:o>=0,score:0===y?.001:y,matchedIndices:w}},o.prototype._getMatchedIndices=function(t){for(var e,r=[],n=-1,s=-1,i=0,o=t.length;i1)throw new Error("Key weight has to be > 0 and <= 1");t=t.name}else this._keyMap[t]={weight:1};this._analyze(t,i(p,t,[]),p,r)}},n.prototype._analyze=function(t,e,n,s){var o,c,u,a,p,h,f,l,b,y,d,v,m,_=this.options,w=!1;if(void 0!==e&&null!==e)if(c=[],"string"==typeof e){if(o=e.split(_.tokenSeparator),_.verbose&&r("---------\nKey:",t),_.verbose&&r("Record:",o),this.options.tokenize){for(v=0;v0){if(e={item:o.item},p.indexOf("matches")!==-1)for(n=o.output,e.matches=[],r=0;rP.maxPatternLength){if(v=t.match(new RegExp(this.pattern.replace(P.tokenSeparator,"|"))),m=!!v)for(w=[],e=0,g=v.length;e=p;r--)if(b=this.patternAlphabet[t.charAt(r-1)],b&&(_[r-1]=1),0===e?f[r]=(f[r+1]<<1|1)&b:f[r]=(f[r+1]<<1|1)&b|((l[r+1]|l[r])<<1|1)|l[r+1],f[r]&this.matchmask&&(y=this._bitapScore(e,r-1),y<=i)){if(i=y,o=r-1,d.push(o),!(o>s))break;p=Math.max(1,2*s-o)}if(this._bitapScore(e+1,s)>i)break;l=f}return w=this._getMatchedIndices(_),{isMatch:o>=0,score:0===y?.001:y,matchedIndices:w}},o.prototype._getMatchedIndices=function(t){for(var e,r=[],n=-1,s=-1,i=0,o=t.length;i - {items} - - ); } + return ( + + {items} + + ); } } diff --git a/test/FilterResults_test.js b/test/FilterResults_test.js index 989b5b3..49de4d1 100644 --- a/test/FilterResults_test.js +++ b/test/FilterResults_test.js @@ -98,6 +98,31 @@ describe('FilterResults', () => { expect(component.find('.wrapper').find('.my-item').length).toEqual(4); expect(component.find('.wrapper').find('.wrapper__val').text()).toEqual('hello'); }); + + it('allows renderContainer function to describe how container will be rendered', () => { + function renderContainer(items) { + return ( +
+
+ {items} +
+
+ ); + } + const component = mount( + + ); + expect(component.find('.wrapper').length).toEqual(1); + expect(component.find('.wrapper').find('.react-fuzzy-filter__results-container').length).toEqual(0); + expect(component.find('.wrapper').find('.my-item').length).toEqual(4); + expect(component.find('.wrapper').find('.wrapper-before').length).toEqual(1); + expect(component.find('.wrapper').find('.wrapper-after').length).toEqual(1); + }); }); describe('#renderItems', () => {