├── .gitignore ├── .travis.yml ├── README.md ├── lerna.json ├── package.json ├── packages ├── vue-compose │ ├── .babelrc │ ├── .eslintrc.js │ ├── .npmignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── changelog.md │ ├── package.json │ ├── rollup.config.js │ ├── spec │ │ ├── acceptProps.spec.js │ │ ├── branch.spec.js │ │ ├── componentFromProp.spec.js │ │ ├── componentFromSlot.spec.js │ │ ├── compose.spec.js │ │ ├── createSink.spec.js │ │ ├── defaultProps.spec.js │ │ ├── hooks.js │ │ ├── mapProps.spec.js │ │ ├── provideInject.spec.js │ │ ├── vuex.spec.js │ │ ├── withClass.spec.js │ │ ├── withComputed.spec.js │ │ ├── withData.spec.js │ │ ├── withHandlers.spec.js │ │ ├── withHooks.spec.js │ │ ├── withMethods.spec.js │ │ ├── withNativeHandlers.spec.js │ │ ├── withPassive.spec.js │ │ ├── withProps.spec.js │ │ └── withStyle.spec.js │ └── src │ │ ├── hocs │ │ ├── acceptProps.js │ │ ├── branch.js │ │ ├── defaultProps.js │ │ ├── mapProps.js │ │ ├── provide.js │ │ ├── withClass.js │ │ ├── withData.js │ │ ├── withHandlers.js │ │ ├── withHooks.js │ │ ├── withNativeHandlers.js │ │ ├── withPassive.js │ │ ├── withProps.js │ │ └── withStyle.js │ │ ├── index.js │ │ ├── mutators │ │ ├── inject.js │ │ ├── setName.js │ │ ├── withComputed.js │ │ └── withMethods.js │ │ └── utils │ │ ├── assign.js │ │ ├── componentFromProp.js │ │ ├── componentFromSlot.js │ │ ├── compose.js │ │ ├── createSink.js │ │ ├── getMixins.js │ │ └── renderNothing.js ├── vue-hoc │ ├── .babelrc │ ├── .eslintrc.js │ ├── .npmignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── changelog.md │ ├── package.json │ ├── rollup.config.js │ ├── spec │ │ ├── createHOC.spec.js │ │ ├── events.spec.js │ │ ├── functional.spec.js │ │ ├── hooks.js │ │ ├── hooks.spec.js │ │ ├── props.spec.js │ │ ├── slots.spec.js │ │ └── templateSlots.spec.js │ └── src │ │ ├── constants.js │ │ ├── createHOC.js │ │ ├── createRenderFn.js │ │ ├── getComponentOptions.js │ │ ├── getProps.js │ │ ├── index.js │ │ ├── normalizeSlots.js │ │ └── utils.js └── vuex-compose │ ├── .babelrc │ ├── .eslintrc.js │ ├── .npmignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── package.json │ ├── rollup.config.js │ ├── spec │ ├── actions.spec.js │ ├── getters.spec.js │ ├── hooks.js │ ├── mapStateToProps.spec.js │ ├── mutations.spec.js │ └── registerModule.spec.js │ └── src │ ├── actions.js │ ├── connect.js │ ├── getters.js │ ├── index.js │ ├── mapStateToProps.js │ ├── mutations.js │ ├── registerModule.js │ └── utils.js ├── travis ├── login.sh └── push.sh └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | 3 | # Logs 4 | logs 5 | *.log 6 | npm-debug.log* 7 | yarn-debug.log* 8 | yarn-error.log* 9 | 10 | # Runtime data 11 | pids 12 | *.pid 13 | *.seed 14 | *.pid.lock 15 | 16 | # Directory for instrumented libs generated by jscoverage/JSCover 17 | lib-cov 18 | 19 | # Coverage directory used by tools like istanbul 20 | coverage 21 | 22 | # nyc test coverage 23 | .nyc_output 24 | 25 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 26 | .grunt 27 | 28 | # Bower dependency directory (https://bower.io/) 29 | bower_components 30 | 31 | # node-waf configuration 32 | .lock-wscript 33 | 34 | # Compiled binary addons (http://nodejs.org/api/addons.html) 35 | build/Release 36 | 37 | # Dependency directories 38 | node_modules/ 39 | jspm_packages/ 40 | 41 | # Typescript v1 declaration files 42 | typings/ 43 | 44 | # Optional npm cache directory 45 | .npm 46 | 47 | # Optional eslint cache 48 | .eslintcache 49 | 50 | # Optional REPL history 51 | .node_repl_history 52 | 53 | # Output of 'npm pack' 54 | *.tgz 55 | 56 | # Yarn Integrity file 57 | .yarn-integrity 58 | 59 | # dotenv environment variables file 60 | .env 61 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "8" 4 | install: 5 | - yarn install 6 | - yarn lerna run build 7 | script: 8 | - yarn lerna run lint 9 | - yarn lerna run test 10 | after_success: 11 | - yarn npm-cli-login 12 | - test $TRAVIS_PULL_REQUEST == "false" && test $TRAVIS_BRANCH == "master" && yarn run pre && yarn run publish && yarn run post && travis/push.sh 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vue HOC 2 | 3 | ## Packages 4 | - [vue-hoc](https://github.com/jackmellis/vue-hoc/blob/master/packages/vue-hoc/README.md) 5 | - [vue-compose](https://github.com/jackmellis/vue-hoc/blob/master/packages/vue-compose/README.md) 6 | - [vuex-compose](https://github.com/jackmellis/vue-hoc/blob/master/packages/vuex-compose/README.md) 7 | -------------------------------------------------------------------------------- /lerna.json: -------------------------------------------------------------------------------- 1 | { 2 | "lerna": "2.10.0", 3 | "packages": [ 4 | "packages/*" 5 | ], 6 | "version": "independent", 7 | "npmClient": "yarn" 8 | } 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-hoc-repo", 3 | "version": "0.0.0", 4 | "description": "", 5 | "main": ".eslintrc.js", 6 | "private": true, 7 | "workspaces": [ 8 | "packages/*" 9 | ], 10 | "scripts": { 11 | "pre": "mono-semantic --loglevel info --debug false pre", 12 | "publish": "mono-semantic --loglevel info --debug false publish", 13 | "post": "mono-semantic --loglevel info --debug false post" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/jackmellis/vue-hoc.git" 18 | }, 19 | "author": "Jack Ellis", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/jackmellis/vue-hoc/issues" 23 | }, 24 | "homepage": "https://github.com/jackmellis/vue-hoc#readme", 25 | "devDependencies": { 26 | "ava": "^0.25.0", 27 | "babel-cli": "^6.26.0", 28 | "babel-core": "^6.26.0", 29 | "babel-eslint": "^8.2.2", 30 | "babel-plugin-external-helpers": "^6.22.0", 31 | "babel-preset-env": "^1.6.1", 32 | "browser-env": "^3.2.5", 33 | "cross-env": "^5.1.4", 34 | "eslint": "^4.19.1", 35 | "eslint-plugin-import": "^2.10.0", 36 | "inspect-process": "^0.5.0", 37 | "lerna": "^2.10.0", 38 | "module-alias": "^2.0.6", 39 | "mono-semantic": "^0.1.0-beta.3", 40 | "npm-cli-login": "^0.0.10", 41 | "nyc": "^11.6.0", 42 | "require-extension-hooks": "^0.3.2", 43 | "require-extension-hooks-babel": "^0.1.1", 44 | "rollup": "^0.57.1", 45 | "rollup-plugin-babel": "^3.0.3", 46 | "sinon": "^4.5.0", 47 | "vue": "^2.5.16", 48 | "vuenit": "^1.1.2", 49 | "vuex": "^3.0.1" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /packages/vue-compose/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "production": { 4 | "presets" : [ 5 | ["env", { 6 | "targets" : { 7 | "browsers" : ["last 2 versions"], 8 | "node" : "current", 9 | }, 10 | "modules": false 11 | }] 12 | ], 13 | "sourceMaps" : true, 14 | "plugins": ["external-helpers"], 15 | }, 16 | "test": { 17 | "presets" : [ 18 | ["env", { 19 | "targets" : { 20 | "browsers" : ["last 2 versions"], 21 | "node" : "current", 22 | }, 23 | "modules": "commonjs" 24 | }] 25 | ], 26 | "sourceMaps" : true, 27 | "plugins": [] 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /packages/vue-compose/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "parser": "babel-eslint", 3 | "plugins": [], 4 | "env": { 5 | "browser": true, 6 | "es6": true 7 | }, 8 | "extends": [ 9 | "eslint:recommended", 10 | "plugin:import/errors", 11 | "plugin:import/warnings", 12 | ], 13 | "parserOptions": { 14 | "sourceType": "module" 15 | }, 16 | "rules": { 17 | "indent": [ 18 | "error", 19 | 2 20 | ], 21 | "linebreak-style": [ 22 | "error", 23 | "unix" 24 | ], 25 | "quotes": [ 26 | "error", 27 | "single" 28 | ], 29 | "semi": [ 30 | "error", 31 | "always" 32 | ] 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /packages/vue-compose/.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | spec 3 | 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | yarn-debug.log* 9 | yarn-error.log* 10 | 11 | # Runtime data 12 | pids 13 | *.pid 14 | *.seed 15 | *.pid.lock 16 | 17 | # Directory for instrumented libs generated by jscoverage/JSCover 18 | lib-cov 19 | 20 | # Coverage directory used by tools like istanbul 21 | coverage 22 | 23 | # nyc test coverage 24 | .nyc_output 25 | 26 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 27 | .grunt 28 | 29 | # Bower dependency directory (https://bower.io/) 30 | bower_components 31 | 32 | # node-waf configuration 33 | .lock-wscript 34 | 35 | # Compiled binary addons (http://nodejs.org/api/addons.html) 36 | build/Release 37 | 38 | # Dependency directories 39 | node_modules/ 40 | jspm_packages/ 41 | 42 | # Typescript v1 declaration files 43 | typings/ 44 | 45 | # Optional npm cache directory 46 | .npm 47 | 48 | # Optional eslint cache 49 | .eslintcache 50 | 51 | # Optional REPL history 52 | .node_repl_history 53 | 54 | # Output of 'npm pack' 55 | *.tgz 56 | 57 | # Yarn Integrity file 58 | .yarn-integrity 59 | 60 | # dotenv environment variables file 61 | .env 62 | -------------------------------------------------------------------------------- /packages/vue-compose/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | ## [0.7.1](https://github.com/jackmellis/vue-hoc/compare/0.3.0...0.7.1) (2018-10-16) 3 | 4 | 5 | ### Bug Fixes 6 | 7 | * **vue-compose:** componentFromSlot now accepts Component slots ([f483bc3](https://github.com/jackmellis/vue-hoc/commit/f483bc3)) 8 | * **vue-compose:** fix readme ([9e19b8e](https://github.com/jackmellis/vue-hoc/commit/9e19b8e)) 9 | * **vue-compose:** missing build ([a881bfc](https://github.com/jackmellis/vue-hoc/commit/a881bfc)) 10 | 11 | 12 | ### Features 13 | 14 | * **vue-compose:** add withNativeHandlers method ([6710991](https://github.com/jackmellis/vue-hoc/commit/6710991)), closes [#37](https://github.com/jackmellis/vue-hoc/issues/37) 15 | * **vue-compose:** branch ([fbc0f2b](https://github.com/jackmellis/vue-hoc/commit/fbc0f2b)) 16 | * **vue-compose:** componentFromSlot listens to events ([8852fdc](https://github.com/jackmellis/vue-hoc/commit/8852fdc)), closes [#27](https://github.com/jackmellis/vue-hoc/issues/27) 17 | * **vue-compose:** move to mono repo ([df2f3f4](https://github.com/jackmellis/vue-hoc/commit/df2f3f4)) 18 | * **vue-compose:** provide and inject hocs ([c25ece5](https://github.com/jackmellis/vue-hoc/commit/c25ece5)) 19 | * **vue-compose:** renderNothing ([a8c296e](https://github.com/jackmellis/vue-hoc/commit/a8c296e)) 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /packages/vue-compose/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /packages/vue-compose/README.md: -------------------------------------------------------------------------------- 1 | # vue-compose 2 | Create awesome Vue HOCs 3 | 4 | ## Installation 5 | ``` 6 | npm install --save vue-compose 7 | ``` 8 | 9 | ## Usage 10 | ```js 11 | import { 12 | compose, 13 | withProps, 14 | defaultProps, 15 | withHandlers, 16 | withData, 17 | setName, 18 | } from 'vue-compose'; 19 | import C from './component.vue'; 20 | 21 | const enhance = compose( 22 | withProps((props) => ({ 23 | someProp: 'foo', 24 | })), 25 | defaultProps({ 26 | notPassedIn: true 27 | }), 28 | withHandlers({ 29 | someEvent(e){ 30 | // do some stuff 31 | } 32 | }), 33 | withData({ 34 | someDataProp: { 35 | initialValue: 'bah', 36 | listener: 'updateBah', 37 | } 38 | }), 39 | setName('My Component'), 40 | ); 41 | 42 | const enhanced = enhance(C); 43 | ``` 44 | 45 | ## API 46 | 47 | ### HOC Creators 48 | The following methods will create a new HOC that wraps the provided component. 49 | #### mapProps 50 | ```js 51 | ( 52 | mapper: (props : Object) => Object, 53 | ) => (Component) => Component; 54 | ``` 55 | Maps the props into a new object. Any props that are not returned will **not** be passed into the base component. 56 | ```js 57 | mapProps((props) => ({ 58 | propA: props.propA, 59 | propB: props.propB + 1, 60 | })); 61 | ``` 62 | 63 | #### withProps 64 | ```js 65 | ( 66 | mapper: { 67 | [propName: string]: Object | (props: Object) => any 68 | } | (props : Object) => Object, 69 | ) => (Component) => Component; 70 | ``` 71 | Maps props into a new object. The returned object will be merged with the existing props. 72 | ```js 73 | withProps({ 74 | someStaticProp: 'foo' 75 | }); 76 | 77 | withProps({ 78 | someDynamicProp: (props) => props.propA + 1 79 | }); 80 | 81 | withProps((props) => ({ 82 | someDynamicProp: props.propA + 1 83 | })) 84 | ``` 85 | 86 | #### defaultProps 87 | ```js 88 | ( 89 | defaults: Object, 90 | ) => (Component) => Component; 91 | ``` 92 | Sets default values for any props that are currently undefined. 93 | ```js 94 | defaultProps({ 95 | someProp: 'bah' 96 | }); 97 | ``` 98 | 99 | #### acceptProps 100 | ```js 101 | ( 102 | props: Object | Array, 103 | ) => (Component) => Component; 104 | ``` 105 | Adds additional props to the component's props option. This allows you to accept props that aren't expected by the base component. 106 | ```js 107 | compose( 108 | mapProps((props) => ({ 109 | someOldProp: props.someNewProp 110 | })), 111 | acceptProps([ 'someNewProp' ]), 112 | ); 113 | ``` 114 | 115 | #### withHandlers 116 | ```js 117 | ( 118 | listeners: { 119 | [handlerName: string]: (...args: Array) => any 120 | }, 121 | ) => (Component) => Component; 122 | ``` 123 | Adds event listeners to the base component's `v-on` attribute. 124 | ```js 125 | withHandlers({ 126 | someEvent(arg){ 127 | // do some event stuff 128 | } 129 | }); 130 | ``` 131 | If you add a listener, that event won't propagate any further. If you want to intercept an event but still propagate it, you will need to re-emit the event: 132 | ```js 133 | withHandlers({ 134 | someEvent(arg){ 135 | // do some event stuff 136 | this.$emit('someEvent', arg); 137 | } 138 | }); 139 | ``` 140 | Handlers can access eachother with the `handle` prefix, meaning you can trigger one handler from another: 141 | ```js 142 | withHandlers({ 143 | click(e){ 144 | /* ... */ 145 | }, 146 | dblClick(e){ 147 | // do something 148 | this.handleClick(e); 149 | } 150 | }) 151 | ``` 152 | 153 | #### withPassive 154 | ```js 155 | ( 156 | listeners: { 157 | [handlerName: string]: (...args: Array) => any 158 | }, 159 | ) => (Component) => Component; 160 | ``` 161 | Just like `withHandlers` except the event is automatically propagated up. 162 | 163 | #### withNativeHandlers 164 | ```js 165 | ( 166 | listeners: { 167 | [handlerName: string]: (...args: Array) => any 168 | }, 169 | ) => (Component) => Component; 170 | ``` 171 | Adds native DOM even listeners to the component. 172 | 173 | #### withData 174 | ```js 175 | ( 176 | data: { 177 | [name: string]: { 178 | prop?: string, 179 | listener?: string, 180 | handler?: (...args: any) => any, 181 | initialValue?: any | (props: Object) => any, 182 | } 183 | }, 184 | ) => (Component) => Component; 185 | ``` 186 | Creates a stateful HOC with the specified data properties. 187 | ##### name 188 | The name of the data property. Note that if this conflicts with an incoming prop, it will kill the prop. 189 | ##### prop 190 | The name of the prop that the data will be passed into. By default it is the same as `name`. 191 | ##### listener 192 | The name of the event that will trigger the update handler. By default it is the same as `name`. 193 | ##### handler 194 | A function that handles updating the data value when the `listener` event is emitted. 195 | ##### initialValue 196 | The initial value for the data property. If it is a function, it will be evaluated when the HOC is instantiated. 197 | 198 | Example with the default options: 199 | ```js 200 | withData({ 201 | something: { 202 | prop: 'something', 203 | listener: 'something', 204 | handler(v){ 205 | this.something = v; 206 | }, 207 | initialValue: undefined 208 | } 209 | }); 210 | ``` 211 | 212 | #### withHooks 213 | ```js 214 | ( 215 | hooks: { 216 | [hookName: string]: Function, 217 | }, 218 | ) => (Component) => Component; 219 | ``` 220 | Adds hooks to the component, i.e. `beforeCreate`, `created`, `mounted`, etc. 221 | ```js 222 | withHooks({ 223 | mounted(){ 224 | // some stuff here 225 | } 226 | }); 227 | ``` 228 | 229 | #### withClass 230 | ```js 231 | ( 232 | classes: any | (props: Object) => any, 233 | ) => (Component) => Component; 234 | ``` 235 | Adds classes to the base component. 236 | 237 | #### withStyle 238 | ```js 239 | ( 240 | style: any | (props: Object) => any, 241 | ) => (Component) => Component; 242 | ``` 243 | Adds styles to the base component. 244 | 245 | #### provide 246 | ```js 247 | ( 248 | provide: Object | () => Object 249 | ) => (Component) => Component; 250 | ``` 251 | Leverages Vue's `provide/inject` functionality. Use in conjunction with the `inject` method. 252 | 253 | #### branch 254 | ```js 255 | ( 256 | testFn: (props: Object) => boolean, 257 | trueFn: (h: Function) => vNode, 258 | falseFn?: (h: Function) => vNode, 259 | ) => (Component) => Component 260 | ``` 261 | Use an alternate render function based on a predecate function. For example, if you want to render a loading spinner based on a `loading` prop. If you don't provide a `falseFn`, it will fall back to using the component's original render function; this is the most common use case. 262 | 263 | Note that you must use render functions, it is not possible to insert template syntax here. This does mean, however, that you can still use `jsx` syntax. 264 | 265 | ### Mutators 266 | Mutators don't create a new HOC, but actually **mutate** the provided component with new attributes. 267 | #### withComputed 268 | ```js 269 | ( 270 | computed: { 271 | [name: string]: Function 272 | }, 273 | ) => (Component) => Component; 274 | ``` 275 | Adds computed properties to the component. 276 | ```js 277 | compose( 278 | withComputed({ 279 | compA(){ 280 | return 'foo'; 281 | } 282 | }), 283 | withProps({ 284 | propA(){ 285 | return this.compA; 286 | } 287 | }), 288 | ) 289 | ``` 290 | 291 | #### withMethods 292 | ```js 293 | ( 294 | methods: { 295 | [name: string]: Function 296 | }, 297 | ) => (Component) => Component; 298 | ``` 299 | Adds methods to the component. 300 | ```js 301 | compose( 302 | withMethods({ 303 | methodA(){ 304 | return 'foo'; 305 | } 306 | }), 307 | withProps({ 308 | propA(){ 309 | return this.methodA(); 310 | } 311 | }), 312 | ) 313 | ``` 314 | 315 | #### setName 316 | ```js 317 | ( 318 | name: string, 319 | ) => (Component) => Component; 320 | ``` 321 | Sets the name of the component. 322 | 323 | ### inject 324 | ```js 325 | ( 326 | inject: Array | Object 327 | ) => (Component) => Component; 328 | ``` 329 | Injects data into the component provided by `provide`. 330 | 331 | ### Utilities 332 | #### compose 333 | ```js 334 | ( 335 | ...hocCreators: Array 336 | ) => (ctor: Component) => Component; 337 | ``` 338 | Chain multiple HOCs together. Compose will combine the HOCs from right-to-left (or bottom-to-top). The result is a function that accepts a Component that will then be applied to all of the HOCs. 339 | 340 | #### componentFromProp 341 | ```js 342 | ( 343 | propName: string | Component, 344 | ) => Component; 345 | ``` 346 | Creates a component using the provided prop value. The prop can either be a string - such as `'input'` or a component. 347 | ```js 348 | const C = componentFromProp('component'); 349 | ``` 350 | You can then use it like: 351 | ```html 352 |
353 | 354 |
355 | ``` 356 | or: 357 | ```html 358 |
359 | 360 |
361 | ``` 362 | 363 | #### componentFromSlot 364 | ```js 365 | ( 366 | componentOptions?: Object 367 | ) => Component; 368 | ``` 369 | Creates a component that just outputs its slot content. This is useful if you want to apply styles etc. through a component, but don't want to render additional html elements. 370 | 371 | Any props passed to this component will be passed through to the slot component. 372 | 373 | > NOTE: This is still an experimental feature and should be used with caution. 374 | 375 | A couple of important notes: 376 | - There must only be one root element inside the compponent 377 | - For multiple root elements, use a `