Skip to content

Commit

Permalink
基本的なコンポーネント群を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
kuramapommel committed Aug 22, 2024
1 parent 12fc42c commit 8f01b2b
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 5 deletions.
38 changes: 38 additions & 0 deletions sample/src/components/atoms/buttons/standard-button.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import StandardButton from '@/components/atoms/buttons/standard-button'
import type { Meta, StoryObj } from '@storybook/react'

const meta: Meta<typeof StandardButton> = {
title: 'atoms/buttons/standard-button',
component: StandardButton,
parameters: {
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
layout: 'centered',
},
tags: ['autodocs'],
}

export default meta

type Story = StoryObj<typeof StandardButton>

/**
* 活性化時
*/
export const Default: Story = {
args: {
onclick: () => alert('標準ボタンが押下されました'),
name: '活性状態',
disabled: false,
},
}

/**
* 非活性化時
*/
export const Disabled: Story = {
args: {
onclick: () => alert('error'),
name: '非活性状態',
disabled: true,
},
}
30 changes: 30 additions & 0 deletions sample/src/components/atoms/buttons/standard-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'

type Props = {
onclick: () => void
name: string
disabled?: boolean
classes?: string[]
}

const StandardButton = React.memo(function StandardButton({
name,
onclick,
disabled = false,
classes = [],
}: Props) {
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}`)}`

return (
<button
type={'button'}
onClick={onclick}
className={style}
disabled={disabled}
>
{name}
</button>
)
})

export default StandardButton
41 changes: 41 additions & 0 deletions sample/src/components/molecules/modals/modal.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import StandardButton from '@/components/atoms/buttons/standard-button'
import Modal from '@/components/molecules/modals/modal'
import type { Meta, StoryObj } from '@storybook/react'
import { useState } from 'react'

const meta: Meta<typeof Modal> = {
title: 'molecules/modals/modal',
component: Modal,
parameters: {
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
layout: 'centered',
},
tags: ['autodocs'],
}

export default meta

type Story = StoryObj<typeof Modal>

/**
* 「開く」ボタン押下でモーダル展開時の挙動を確認することができます
*/
export const Default: Story = {
render: () => {
const [isOpen, setIsOpen] = useState(false)

return (
<>
<StandardButton onclick={() => setIsOpen(true)} name={'開く'} />
<Modal isOpen={isOpen} onCancel={() => setIsOpen(false)}>
<h1 className="text-center text-2xl mt-2 mb-5 mx-auto">モーダル</h1>
<p className="text-base mb-5 mx-auto">
モーダル内の要素は自由に設定できます
</p>
<StandardButton onclick={() => setIsOpen(false)} name={'閉じる'} />
<div className="min-h-5" />
</Modal>
</>
)
},
}
42 changes: 42 additions & 0 deletions sample/src/components/molecules/modals/modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useCallback, useEffect, useRef } from 'react'

type Props = {
isOpen: boolean
children: React.ReactNode
onCancel: () => void
}

const Modal = React.memo(function Modal({
isOpen = false,
children,
onCancel,
}: Props) {
const dialogRef = useRef<HTMLDialogElement>(null)
const open = useCallback(() => dialogRef.current?.showModal(), [])
const close = useCallback(() => dialogRef.current?.close(), [])

useEffect(() => {
const switchOpenClose = isOpen ? open : close
switchOpenClose()
}, [isOpen, open, close])

return (
<>
<dialog
ref={dialogRef}
className="min-w-96 bg-white border-none rounded-md"
onClose={onCancel}
onClick={onCancel}
>
<div
className="flex flex-col justify-center p-0 m-0"
onClick={(e) => e.stopPropagation()}
>
{children}
</div>
</dialog>
</>
)
})

export default Modal
10 changes: 5 additions & 5 deletions sample/src/pages/product-list.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// ProductList.tsx
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react'
import { useEffect, useState } from 'react'
import ProductAdditionForm from '@/components/molecules/forms/product-addition-form'
import ProductEditingForm from '@/components/molecules/forms/product-editing-form'
import FormModal from '@/components/molecules/modal/form-modal'
import ProductDeletionForm from '@/components/molecules/forms/product-deletion-form'
import ProductEditingForm from '@/components/molecules/forms/product-editing-form'
import FormModal from '@/components/molecules/modals/form-modal'
import TenantsTemplate from '@/components/templtates/tenants-template'
import { useProducts } from '@/hooks/use-products'
import { getProductsAndRefresh } from '@/reducks/products/selectors'
import TenantsTemplate from '@/components/templtates/tenants-template'
import { Product } from '@/reducks/products/types'
import { css } from '@emotion/react'
import { useEffect, useState } from 'react'

const styles = {
button: css`
Expand Down

0 comments on commit 8f01b2b

Please sign in to comment.