Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override parent props #8

Merged
merged 5 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@dabapps/react-set-props",
"version": "1.0.0",
"version": "1.1.0",
"description": "Store arbitrary state in redux with a similar API to setState",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"prepublish": "npm test && npm run dist",
"dist": "rm -rf dist/ && mkdir -p dist/ && tsc --project src/tsconfig.json",
"tests": "jest",
"lint": "tslint --project tsconfig.json --type-check '{ts,tests,src}/**/*.{ts,tsx}'",
"lint": "tslint --project tsconfig.json '{ts,tests,src}/**/*.{ts,tsx}'",
"test": "npm run lint && npm run tests -- --coverage"
},
"repository": {
Expand Down Expand Up @@ -53,8 +53,8 @@
"react-dom": "15.5.4",
"react-test-renderer": "15.5.4",
"ts-jest": "20.0.3",
"tslint": "5.1.0",
"tslint-config-dabapps": "github:dabapps/tslint-config-dabapps"
"tslint": "5.8.0",
"tslint-config-dabapps": "github:dabapps/tslint-config-dabapps#v0.4.0"
},
"peerDependencies": {
"react": ">= 15",
Expand Down
26 changes: 13 additions & 13 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const SET_PROPS = 'SET_PROPS';
const CLEAR_PROPS = 'CLEAR_PROPS';
const SET_PROPS_SECRET_KEY = 'SET_PROPS_SECRET_KEY';

type Component<P> = React.ComponentClass<P> | React.StatelessComponent<P>;
type ComponentType<P> = React.ComponentClass<P> | React.StatelessComponent<P>;

export interface StringKeyedObject {
[index: string]: any;
Expand Down Expand Up @@ -57,7 +57,7 @@ export function clearPropsAction(id: string) {
return {
type: CLEAR_PROPS,
payload: {
id
id,
},
};
}
Expand All @@ -76,7 +76,7 @@ export function setPropsReducer(
...previous,
...action.payload.props,
},
__secretKey: SET_PROPS_SECRET_KEY
__secretKey: SET_PROPS_SECRET_KEY,
};
case CLEAR_PROPS:
const clearedState = {...state};
Expand All @@ -85,7 +85,7 @@ export function setPropsReducer(

return {
...clearedState,
__secretKey: SET_PROPS_SECRET_KEY
__secretKey: SET_PROPS_SECRET_KEY,
};
default:
return state;
Expand All @@ -94,7 +94,7 @@ export function setPropsReducer(

export function withSetProps<
Props extends StringKeyedObject,
ExternalProps extends StringKeyedObject = StringKeyedObject
OwnProps extends StringKeyedObject = StringKeyedObject
>(getInitialProps: (props: StringKeyedObject) => Props) {

const unconnected = (id: string) =>
Expand All @@ -110,27 +110,27 @@ export function withSetProps<
const storeProps = props[id];

return {
...storeProps,
// TODO: Remove casts when TS supports destructing extended types
...parentProps as any
...parentProps as any,
...storeProps,
};
});

return (Component: Component<SetPropsInterface<Props> & ExternalProps>) => {
return (Component: ComponentType<SetPropsInterface<Props> & OwnProps>) => {
return connect(
(state, props: ExternalProps): ExternalProps => props,
(state, props: OwnProps): OwnProps => props,
{
__setProps: setPropsAction,
__clearProps: clearPropsAction
__clearProps: clearPropsAction,
}
)
(
class SetPropsWrapper extends React.PureComponent<InternalSetPropsInterface<Props> & ExternalProps, void> {
class SetPropsWrapper extends React.PureComponent<InternalSetPropsInterface<Props> & OwnProps, void> {
private __id: string; // tslint:disable-line:variable-name
private Connected: Component<SetPropsInterface<Props>>;
private Connected: ComponentType<SetPropsInterface<Props>>;

public constructor(
inputProps: InternalSetPropsInterface<Props> & ExternalProps
inputProps: InternalSetPropsInterface<Props> & OwnProps
) {
super(inputProps);

Expand Down
41 changes: 21 additions & 20 deletions tests/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
setPropsAction,
SetPropsInterface,
setPropsReducer,
withSetProps
withSetProps,
} from '../src/';

describe('Set Props', () => {
Expand All @@ -27,6 +27,7 @@ describe('Set Props', () => {
return (
<p>
Count: {count}
{/* tslint:disable-next-line:jsx-no-lambda */}
<button onClick={() => setProps({count: count + 1})}>
{buttonText}
</button>
Expand All @@ -35,7 +36,7 @@ describe('Set Props', () => {
};

const getInitialProps = () => ({
count: 0
count: 0,
});

const SetPropsCounter = withSetProps<Props, ExternalProps>(getInitialProps)(Counter);
Expand Down Expand Up @@ -84,8 +85,8 @@ describe('Set Props', () => {

expect(testStore.getState()).toEqual({
setPropsReducer: {
__secretKey: 'SET_PROPS_SECRET_KEY'
}
__secretKey: 'SET_PROPS_SECRET_KEY',
},
});

expect(Object.keys((testStore.getState() as any).setPropsReducer).length).toEqual(1);
Expand Down Expand Up @@ -115,8 +116,8 @@ describe('Set Props', () => {

expect(testStore.getState()).toEqual({
setPropsReducer: {
__secretKey: 'SET_PROPS_SECRET_KEY'
}
__secretKey: 'SET_PROPS_SECRET_KEY',
},
});

expect(Object.keys((testStore.getState() as any).setPropsReducer).length).toEqual(1);
Expand All @@ -133,8 +134,8 @@ describe('Set Props', () => {

expect(testStore.getState()).toEqual({
setPropsReducer: {
__secretKey: 'SET_PROPS_SECRET_KEY'
}
__secretKey: 'SET_PROPS_SECRET_KEY',
},
});

const props = (testStore.getState() as any).setPropsReducer;
Expand Down Expand Up @@ -165,7 +166,7 @@ describe('Set Props', () => {
state = setPropsReducer(undefined, {type: 'Unknown'} as any);

expect(state).toEqual({
__secretKey: 'SET_PROPS_SECRET_KEY'
__secretKey: 'SET_PROPS_SECRET_KEY',
});
});

Expand All @@ -175,20 +176,20 @@ describe('Set Props', () => {
expect(state).toEqual({
__secretKey: 'SET_PROPS_SECRET_KEY',
id: {
foo: 'bar'
}
foo: 'bar',
},
});

state = setPropsReducer(state, setPropsAction('id2', {foo: 'bar'}));

expect(state).toEqual({
__secretKey: 'SET_PROPS_SECRET_KEY',
id: {
foo: 'bar'
foo: 'bar',
},
id2: {
foo: 'bar'
}
foo: 'bar',
},
});
});

Expand All @@ -199,11 +200,11 @@ describe('Set Props', () => {
__secretKey: 'SET_PROPS_SECRET_KEY',
id: {
foo: undefined,
baz: 'foo'
baz: 'foo',
},
id2: {
foo: 'bar'
}
foo: 'bar',
},
});
});

Expand All @@ -213,14 +214,14 @@ describe('Set Props', () => {
expect(state).toEqual({
__secretKey: 'SET_PROPS_SECRET_KEY',
id2: {
foo: 'bar'
}
foo: 'bar',
},
});

state = setPropsReducer(state, clearPropsAction('id2'));

expect(state).toEqual({
__secretKey: 'SET_PROPS_SECRET_KEY'
__secretKey: 'SET_PROPS_SECRET_KEY',
});
});

Expand Down