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

Add form storybook #35

Merged
merged 1 commit into from
Nov 12, 2024
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,427 changes: 3,298 additions & 5,129 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"antd": "^5.21.2",
"autoprefixer": "^10.4.20",
"classnames": "^2.5.1",
"formik": "^2.4.6",
"formik-antd": "^3.0.0-beta.9",
gustiando marked this conversation as resolved.
Show resolved Hide resolved
"highcharts": "^11.4.8",
"highcharts-react-official": "^3.2.1",
"package-lock.json": "^1.0.0",
Expand All @@ -33,7 +35,8 @@
"react-dom": "^18.3.1",
"tailwindcss": "^3.4.14",
"typescript": "^5.5.3",
"vite": "^5.4.8"
"vite": "^5.4.8",
"yup": "^1.4.0"
},
"devDependencies": {
"@chromatic-com/storybook": "^1.9.0",
Expand Down
2 changes: 1 addition & 1 deletion src/appTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const appTheme: ThemeConfig = {
colorSuccess: 'var(--j2-color-success)',
colorWarning: 'var(--j2-color-warning)',
colorInfo: 'var(--j2-color-info)',
colorError: 'var(--j2-color-error)',
colorLink: 'var(--j2-color-link)',
colorError: '#bf6c6f',
gustiando marked this conversation as resolved.
Show resolved Hide resolved
colorErrorBg: 'var(--j2-color-error-bg)',
colorErrorBgHover: 'var(--j2-color-error-bg-hover)',
colorErrorBorder: 'var(--j2-color-error-border)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Alert should render correctly 1`] = `
<div>
<div
class="ant-alert ant-alert-info ant-alert-no-icon css-dev-only-do-not-override-ccdg5a"
class="ant-alert ant-alert-info ant-alert-no-icon css-dev-only-do-not-override-tpassh"
data-show="true"
role="alert"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Breadcrumb should render correctly 1`] = `
<div>
<nav
class="ant-breadcrumb css-dev-only-do-not-override-ccdg5a"
class="ant-breadcrumb css-dev-only-do-not-override-tpassh"
>
<ol />
</nav>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Button should render correctly 1`] = `
<div>
<button
class="ant-btn css-dev-only-do-not-override-ccdg5a ant-btn-default ant-btn-color-default ant-btn-variant-outlined"
class="ant-btn css-dev-only-do-not-override-tpassh ant-btn-default ant-btn-color-default ant-btn-variant-outlined"
type="button"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Card should render correctly 1`] = `
<div>
<div
class="ant-card ant-card-bordered css-dev-only-do-not-override-ccdg5a"
class="ant-card ant-card-bordered css-dev-only-do-not-override-tpassh"
>
<div
class="ant-card-head"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`Dropdown should render correctly 1`] = `
class="ant-dropdown-trigger"
>
<button
class="ant-btn css-dev-only-do-not-override-ccdg5a ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
class="ant-btn css-dev-only-do-not-override-tpassh ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
type="button"
>
<span>
Expand Down
64 changes: 64 additions & 0 deletions src/components/form/Form.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { Meta, StoryObj } from '@storybook/react'
import { Formik } from 'formik'
import { Form } from 'formik-antd'
import { Select } from '../select'
import { Button } from '../button/Button'
import { action } from '@storybook/addon-actions'
import * as Yup from 'yup'

const meta = {
title: 'Components/Form',
component: Form,
} satisfies Meta<typeof Form>

export default meta
type Story = StoryObj<typeof meta>

export const BasicForm: Story = {
render: () => {
return (
<Formik
initialValues={{ ma_plan_type: '' }}
validationSchema={Yup.object({
ma_plan_type: Yup.string().required('MA plan type is required'),
})}
onSubmit={(values) => action('form-submitted')(values)}
>
{({ isValid, dirty }) => {
const isFormReadyToSubmit = isValid && dirty
return (
<Form>
<Form.Item label="MA Plan Type" name="ma_plan_type" required>
<Select
name="ma_plan_type"
placeholder="Select a plan type"
allowClear={true}
style={{ width: '256px' }}
options={[
{ label: 'HMO', value: 'hmo' },
{ label: 'HMO SNP', value: 'hmo_snp' },
{ label: 'HMO-POS', value: 'hmo_pos' },
{ label: 'HMO D-SNP', value: 'hmo_d_snp' },
{ label: 'PPO', value: 'ppo' },
{ label: 'PPO SNP', value: 'ppo_snp' },
{ label: 'PFFS', value: 'pffs' },
{ label: 'MSA', value: 'msa' },
{ label: 'Cost Plan', value: 'cost_plan' },
{ label: 'PACE', value: 'pace' },
]}
/>
</Form.Item>
<Button
type="primary"
htmlType="submit"
disabled={!isFormReadyToSubmit}
>
Submit
</Button>
</Form>
)
}}
</Formik>
)
},
}
1 change: 1 addition & 0 deletions src/components/form/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { FormInput } from './FormInput'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Radio should render correctly 1`] = `
<div>
<label
class="ant-radio-wrapper css-dev-only-do-not-override-ccdg5a"
class="ant-radio-wrapper css-dev-only-do-not-override-tpassh"
>
<span
class="ant-radio ant-wave-target"
Expand All @@ -24,10 +24,10 @@ exports[`Radio should render correctly 1`] = `
exports[`RadioGroup should render correctly 1`] = `
<div>
<div
class="ant-radio-group ant-radio-group-solid css-dev-only-do-not-override-ccdg5a"
class="ant-radio-group ant-radio-group-solid css-dev-only-do-not-override-tpassh"
>
<label
class="ant-radio-button-wrapper css-dev-only-do-not-override-ccdg5a"
class="ant-radio-button-wrapper css-dev-only-do-not-override-tpassh"
>
<span
class="ant-radio-button"
Expand All @@ -46,7 +46,7 @@ exports[`RadioGroup should render correctly 1`] = `
</span>
</label>
<label
class="ant-radio-button-wrapper css-dev-only-do-not-override-ccdg5a"
class="ant-radio-button-wrapper css-dev-only-do-not-override-tpassh"
>
<span
class="ant-radio-button"
Expand Down
33 changes: 33 additions & 0 deletions src/components/select/Select.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Meta, StoryObj } from '@storybook/react'
import { Formik } from 'formik'
import { Form } from 'formik-antd'
import { Select } from './Select'

