-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12fc42c
commit 8f01b2b
Showing
6 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
sample/src/components/atoms/buttons/standard-button.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters