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

Typescript rewrite #28

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 15 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "react-dream",
"version": "0.5.1",
"name": "@brettm12345/react-dream",
"version": "0.5.8",
"description": "Fantasy Land type for React Components",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"dist": "babel src/ -d dist/ --ignore spec.js",
"build": "tsc",
"dist": "tsc",
"prepublishOnly": "npm run dist",
"test": "babel-node test"
},
Expand All @@ -29,6 +31,7 @@
"url": "https://github.com/xaviervia/react-dream/issues"
},
"homepage": "https://github.com/xaviervia/react-dream#readme",
"private": false,
"devDependencies": {
"@klarna/higher-order-components": "^3.0.1",
"babel-cli": "^6.24.1",
Expand All @@ -37,19 +40,22 @@
"babel-preset-stage-3": "^6.24.1",
"jsdom": "^11.12.0",
"jsdom-global": "^3.0.2",
"prettier": "^1.5.3",
"ramda": "^0.25.0",
"prettier": "^1.19.1",
"react": "^16.0.0",
"react-test-renderer": "^16.0.0",
"recompose": "^0.24.0",
"recompose": "^0.30.0",
"washington": "^2.0.0-rc.3"
},
"peerDependencies": {
"react": "^16.0.0",
"recompose": "^0.24.0"
"react": "^16.12.0",
"recompose": "^0.30.0"
},
"dependencies": {
"@hocs/with-debugger": "^0.1.0",
"@hocs/with-log": "^0.1.0"
"@hocs/with-log": "^0.1.0",
"@types/react": "^16.9.17",
"@types/recompose": "^0.30.7",
"fp-ts": "^2.4.1",
"typescript": "^3.7.5"
}
}
6 changes: 6 additions & 0 deletions src/@types/@hocs__with-debugger/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Hoc } from "src/types";

declare module "@hocs/with-debugger" {
const hoc: Hoc<any>;
export default hoc;
}
6 changes: 6 additions & 0 deletions src/@types/@hocs__with-log/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Hoc } from "@hocs/with-debugger/node_modules/src/types";

declare module "@hocs/with-log" {
const hoc: <P>(messageFromProps: (props: P) => string) => Hoc<P>;
export default hoc;
}
130 changes: 0 additions & 130 deletions src/ReactDream.js

This file was deleted.

126 changes: 126 additions & 0 deletions src/ReactDream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import setDisplayName from "recompose/setDisplayName";
import recomposeDefaultProps from "recompose/defaultProps";
import setPropTypes from "recompose/setPropTypes";
import { flow, Endomorphism } from "fp-ts/lib/function";
import doAp from "./internals/doAp";
import doConcat from "./internals/doConcat";
import doContramap from "./internals/doContramap";
import doMap from "./internals/doMap";
import doPromap from "./internals/doPromap";
import doRotate from "./internals/doRotate";
import doTranslate from "./internals/doTranslate";
import doScale from "./internals/doScale";
import styleFromProps from "./styleFromProps";
import { ComponentType, ValidationMap, CSSProperties } from "react";
import { Hoc } from "./types";

interface EnhancedProps {
style?: {
transform?: string;
};
}