const meta = {
Expand All @@ -16,9 +18,13 @@ const meta = {
allowClear: {
control: 'boolean',
},
loading: {
control: 'boolean',
},
onChange: { action: 'changed' },
},
args: {
name: 'plan_metal',
size: 'middle',
options: [
{ label: 'Bronze Plan', value: 'bronze' },
Expand All @@ -36,7 +42,15 @@ export const Basic: Story = {
args: {
size: 'middle',
defaultValue: 'bronze',
loading: false,
},
render: (args) => (
<Formik initialValues={{ plan_metal: 'bronze' }} onSubmit={() => {}}>
<Form>
<Select {...args} />
</Form>
</Formik>
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we wrapping the select component in formik forms?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnoha that's how formik-antd wrapper components are supposed to be used 🤷.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in general, any input anywhere should be used within a form?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnoha sort of — If we need an input outside of the context of a form, that'll be a fork in the road we can decide on what the component(s) should look like. Granted, this wouldn't be an issue if AntD was implemented solo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can also always export a non-formik-antd <Select /> component too! But yeah it depends on the use case. Right now I don't think we're planning on using any form controls outside of the context of a form

}

export const Search: Story = {
Expand All @@ -46,6 +60,7 @@ export const Search: Story = {
placeholder: 'Select a health care plan',
optionFilterProp: 'label',
allowClear: true,
loading: false,
onChange: { action: 'changed' },
onSearch: { action: 'searched' },
options: [
Expand All @@ -55,6 +70,13 @@ export const Search: Story = {
{ value: 'platinum', label: 'Platinum Plan' },
],
},
render: (args) => (
<Formik initialValues={{ plan_metal: '' }} onSubmit={() => {}}>
<Form>
<Select {...args} />
</Form>
</Formik>
),
}

export const Multiple: Story = {
Expand All @@ -64,5 +86,16 @@ export const Multiple: Story = {
placeholder: 'Select health care plans',
defaultValue: ['bronze', 'gold'],
allowClear: true,
loading: false,
},
render: (args) => (
<Formik
initialValues={{ plan_metal: ['bronze', 'gold'] }}
onSubmit={() => {}}
>
<Form>
<Select {...args} />
</Form>
</Formik>
),
}
9 changes: 3 additions & 6 deletions src/components/select/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Select as AntDSelect, Spin } from 'antd'
import { Select as AntDSelect } from 'formik-antd'
import { Spin } from 'antd'
import { LoadingOutlined } from '@ant-design/icons'
import type { SelectProps as AntDSelectProps } from 'antd'
import { CaretDown, MagnifyingGlass, XCircle } from '@phosphor-icons/react'
import { useState } from 'react'

type SelectProps = Expand<AntDSelectProps> & {
size?: 'large' | 'middle' | 'small'
mode?: '' | 'multiple'
onChange?: (value: string | string[]) => void
onSearch?: (value: string) => void
options?: { label: string; value: string }[]
name: string
}

export const Select = (props: SelectProps) => {
Expand Down
Loading