Skip to content

Commit

Permalink
feat(storybook/SpinFormik): Create a story for SpinFormik
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshm authored and mergify[bot] committed Sep 23, 2020
1 parent 2273dee commit 519dfc0
Show file tree
Hide file tree
Showing 6 changed files with 1,274 additions and 106 deletions.
20 changes: 0 additions & 20 deletions .storybook/.babelrc

This file was deleted.

29 changes: 21 additions & 8 deletions .storybook/main.js
@@ -1,9 +1,13 @@
const deckWebpackConfigurer = require('../webpack.config');
const createCompiler = require('@storybook/addon-docs/mdx-compiler-plugin');

module.exports = {
addons: ['@storybook/preset-typescript'],
stories: ['../app/scripts/modules/core/src/presentation/**/*.stories.[tj]sx'],
webpackFinal: async config => {
addons: ['@storybook/preset-typescript', '@storybook/addon-essentials'],
stories: [
'../app/scripts/modules/core/src/presentation/**/*.stories.[tj]sx',
'../app/scripts/modules/core/src/presentation/**/*.stories.mdx',
],
webpackFinal: async (config) => {
const deckWebpackConfig = deckWebpackConfigurer({}, {});

config.resolve = {
Expand All @@ -13,11 +17,20 @@ module.exports = {
modules: ['node_modules'],
};

config.module.rules = deckWebpackConfig.module.rules.map(rule => {
// There are some issues with thread-loader and storybook, so disabling it until it gets fixed.
rule.use = (rule.use || []).filter(({ loader }) => loader !== 'thread-loader');
return rule;
});
config.module.rules = [
...deckWebpackConfig.module.rules.map((rule) => {
// There are some issues with thread-loader and storybook, so disabling it until it gets fixed.
rule.use = (rule.use || []).filter(({ loader }) => loader !== 'thread-loader');
return rule;
}),
{
test: /\.mdx?$/,
use: [
{ loader: 'babel-loader', options: { presets: ['@babel/preset-env', '@babel/preset-react'] } },
{ loader: '@mdx-js/loader', options: { compilers: [createCompiler()] } },
],
},
];

config.watch = true;

Expand Down
5 changes: 5 additions & 0 deletions .storybook/preview-head.html
@@ -0,0 +1,5 @@
<style>
body {
overflow: scroll;
}
</style>
156 changes: 156 additions & 0 deletions app/scripts/modules/core/src/presentation/forms/SpinFormik.stories.mdx
@@ -0,0 +1,156 @@
import { ArgsTable, Meta, Story, Canvas } from '@storybook/addon-docs/blocks';

import {
ChecklistInput,
DayPickerInput,
FormikFormField,
NumberInput,
SpinFormik,
RadioButtonInput,
ReactSelectInput,
TextAreaInput,
TextInput,
} from '../../presentation';

<Meta
title="Forms/Spinnaker Form"
decorators={[
(Story) => (
<div style={{ background: '#fff', border: '1px solid #eee', borderRadius: 10, padding: 20, width: 500 }}>
<Story />
</div>
),
]}
parameters={{
layout: 'centered',
}}
/>

Spinnaker forms are built upon [Formik](https://formik.org) and provides two key abstractions for easily building forms: `SpinFormik` and `FormikFormField`.

### `SpinFormik`

This component is thin wrapper around [`Formik`](https://formik.org/docs/api/formik) component with no additional props. A typical form is created the following way

```jsx
interface FormValues {
field1: string;
field2: number;
}

<SpinFormik<FormValues>
initialValues={{ field1: 'foo', field2: 2 }}
render={(formikProps) => <YourFormView {...formikProps} />}
onSubmit={(values) => {
/* handle form submission */
}}
/>
```

### Props

Detailed information on the props can be found [here](https://formik.org/docs/api/formik#reference).

### `FormikFormField`

This component handles the communication with formik and provides the necessary props to your input fields. Render your fields by passing them to the `input` prop.

```jsx
<SpinFormik<FormValues>
initialValues={{ field1: 'foo', field2: 2 }}
render={(formikProps) => (
<form onSubmit={formikProps.handleSubmit}>
<FormikFormField name="field" label="Field 1" input={(inputProps) => <TextInput {...inputProps} />} />
</form>
)}
onSubmit={(values) => {
/* handle form submission */
}}
/>
```

### Props

<ArgsTable of={FormikFormField} />

## Examples

Here's a simple example of a form with all supported input fields

<Canvas>
<Story name="Simple Form">
<SpinFormik
initialValues={{
data: {
account: 'prod',
applicationName: 'spinnaker',
comment: '',
enableAutoScaling: 'enabled',
instanceCount: 1,
launchDate: new Date(Date.now()).toISOString().slice(0, 10),
regions: ['us-east'],
},
}}
render={(formik) => (
<form style={{}} onSubmit={formik.handleSubmit}>
<FormikFormField
name="data.applicationName"
label="Application Name"
input={(props) => <TextInput {...props} />}
/>
<FormikFormField
name="data.account"
label="Account"
input={(props) => <ReactSelectInput {...props} clearable={false} stringOptions={['prod', 'test']} />}
/>
<FormikFormField
name="data.regions"
label="Regions"
input={(props) => (
<ChecklistInput
{...props}
inline={true}
options={[
{ label: 'us-east', value: 'us-east' },
{ label: 'us-west', value: 'us-west' },
{ label: 'eu-west', value: 'eu-west' },
]}
/>
)}
/>
<FormikFormField
name="data.enableAutoScaling"
label="Auto Scaling"
input={(props) => (
<RadioButtonInput
{...props}
inline={true}
options={[
{ label: 'Enable', value: 'enabled' },
{ label: 'Disable', value: 'disabled' },
]}
/>
)}
/>
<FormikFormField
name="data.launchDate"
label="Launch Date"
input={(props) => <DayPickerInput {...props} />}
/>
<FormikFormField
name="data.instanceCount"
label="Instances"
input={(props) => <NumberInput {...props} min={1} max={10} />}
/>
<FormikFormField name="data.comment" label="Comment" input={(props) => <TextAreaInput {...props} />} />
<div style={{ marginTop: 12, textAlign: 'right' }}>
<button className="btn btn-primary" type="submit">
Submit
</button>
</div>
</form>
)}
onSubmit={({ data, ...others }) => console.log(data)}
/>
</Story>
</Canvas>
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -107,8 +107,11 @@
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/preset-react": "^7.10.4",
"@mdx-js/loader": "^1.6.18",
"@spinnaker/eslint-plugin": "1.0.8",
"@spinnaker/mocks": "1.0.6",
"@storybook/addon-essentials": "^6.0.21",
"@storybook/preset-typescript": "^3.0.0",
"@storybook/react": "^6.0.21",
"@svgr/webpack": "^5.2.0",
Expand Down Expand Up @@ -201,7 +204,7 @@
"postcss-colorfix": "^0.0.4",
"postcss-loader": "3.0.0",
"postcss-nested": "^4.2.1",
"prettier": "1.19.1",
"prettier": "2.1.2",
"pretty-quick": "^1.11.1",
"prompts": "^2.3.1",
"react-addons-test-utils": "15.6.2",
Expand Down

0 comments on commit 519dfc0

Please sign in to comment.