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

js: react/typescript storybook init #29

Open
wants to merge 14 commits into
base: baseline/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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ notebooks/**/*.pdf
/.npm/
/.config/
/Library/
**/*.tsbuildinfo
3 changes: 3 additions & 0 deletions js/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
addons: ["@storybook/addon-actions", "@storybook/preset-typescript"],
};
12 changes: 12 additions & 0 deletions js/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @flow
*/

import * as React from "react";
import { configure, addDecorator } from "@storybook/react";

function load() {
require("./stories/index.tsx");
}

configure(load, module);
4 changes: 4 additions & 0 deletions js/.storybook/stories/Info.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as React from "react";
declare type InfoComponent = (fn: React.FunctionComponent) => () => React.ReactElement<any>;
export declare let Info: InfoComponent;
export {};
11 changes: 11 additions & 0 deletions js/.storybook/stories/Info.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as React from "react";
import { withInfo } from "@storybook/addon-info";

type InfoComponent = (
fn: React.FunctionComponent,
) => () => React.ReactElement<any>;

export let Info: InfoComponent = (fn: React.FunctionComponent) =>
withInfo({ inline: true })(() => {
return <div style={{ padding: "10px 40px" }}>{fn({})}</div>;
});
19 changes: 19 additions & 0 deletions js/.storybook/stories/StoryButton.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference types="react" />
declare type TOnClick = () => void;
declare type Props = {
/**
* Example value to display
**/
enum?: " " | "a" | "b";
/** Example value with string type */
value: string;
/** Example value with some object type */
position?: {
x: number;
y: number;
};
/** Example onClick with custom type */
onClick?: TOnClick;
};
export declare function StoryButton(props?: Props): JSX.Element;
export {};
36 changes: 36 additions & 0 deletions js/.storybook/stories/StoryButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from "react";

type TOnClick = () => void;

type Props = {
/**
* Example value to display
**/
enum?: " " | "a" | "b";

/** Example value with string type */
value: string;

/** Example value with some object type */
position?: { x: number; y: number };

/** Example onClick with custom type */
onClick?: TOnClick;
};

export function StoryButton(props: Props = { value: "Hello world!" }) {
let [state, setState] = React.useState(0);
let { onClick } = props;
return (
<button
onClick={() => {
setState(++state);
if (onClick != null) {
onClick();
}
}}
>
{props.value}: {state}
</button>
);
}
1 change: 1 addition & 0 deletions js/.storybook/stories/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
18 changes: 18 additions & 0 deletions js/.storybook/stories/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as React from "react";

import { storiesOf } from "@storybook/react";
import { action } from "@storybook/addon-actions";

import { Info } from "./Info";

import { StoryButton } from "./StoryButton";

storiesOf("Button", module).add(
"With example value",
Info(() => (
<StoryButton
value={"Hello there!"}
onClick={action("StoryButton onClick!")}
/>
)),
);
7 changes: 7 additions & 0 deletions js/.storybook/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"emitDeclarationOnly": true
},
"include": ["./stories/**/*.tsx", "./stories/**/*.ts"]
}
7 changes: 7 additions & 0 deletions js/.storybook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"emitDeclarationOnly": false
},
"include": ["./stories/**/*.tsx", "./stories/**/*.ts"]
}
37 changes: 37 additions & 0 deletions js/.storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const path = require("path");

let PATH_TS = path.resolve(__dirname, "./stories");
let PATH_TSCONFIG = path.resolve(__dirname, "./tsconfig.json");

module.exports = (baseConfig, env) => {
let { config } = baseConfig;

let shallowRules = { ...config.module.rules[0] };

/**
* Some hacks to make things work...
*/
config.module.rules[0] = {
test: /\.tsx?$/,
include: PATH_TS,
exclude: shallowRules.exclude,
use: [
require.resolve("ts-loader"),
{
loader: require.resolve("react-docgen-typescript-loader"),
options: {
// Provide the path to our tsconfig.json
tsconfigPath: PATH_TSCONFIG,
setDisplayName: false,
},
},
],
};

config.module.rules.unshift({
...shallowRules,
test: /\.jsx?$/,
});

return config;
};
15 changes: 15 additions & 0 deletions js/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ FMT = flow-typed rex-graphql rex-ui rex-demo-ui
test:
@yarn run jest --no-watch --no-watchman

test-update:
@yarn run jest --no-watch --no-watchman -u

test-watch:
@yarn run jest --watch

install:
@yarn
@yarn --cwd ./rex-graphql-codegen run build

showcase:
@yarn run cosmos

storybook:
@yarn run storybook

build:
@./node_modules/.bin/tsc --build

watch:
@./node_modules/.bin/tsc --build --watch

lint:
@yarn run eslint --config ./.eslintrc .

Expand Down
13 changes: 8 additions & 5 deletions js/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
let plugins = process.env.REACT_STORYBOOK === "true" ? ["react-docgen"] : [];

