Skip to content

Commit 8f01b2b

Browse files
committed
基本的なコンポーネント群を追加
1 parent 12fc42c commit 8f01b2b

File tree

6 files changed

+156
-5
lines changed

6 files changed

+156
-5
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import StandardButton from '@/components/atoms/buttons/standard-button'
2+
import type { Meta, StoryObj } from '@storybook/react'
3+
4+
const meta: Meta<typeof StandardButton> = {
5+
title: 'atoms/buttons/standard-button',
6+
component: StandardButton,
7+
parameters: {
8+
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
9+
layout: 'centered',
10+
},
11+
tags: ['autodocs'],
12+
}
13+
14+
export default meta
15+
16+
type Story = StoryObj<typeof StandardButton>
17+
18+
/**
19+
* 活性化時
20+
*/
21+
export const Default: Story = {
22+
args: {
23+
onclick: () => alert('標準ボタンが押下されました'),
24+
name: '活性状態',
25+
disabled: false,
26+
},
27+
}
28+
29+
/**
30+
* 非活性化時
31+
*/
32+
export const Disabled: Story = {
33+
args: {
34+
onclick: () => alert('error'),
35+
name: '非活性状態',
36+
disabled: true,
37+
},
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react'
2+
3+
type Props = {
4+
onclick: () => void
5+
name: string
6+
disabled?: boolean
7+
classes?: string[]
8+
}
9+
10+
const StandardButton = React.memo(function StandardButton({
11+
name,
12+
onclick,
13+
disabled = false,
14+
classes = [],
15+
}: Props) {
16+
const style = `text-xl min-w-28 bg-green-600 text-white p-2 border-none rounded-md cursor-pointer ${disabled ? 'opacity-50' : 'hover:bg-green-700'} mx-auto${classes.map((className) => ` ${className}`)}`
17+
18+
return (
19+
<button
20+
type={'button'}
21+
onClick={onclick}
22+
className={style}
23+
disabled={disabled}
24+
>
25+
{name}
26+
</button>
27+
)
28+
})
29+
30+
export default StandardButton
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import StandardButton from '@/components/atoms/buttons/standard-button'
2+
import Modal from '@/components/molecules/modals/modal'
3+
import type { Meta, StoryObj } from '@storybook/react'
4+
import { useState } from 'react'
5+
6+
const meta: Meta<typeof Modal> = {
7+
title: 'molecules/modals/modal',
8+
component: Modal,
9+
parameters: {
10+
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
11+
layout: 'centered',
12+
},
13+
tags: ['autodocs'],
14+
}
15+
16+
export default meta
17+
18+
type Story = StoryObj<typeof Modal>
19+
20+
/**
21+
* 「開く」ボタン押下でモーダル展開時の挙動を確認することができます
22+
*/
23+
export const Default: Story = {
24+
render: () => {
25+
const [isOpen, setIsOpen] = useState(false)
26+
27+
return (
28+
<>
29+
<StandardButton onclick={() => setIsOpen(true)} name={'開く'} />
30+
<Modal isOpen={isOpen} onCancel={() => setIsOpen(false)}>
31+
<h1 className="text-center text-2xl mt-2 mb-5 mx-auto">モーダル</h1>
32+
<p className="text-base mb-5 mx-auto">
33+
モーダル内の要素は自由に設定できます
34+
</p>
35+
<StandardButton onclick={() => setIsOpen(false)} name={'閉じる'} />
36+
<div className="min-h-5" />
37+
</Modal>
38+
</>
39+
)
40+
},
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React, { useCallback, useEffect, useRef } from 'react'
2+
3+
type Props = {
4+
isOpen: boolean
5+
children: React.ReactNode
6+
onCancel: () => void
7+
}
8+
9+
const Modal = React.memo(function Modal({
10+
isOpen = false,
11+
children,
12+
onCancel,
13+
}: Props) {
14+
const dialogRef = useRef<HTMLDialogElement>(null)
15+
const open = useCallback(() => dialogRef.current?.showModal(), [])
16+
const close = useCallback(() => dialogRef.current?.close(), [])
17+
18+
useEffect(() => {
19+
const switchOpenClose = isOpen ? open : close
20+
switchOpenClose()
21+
}, [isOpen, open, close])
22+
23+
return (
24+
<>
25+
<dialog
26+
ref={dialogRef}
27+
className="min-w-96 bg-white border-none rounded-md"
28+
onClose={onCancel}
29+
onClick={onCancel}
30+
>
31+
<div
32+
className="flex flex-col justify-center p-0 m-0"
33+
onClick={(e) => e.stopPropagation()}
34+
>
35+
{children}
36+
</div>
37+
</dialog>
38+
</>
39+
)
40+
})
41+
42+
export default Modal

sample/src/pages/product-list.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// ProductList.tsx
22
/** @jsxImportSource @emotion/react */
3-
import { css } from '@emotion/react'
4-
import { useEffect, useState } from 'react'
53
import ProductAdditionForm from '@/components/molecules/forms/product-addition-form'
6-
import ProductEditingForm from '@/components/molecules/forms/product-editing-form'
7-
import FormModal from '@/components/molecules/modal/form-modal'
84
import ProductDeletionForm from '@/components/molecules/forms/product-deletion-form'
5+
import ProductEditingForm from '@/components/molecules/forms/product-editing-form'
6+
import FormModal from '@/components/molecules/modals/form-modal'
7+
import TenantsTemplate from '@/components/templtates/tenants-template'
98
import { useProducts } from '@/hooks/use-products'
109
import { getProductsAndRefresh } from '@/reducks/products/selectors'
11-
import TenantsTemplate from '@/components/templtates/tenants-template'
1210
import { Product } from '@/reducks/products/types'
11+
import { css } from '@emotion/react'
12+
import { useEffect, useState } from 'react'
1313

1414
const styles = {
1515
button: css`

0 commit comments

Comments
 (0)