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

feat: Stepを持つダイアログをキーボードフォーカスしやすく開発しやすくするカスタムフックを作る #4756

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { StoryFn } from '@storybook/react'
import React, { ReactNode } from 'react'

import { Button } from '../Button'
import { FormControl } from '../FormControl'
import { Input } from '../Input'
import { Stack } from '../Layout'

import { useDialogSteps } from './useDialogSteps'

import { ActionDialog } from '.'

export default {
title: 'Dialog(ダイアログ)/Step Dialog',
component: ActionDialog,
}

type DialogContents = {
[key: number]: Omit<
React.ComponentProps<typeof ActionDialog>,
'title' | 'onClickClose' | 'isOpen'
> & { children: ReactNode }
}

export const Default: StoryFn = () => {
const [currentStep, { setStep, nextStep, prevStep }] = useDialogSteps()

const dialogContents: DialogContents = {
0: {
actionText: '',
onClickAction: nextStep,
children: null,
},
1: {
actionText: '次へ',
onClickAction: nextStep,
children: (
<FormControl title="Label 1">
<Input name="input" />
</FormControl>
),
},
2: {
actionText: '完了',
onClickAction: () => setStep(0),
children: (
<FormControl title="Label 2">
<Input name="input" />
</FormControl>
),
},
}

const dialogContent = dialogContents[currentStep]

return (
<>
<Stack>
<div>
<Button onClick={nextStep} aria-haspopup="dialog">
Dialog
</Button>
</div>
<FormControl title="Dummy">
<Input name="input" />
</FormControl>
</Stack>

<ActionDialog
{...dialogContent}
title={`Stepper Dialog ${currentStep} / 2`}
isOpen={currentStep > 0}
onClickClose={currentStep === 2 ? () => setStep(0) : prevStep}
>
{dialogContent.children}
</ActionDialog>
</>
)
}
41 changes: 41 additions & 0 deletions packages/smarthr-ui/src/components/Dialog/useDialogSteps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { useCallback, useEffect, useRef, useState } from 'react'

type UseDialogStepsResult = [
number,
{
setStep: React.Dispatch<React.SetStateAction<number>>
nextStep: () => void
prevStep: () => void
},
]

export function useDialogSteps(startStep?: number): UseDialogStepsResult {
const [step, setStep] = useState<number>(startStep || 0)
const prevStep = useRef<number>(step)

useEffect(() => {
if (step !== prevStep.current && step > 0) {
const focusTarget = document.querySelector('[role=dialog] > div > div[tabindex]')
if (focusTarget instanceof HTMLDivElement) {
focusTarget.focus()
}
}

prevStep.current = step
}, [step])

const dialogSteps: UseDialogStepsResult = [
step,
{
setStep,
nextStep: useCallback(() => {
setStep((prev) => prev + 1)
}, []),
prevStep: useCallback(() => {
setStep((prev) => prev - 1)
}, []),
},
]

return dialogSteps
}