export interface ReactDream<P extends EnhancedProps> {
Component: ComponentType<P>;

// ap : higherOrderComponent -> ReactDream -> ReactDream
ap: (hoc: Hoc<P>) => ReactDream<P>;

// chain : Component -> (Component -> ReactDream) -> ReactDream
chain: (
toDream: (Component: ComponentType<P>) => ReactDream<P>
) => ReactDream<P>;

// map : Component -> (Component -> Component) -> ReactDream
map: (hoc: Hoc<P>) => ReactDream<P>;

// concat : ReactDream -> Component -> ReactDream
concat: (OtherComponent: ReactDream<P>) => ReactDream<P>;

// contramap : Component -> (a -> Props) -> ReactDream
contramap: <Props extends P>(
propsPreprocessor: (props: P) => Props
) => ReactDream<P>;

// promap : Component -> (a -> Props) -> (Component -> Component) -> ReactDream
promap: (propsPreprocessor: (props: P) => P, hoc: Hoc<P>) => ReactDream<P>;

// addProps : Component -> (Props -> Props) -> ReactDream
addProps: (getPropsToAdd: Endomorphism<P>) => ReactDream<P>;

// fork : Component -> (Component -> a) -> a
fork: <A>(extractComponent: (Component: ComponentType<P>) => A) => A;

// defaultProps : Component -> (Props) -> ReactDream
defaultProps: (props: P) => ReactDream<P>;

// name : Component -> String -> ReactDream
name: (name: string) => ReactDream<P>;

// removeProps : Component -> (...Array) -> ReactDream
removeProps: (...propsToRemove: Array<keyof P>) => ReactDream<P>;

// propTypes : Component -> (PropTypes) -> ReactDream
propTypes: (propTypes: ValidationMap<P>) => ReactDream<P>;

// rotate : Component -> (Props -> Number) -> ReactDream
rotate: (getRotateFromProps: (props: P) => number) => ReactDream<P>;

// scale : Component -> (Props -> Number) -> ReactDream
scale: (getScaleFromProps: (props: P) => number) => ReactDream<P>;

// style : Component -> (Props -> Style) -> ReactDream
style: (getStyleFromProps: (props: P) => CSSProperties) => ReactDream<P>;

// translate : Component -> (Props -> [Number]) -> ReactDream
translate: (
getTranslateFromProps: (props: P) => Array<number | null>
) => ReactDream<P>;
}

const ReactDream = <P>(Component: ComponentType<P>): ReactDream<P> => ({
Component,
ap: hoc => ReactDream(doAp(hoc)(ReactDream(Component))),
chain: toDream => toDream(Component),
map: hoc => ReactDream(doMap(hoc)(Component)),
concat: OtherComponent =>
ReactDream(doConcat(OtherComponent.Component)(Component)),
contramap: propsPreprocessor =>
ReactDream(doContramap<P, P>(propsPreprocessor)(Component)),
promap: (propsPreprocessor, hoc) =>
ReactDream(doPromap<P, P>(propsPreprocessor, hoc)(Component)),
addProps: getPropsToAdd =>
ReactDream(Component).contramap(props => ({
...getPropsToAdd(props),
...props
})),
fork: extractComponent => extractComponent(Component),
defaultProps: props =>
ReactDream(
(recomposeDefaultProps(props)(Component) as unknown) as ComponentType<P>
),
name: flow(setDisplayName, ReactDream(Component).map),
removeProps: (...propsToRemove) =>
ReactDream(Component).contramap(props => {
// Nasty but efficient
const propsCopy = { ...props };
propsToRemove.forEach(propName => {
delete propsCopy[propName];
});
return propsCopy;
}),
propTypes: propTypesToSet =>
ReactDream(setPropTypes(propTypesToSet)(Component)),
scale: getScaleFromProps => ReactDream(doScale(getScaleFromProps)(Component)),
translate: getTranslateFromProps =>
ReactDream(doTranslate(getTranslateFromProps)(Component)),
rotate: getRotateFromProps =>
ReactDream(doRotate(getRotateFromProps)(Component)),
style: getStyleFromProps =>
ReactDream(Component).contramap(styleFromProps(getStyleFromProps))
});

// TYPE
// ////////////////////////////////////////////////////////////////////////// //

export default ReactDream;
3 changes: 0 additions & 3 deletions src/createElementWithProps.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/createElementWithProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { ComponentType, ReactElement } from "react";

type CreateElementWithProps = <P>(
props: P
) => (Component: ComponentType<P>) => ReactElement;

const createElementWithProps: CreateElementWithProps = props => Component => (
<Component {...props} />
);

export default createElementWithProps;
30 changes: 0 additions & 30 deletions src/index.js

This file was deleted.

Loading