From af00c3c8cd41a40ade21b819468e872fa58b5212 Mon Sep 17 00:00:00 2001 From: Tim Branyen Date: Wed, 21 Sep 2016 12:05:16 -0700 Subject: [PATCH 1/4] Rebase w/ latest code --- dist/hyperlist.js | 82 ++++++++++++++++++++++++++-- examples/react-example.html | 75 ++++++++++---------------- lib/index.js | 104 ++++++++++++++++++++++++++++-------- package.json | 4 +- test/render.js | 2 +- 5 files changed, 192 insertions(+), 75 deletions(-) diff --git a/dist/hyperlist.js b/dist/hyperlist.js index 289273f..910321c 100644 --- a/dist/hyperlist.js +++ b/dist/hyperlist.js @@ -129,15 +129,25 @@ var HyperList = function () { Object.assign(this._config, defaultConfig, userProvidedConfig); - if (!element || element.nodeType !== 1) { + var config = this[_config]; + + if (!config.isReact && (!element || element.nodeType !== 1)) { throw new Error('HyperList requires a valid DOM Node container'); } +<<<<<<< HEAD this._element = element; var config = this._config; var scroller = this._scroller || config.scroller || document.createElement(config.scrollerTagName || 'tr'); +======= + this[_element] = config.isReact ? {} : element; + + var scroller = this[_scroller] || config.scroller || config.isReact ? { + key: 'scroller' + } : document.createElement(config.scrollerTagName || 'tr'); +>>>>>>> f087f4e... Some initial work on it... // Default configuration option `useFragment` to `true`. if (typeof config.useFragment !== 'boolean') { @@ -190,6 +200,7 @@ var HyperList = function () { } }); +<<<<<<< HEAD // Decorate the container element with styles that will match // the user supplied configuration. var elementStyle = { @@ -200,6 +211,23 @@ var HyperList = function () { }; HyperList.mergeStyle(element, elementStyle); +======= + // If using React, the element object turns into a props object. + if (config.isReact) { + this[_element].style = { + width: '' + config.width, + height: '' + config.height, + overflow: 'auto', + position: 'relative', + padding: '0px' + }; + } + // Decorate the container element with inline styles that will match + // the user supplied configuration. + else if (!config.isReact) { + element.setAttribute('style', '\n width: ' + config.width + ';\n height: ' + config.height + ';\n overflow: auto;\n position: relative;\n padding: 0px;\n '); + } +>>>>>>> f087f4e... Some initial work on it... var scrollerHeight = config.itemHeight * config.total; var maxElementHeight = this._maxElementHeight; @@ -208,6 +236,7 @@ var HyperList = function () { console.warn(['HyperList: The maximum element height', maxElementHeight + 'px has', 'been exceeded; please reduce your item height.'].join(' ')); } +<<<<<<< HEAD var scrollerStyle = { opacity: '0', position: 'absolute', @@ -220,6 +249,22 @@ var HyperList = function () { // Only append the scroller element once. if (!this._scroller) { element.appendChild(scroller); +======= + if (config.isReact) { + scroller.style = Object.assign({}, scroller.style, { + opacity: 0, + position: 'absolute', + width: '1px', + height: scrollerHeight + 'px' + }); + } else { + scroller.setAttribute('style', '\n opacity: 0;\n position: absolute;\n width: 1px;\n height: ' + scrollerHeight + 'px;\n '); + + // Only append the scroller element once. + if (!this[_scroller]) { + element.appendChild(scroller); + } +>>>>>>> f087f4e... Some initial work on it... } // Set the scroller instance. @@ -259,19 +304,44 @@ var HyperList = function () { height = this._itemHeights[i]; } - if (!item || item.nodeType !== 1) { + var offsetTop = i * itemHeight; + var top = reverse ? (total - 1) * itemHeight - offsetTop : offsetTop; + + // Check for valid DOM nodes, unless using React. + if (!config.isReact && (!item || item.nodeType !== 1)) { throw new Error('Generator did not return a DOM Node for index: ' + i); } + // Is not a React element. + else if (config.isReact && !item) { + throw new Error('Generator did not return a React Element for index: ' + i); + } + // Is a React element. + else if (config.isReact) { + var _oldClass = item.props.className || ''; + + return React.cloneElement(item, { + key: i, + className: _oldClass + ' ' + (config.rowClassName || 'vrow'), + style: Object.assign({}, item.props.style, { + position: 'absolute', + top: top + 'px' + }) + }); + } var oldClass = item.getAttribute('class') || ''; item.setAttribute('class', oldClass + ' ' + (config.rowClassName || 'vrow')); +<<<<<<< HEAD var top = this._itemPositions[i]; HyperList.mergeStyle(item, { position: 'absolute', top: top + 'px' }); +======= + item.setAttribute('style', '\n ' + (item.style.cssText || '') + '\n position: absolute;\n top: ' + top + 'px\n '); +>>>>>>> f087f4e... Some initial work on it... return item; } @@ -323,7 +393,13 @@ var HyperList = function () { ;var scroller = this._scroller; // Keep the scroller in the list of children. +<<<<<<< HEAD fragment[config.useFragment ? 'appendChild' : 'push'](scroller); +======= + if (!config.isReact) { + fragment[config.useFragment ? 'appendChild' : 'push'](this[_scroller]); + } +>>>>>>> f087f4e... Some initial work on it... for (var i = from; i < to; i++) { var row = this._getRow(i); @@ -332,7 +408,7 @@ var HyperList = function () { } if (config.applyPatch) { - return config.applyPatch(element, fragment); + return config.applyPatch(element, fragment, scroller); } element.innerHTML = ''; diff --git a/examples/react-example.html b/examples/react-example.html index fc726e6..23fb7d0 100644 --- a/examples/react-example.html +++ b/examples/react-example.html @@ -60,59 +60,37 @@ - - - - - - diff --git a/lib/index.js b/lib/index.js index 4970989..269416d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -210,8 +210,6 @@ export default class HyperList { height: `${scrollerHeight}px` } - HyperList.mergeStyle(scroller, scrollerStyle) - if (config.isReact) { scroller.style = Object.assign({}, scroller.style, { opacity: 0, @@ -220,12 +218,7 @@ export default class HyperList { height: `${scrollerHeight}px` }) } else { - scroller.setAttribute('style', ` - opacity: 0; - position: absolute; - width: 1px; - height: ${scrollerHeight}px; - `) + HyperList.mergeStyle(scroller, scrollerStyle) // Only append the scroller element once. if (!this._scroller) { diff --git a/package.json b/package.json index eecfc63..b7f6cef 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,6 @@ "istanbul": "1.0.0-alpha.2", "mocha": "^2.4.5", "moment": "^2.13.0", - "react": "^15.3.2", - "react-dom": "^15.3.2", - "react-faux-dom": "^2.5.0", "standard": "^10.0.2", "stringdom": "jugglinmike/stringdom#ec8f199df36157330148b7a2e64b38c1f5008702", "watchify": "^3.7.0" @@ -40,7 +37,6 @@ "url": "git://github.com/tbranyen/hyperlist.git" }, "scripts": { - "build-faux-dom": "browserify -s ReactFauxDOM -r react-faux-dom -o dist/react-faux-dom.js", "build": "browserify -t [ babelify ] -s HyperList lib/index.js | derequire > dist/hyperlist.js", "watch": "watchify -t [ babelify ] -s HyperList lib/index.js -o 'derequire > dist/hyperlist.js' -v", "test": "standard --env mocha && babel-node ./node_modules/istanbul/lib/cli.js cover _mocha -- -R spec" From 18e8328efdd5a44572e91c7b0211973c5b98edfe Mon Sep 17 00:00:00 2001 From: Tim Branyen Date: Wed, 28 Jun 2017 16:04:16 -0700 Subject: [PATCH 4/4] Remove React test --- test/react.js | 63 --------------------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 test/react.js diff --git a/test/react.js b/test/react.js deleted file mode 100644 index 81d8444..0000000 --- a/test/react.js +++ /dev/null @@ -1,63 +0,0 @@ -import './_setup' -import assert from 'assert' -import React from 'react' -import ReactFauxDOM from 'react-faux-dom' -import ReactDOMServer from 'react-dom/server' -import HyperList from '../lib/index' - -describe('React', function () { - beforeEach(() => { - this.fixture = document.createElement('div') - }) - - afterEach(() => { - if (this.actual) { - this.actual.destroy() - } - }) - - it('can render a small amount of items to react', (done) => { - const reactContainer = ReactFauxDOM.createElement('div') - - this.actual = new HyperList(reactContainer, { - generate (i) { - const el = ReactFauxDOM.createElement('span') - el.innerHTML = i - return el - }, - - applyPatch (element, fragment) { - fragment.forEach(childNode => element.appendChild(childNode)) - }, - - overrideScrollPosition () { - return 0 - }, - - height: 5, - useFragment: false, - scroller: ReactFauxDOM.createElement('div'), - itemHeight: 1, - total: 3 - }) - - class ViewportScroller extends React.Component { - render () { - return reactContainer.toReact() - } - } - - window.requestAnimationFrame(() => { - const renderedString = ReactDOMServer.renderToString( - React.createElement(ViewportScroller) - ) - - const actual = renderedString - - const expected = '
12
' - - assert.equal(actual, expected) - done() - }) - }) -})