├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── license ├── package.json ├── readme.md ├── src └── index.js └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | dist 4 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '8' 4 | - '6' 5 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Vadim Demedes (github.com/vadimdemedes) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ink-redux", 3 | "version": "1.0.0", 4 | "description": "Redux bindings for Ink", 5 | "license": "MIT", 6 | "repository": "vadimdemedes/ink-redux", 7 | "author": { 8 | "name": "Vadim Demedes", 9 | "email": "vdemedes@gmail.com", 10 | "url": "github.com/vadimdemedes" 11 | }, 12 | "main": "dist/index.js", 13 | "engines": { 14 | "node": ">=6" 15 | }, 16 | "scripts": { 17 | "pretest": "npm run build", 18 | "test": "xo && ava", 19 | "build": "babel src --out-dir=dist", 20 | "prepublish": "npm run build" 21 | }, 22 | "files": [ 23 | "dist" 24 | ], 25 | "keywords": [ 26 | "ink", 27 | "redux" 28 | ], 29 | "devDependencies": { 30 | "ava": "^0.20.0", 31 | "babel-cli": "^6.24.1", 32 | "babel-plugin-transform-react-jsx": "^6.24.1", 33 | "eslint-config-xo-react": "^0.13.0", 34 | "eslint-plugin-react": "^7.1.0", 35 | "ink": "^0.1.2", 36 | "redux": "^3.7.1", 37 | "sinon": "^2.3.6", 38 | "strip-ansi": "^4.0.0", 39 | "xo": "^0.18.2" 40 | }, 41 | "babel": { 42 | "presets": [ 43 | "@ava/stage-4" 44 | ], 45 | "plugins": [ 46 | [ 47 | "transform-react-jsx", 48 | { 49 | "pragma": "h", 50 | "useBuiltIns": true 51 | } 52 | ] 53 | ] 54 | }, 55 | "ava": { 56 | "babel": { 57 | "plugins": [ 58 | [ 59 | "transform-react-jsx", 60 | { 61 | "pragma": "h", 62 | "useBuiltIns": true 63 | } 64 | ] 65 | ] 66 | } 67 | }, 68 | "xo": { 69 | "extends": [ 70 | "xo-react" 71 | ], 72 | "rules": { 73 | "react/prop-types": 0 74 | }, 75 | "settings": { 76 | "react": { 77 | "pragma": "h" 78 | } 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ink-redux [![Build Status](https://travis-ci.org/vadimdemedes/ink-redux.svg?branch=master)](https://travis-ci.org/vadimdemedes/ink-redux) 2 | 3 | > Redux bindings for [Ink](https://github.com/vadimdemedes/ink). 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install redux ink-redux 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```jsx 16 | const {h, render, Component} = require('ink'); 17 | const {Provider, connect} = require('ink-redux'); 18 | const {createStore} = require('redux'); 19 | 20 | const store = createStore((state = 0, action) => { 21 | switch (action.type) { 22 | case 'INCREMENT': return state + 1; 23 | default: return state; 24 | } 25 | }); 26 | 27 | class Counter extends Component { 28 | render(props) { 29 | return `Counter: ${props.counter}`; 30 | } 31 | 32 | componentDidMount() { 33 | this.timer = setInterval(this.props.onIncrement, 100); 34 | } 35 | 36 | componentWillUnmount() { 37 | clearInterval(this.timer); 38 | } 39 | } 40 | 41 | const mapStateToProps = state => ({ 42 | counter: state 43 | }); 44 | 45 | const mapDispatchToProps = { 46 | onIncrement: () => ({type: 'INCREMENT'}) 47 | }; 48 | 49 | const ConnectedCounter = connect(mapStateToProps, mapDispatchToProps)(Counter); 50 | 51 | render(( 52 | 53 | 54 | 55 | )); 56 | ``` 57 | 58 | 59 | ## API 60 | 61 | See [react-redux](https://github.com/reactjs/react-redux) for documentation. 62 | 63 | ### Differences 64 | 65 | - Matches API of `redux@4` 66 | - Doesn't support `options` in `connect()` 67 | - Doesn't support returning a function from `mapStateToProps()` 68 | 69 | 70 | ## License 71 | 72 | MIT © [Vadim Demedes](https://github.com/vadimdemedes) 73 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const {bindActionCreators} = require('redux'); 4 | const {h, Component} = require('ink'); 5 | 6 | class Provider extends Component { 7 | render({children}) { 8 | return children; 9 | } 10 | 11 | getChildContext() { 12 | return { 13 | store: this.props.store 14 | }; 15 | } 16 | } 17 | 18 | exports.Provider = Provider; 19 | 20 | const defaultMergeProps = (stateProps, dispatchProps, ownProps) => { 21 | return Object.assign({}, ownProps, stateProps, dispatchProps); 22 | }; 23 | 24 | const connect = (mapStateToProps, mapDispatchToProps, mergeProps = defaultMergeProps) => { 25 | return WrappedComponent => { 26 | class ConnectedComponent extends Component { 27 | render(ownProps, state, context) { 28 | const {dispatch, getState} = context.store; 29 | let stateProps; 30 | let dispatchProps; 31 | 32 | if (typeof mapStateToProps === 'function') { 33 | stateProps = mapStateToProps(getState(), ownProps); 34 | } 35 | 36 | if (mapDispatchToProps) { 37 | if (typeof mapDispatchToProps === 'function') { 38 | dispatchProps = mapDispatchToProps(dispatch, ownProps); 39 | } 40 | 41 | if (typeof mapDispatchToProps === 'object') { 42 | dispatchProps = bindActionCreators(mapDispatchToProps, dispatch); 43 | } 44 | } else { 45 | dispatchProps = {dispatch}; 46 | } 47 | 48 | return ; 49 | } 50 | 51 | componentDidMount() { 52 | if (typeof mapStateToProps === 'function') { 53 | this.unsubscribe = this.context.store.subscribe(() => { 54 | this.forceUpdate(); 55 | }); 56 | } 57 | } 58 | 59 | componentWillUnmount() { 60 | if (typeof this.unsubscribe === 'function') { 61 | this.unsubscribe(); 62 | } 63 | } 64 | } 65 | 66 | return ConnectedComponent; 67 | }; 68 | }; 69 | 70 | exports.connect = connect; 71 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import {h, render, renderToString, mount, Component} from 'ink'; 2 | import {createStore, combineReducers} from 'redux'; 3 | import stripAnsi from 'strip-ansi'; 4 | import {spy} from 'sinon'; 5 | import test from 'ava'; 6 | import {Provider, connect} from '.'; 7 | 8 | const counterReducer = (state = 0, action) => { 9 | switch (action.type) { 10 | case 'INCREMENT': return state + 1; 11 | default: return state; 12 | } 13 | }; 14 | 15 | const reducer = combineReducers({ 16 | counter: counterReducer 17 | }); 18 | 19 | const createTestStore = () => createStore(reducer); 20 | 21 | test('provider', t => { 22 | t.plan(1); 23 | 24 | const store = {}; 25 | 26 | const Child = (props, context) => { 27 | t.is(context.store, store); 28 | 29 | return 'Test'; 30 | }; 31 | 32 | render(( 33 | 34 | 35 | 36 | )); 37 | }); 38 | 39 | test('pass props to child component', t => { 40 | const store = createTestStore(); 41 | 42 | const Child = props => `Hello, ${props.name}`; 43 | const ConnectedChild = connect()(Child); 44 | 45 | const tree = render(( 46 | 47 | 48 | 49 | )); 50 | 51 | t.is(renderToString(tree), 'Hello, Chandler'); 52 | }); 53 | 54 | test.cb('subscribe to store and rerender on changes', t => { 55 | const store = createTestStore(); 56 | 57 | const Child = props => `Iteration #${props.iteration}`; 58 | 59 | const mapStateToProps = state => ({ 60 | iteration: state.counter 61 | }); 62 | 63 | const ConnectedChild = connect(mapStateToProps)(Child); 64 | 65 | const stream = {write: spy()}; 66 | const unmount = mount(( 67 | 68 | 69 | 70 | ), stream); 71 | 72 | t.is(stripAnsi(stream.write.firstCall.args[0]), 'Iteration #0\n'); 73 | 74 | store.dispatch({type: 'INCREMENT'}); 75 | 76 | process.nextTick(() => { 77 | unmount(); 78 | 79 | t.is(stripAnsi(stream.write.secondCall.args[0]), 'Iteration #1\n'); 80 | t.end(); 81 | }); 82 | }); 83 | 84 | test('pass store.dispatch() to child component', t => { 85 | const store = createTestStore(); 86 | 87 | class Child extends Component { 88 | render() { 89 | return 'Test'; 90 | } 91 | 92 | componentDidMount() { 93 | this.props.dispatch({type: 'INCREMENT'}); 94 | } 95 | } 96 | 97 | const ConnectedChild = connect()(Child); 98 | 99 | render(( 100 | 101 | 102 | 103 | )); 104 | 105 | t.deepEqual(store.getState(), {counter: 1}); 106 | }); 107 | 108 | test('bind dispatch props', t => { 109 | const store = createTestStore(); 110 | 111 | class Child extends Component { 112 | render() { 113 | return 'Test'; 114 | } 115 | 116 | componentDidMount() { 117 | this.props.onIncrement(); 118 | } 119 | } 120 | 121 | const mapDispatchToProps = { 122 | onIncrement: () => ({type: 'INCREMENT'}) 123 | }; 124 | 125 | const ConnectedChild = connect(null, mapDispatchToProps)(Child); 126 | 127 | render(( 128 | 129 | 130 | 131 | )); 132 | 133 | t.deepEqual(store.getState(), {counter: 1}); 134 | }); 135 | 136 | test('pass custom dispatch props', t => { 137 | const store = createTestStore(); 138 | spy(store, 'dispatch'); 139 | 140 | class Child extends Component { 141 | render() { 142 | return 'Test'; 143 | } 144 | 145 | componentDidMount() { 146 | this.props.onIncrement(); 147 | } 148 | } 149 | 150 | const mapDispatchToProps = (dispatch, ownProps) => { 151 | return { 152 | onIncrement: () => { 153 | dispatch({ 154 | type: 'INCREMENT', 155 | message: ownProps.message 156 | }); 157 | } 158 | }; 159 | }; 160 | 161 | const ConnectedChild = connect(null, mapDispatchToProps)(Child); 162 | 163 | render(( 164 | 165 | 166 | 167 | )); 168 | 169 | t.deepEqual(store.getState(), {counter: 1}); 170 | t.deepEqual(store.dispatch.firstCall.args[0], { 171 | type: 'INCREMENT', 172 | message: 'Hello' 173 | }); 174 | }); 175 | 176 | test('overwrite own props', t => { 177 | const store = createTestStore(); 178 | 179 | class Child extends Component { 180 | render(props) { 181 | return `Iteration #${props.counter}`; 182 | } 183 | 184 | componentDidMount() { 185 | this.props.onIncrement(); 186 | } 187 | } 188 | 189 | const mapStateToProps = state => ({ 190 | counter: state.counter 191 | }); 192 | 193 | const mapDispatchToProps = { 194 | onIncrement: () => ({type: 'INCREMENT'}) 195 | }; 196 | 197 | const ConnectedChild = connect(mapStateToProps, mapDispatchToProps)(Child); 198 | 199 | const fakeOnIncrement = spy(); 200 | 201 | const tree = render(( 202 | 203 | 204 | 205 | )); 206 | 207 | t.is(renderToString(tree), 'Iteration #0'); 208 | t.false(fakeOnIncrement.called); 209 | t.deepEqual(store.getState(), {counter: 1}); 210 | }); 211 | 212 | test('custom merge function', t => { 213 | const store = createTestStore(); 214 | 215 | class Child extends Component { 216 | render(props) { 217 | return `Iteration #${props.counter}`; 218 | } 219 | 220 | componentDidMount() { 221 | this.props.onIncrement(); 222 | } 223 | } 224 | 225 | const mapStateToProps = state => ({ 226 | counter: state.counter 227 | }); 228 | 229 | const mapDispatchToProps = { 230 | onIncrement: () => ({type: 'INCREMENT'}) 231 | }; 232 | 233 | const mergeProps = (stateProps, dispatchProps, ownProps) => { 234 | return Object.assign({}, stateProps, dispatchProps, ownProps); 235 | }; 236 | 237 | const ConnectedChild = connect(mapStateToProps, mapDispatchToProps, mergeProps)(Child); 238 | 239 | const fakeOnIncrement = spy(); 240 | 241 | const tree = render(( 242 | 243 | 244 | 245 | )); 246 | 247 | t.is(renderToString(tree), 'Iteration #10'); 248 | t.true(fakeOnIncrement.calledOnce); 249 | t.deepEqual(store.getState(), {counter: 0}); 250 | }); 251 | --------------------------------------------------------------------------------