module.exports = {
env: {
production: {
presets: ["react-app"]
presets: ["react-app"],
},
development: {
presets: ["react-app"]
presets: ["react-app"],
},
test: {
presets: [["@babel/preset-env", { targets: { ie: 9 } }], "react-app"]
}
}
presets: [["@babel/preset-env", { targets: { ie: 9 } }], "react-app"],
},
},
plugins,
};
37 changes: 26 additions & 11 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,46 @@
],
"scripts": {
"prettier:rex:forms": "prettier --write ./rex-forms/**/*.js",
"prettier-check:rex:forms": "prettier --check ./rex-forms/**/*.js"
"prettier-check:rex:forms": "prettier --check ./rex-forms/**/*.js",
"storybook": "REACT_STORYBOOK=true start-storybook -p 9001 -c .storybook"
},
"devDependencies": {
"@babel/core": "7.1.6",
"babel-jest": "^24.0.0",
"babel-preset-react-app": "^9.0.0",
"@storybook/addon-actions": "5.3.19",
"@storybook/addon-info": "5.3.19",
"@storybook/preset-typescript": "3.0.0",
"@storybook/react": "5.3.18",
"@types/jest": "^26.0.0",
"@types/jss": "^10.0.0",
"@types/react": "^16.8.1",
"@types/react-dom": "^16.8.1",
"@types/storybook__addon-info": "^5.2.1",
"@types/storybook__addons": "^5.2.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
"babel-jest": "^24.0.0",
"babel-plugin-react-docgen": "4.1.0",
"babel-preset-react-app": "^9.0.0",
"eslint": "6.8.0",
"eslint-config-react-app": "5.2.0",
"eslint-loader": "3.0.3",
"eslint-plugin-flowtype": "4.6.0",
"eslint-plugin-import": "2.20.1",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.18.3",
"eslint-plugin-react-hooks": "2.4.0",
"flow-bin": "0.118.0",
"fork-ts-checker-webpack-plugin": "4.1.5",
"html-webpack-plugin": "4.0.0-beta.5",
"jest": "^24.0.0",
"jest-cli": "^24.0.0",
"jest-dom": "^3.2.1",
"prettier": "1.19.1",
"react-cosmos": "4.8.1",
"react-docgen-typescript-loader": "3.7.2",
"react-test-renderer": "16.8.6",
"react-testing-library": "7.0.0",
"eslint": "6.8.0",
"eslint-config-react-app": "5.2.0",
"eslint-loader": "3.0.3",
"eslint-plugin-flowtype": "4.6.0",
"eslint-plugin-import": "2.20.1",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.18.3",
"eslint-plugin-react-hooks": "2.4.0"
"ts-loader": "7.0.5"
},
"resolutions": {
"**/@material-ui/core": "3.9.3",
Expand Down
24 changes: 14 additions & 10 deletions js/rex-conf-jest/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
module.exports = {
collectCoverageFrom: ["**/*.js"],
collectCoverageFrom: ["**/*.js", "**/*.ts"],
// TODO: this is neede for pnp, enable when needed.
// resolver: "jest-pnp-resolver",
setupFiles: ["react-app-polyfill/jsdom"],
testMatch: [
"<rootDir>/**/__tests__/**/*-test.{js,jsx}",
"<rootDir>/**/*.(spec|test).{js,jsx}"
"<rootDir>/**/*.(spec|test).{js,jsx}",
"<rootDir>/**/__tests__/**/*-test.{ts,tsx}",
"<rootDir>/**/*.(spec|test).{ts,tsx}",
],
testEnvironment: "jsdom",
testURL: "http://localhost",
transform: {
"^.+\\.(js|jsx|ts|tsx)$": "babel-jest",
"^.+\\.css$": require.resolve("./cssTransform.js"),
"^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": require.resolve("./fileTransform.js")
"^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": require.resolve(
"./fileTransform.js",
),
},
//transformIgnorePatterns: [
// "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
// "^.+\\.module\\.(css|sass|scss)$"
//],
//transformIgnorePatterns: [
// "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
// "^.+\\.module\\.(css|sass|scss)$"
//],
moduleNameMapper: {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy",
},
moduleFileExtensions: [
"web.js",
Expand All @@ -32,6 +36,6 @@ module.exports = {
"json",
"web.jsx",
"jsx",
"node"
]
"node",
],
};
1 change: 1 addition & 0 deletions js/rex-demo-ui/__tests__/index-test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare let fn: (str: string) => string;
7 changes: 7 additions & 0 deletions js/rex-demo-ui/__tests__/index-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let fn = (str: string): string => str;

describe("This is a simple test", () => {
test("Check the fn returns", () => {
expect(fn("hello")).toEqual("hello");
});
});
2 changes: 2 additions & 0 deletions js/rex-demo-ui/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="react" />
export declare function App(): JSX.Element;
17 changes: 0 additions & 17 deletions js/rex-demo-ui/index.js

This file was deleted.

37 changes: 37 additions & 0 deletions js/rex-demo-ui/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import { Typography, Theme, CssBaseline } from "@material-ui/core";
import { makeStyles, ThemeProvider } from "@material-ui/styles";
import { createMuiTheme } from "@material-ui/core/styles";

let theme = createMuiTheme();

const useStyles = makeStyles((theme: Theme) => {
return {
example: {
margin: "auto",
maxWidth: 720,
color: theme.palette.text.primary,
},
};
});

export function App() {
let styles = useStyles();

return (
<div className={styles.example}>
<Typography variant={"h1"} align={"center"}>
Hello World!
</Typography>
</div>
);
}

ReactDOM.render(
<ThemeProvider theme={theme}>
<CssBaseline />
<App />
</ThemeProvider>,
document.getElementById("root"),
);
Loading