-
Notifications
You must be signed in to change notification settings - Fork 141
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
Showing
8 changed files
with
195 additions
and
351 deletions.
There are no files selected for viewing
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
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
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
79 changes: 79 additions & 0 deletions
79
packages/smarthr-ui/src/components/AppNavi/AppNaviDropdownMenuButton.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,79 @@ | ||
import React, { | ||
type ComponentProps, | ||
type FC, | ||
type PropsWithChildren, | ||
type ReactElement, | ||
type ReactNode, | ||
useMemo, | ||
useState, | ||
} from 'react' | ||
import { tv } from 'tailwind-variants' | ||
|
||
import { AnchorButton, Button, UnstyledButton } from '../Button' | ||
import { RemoteDialogTrigger } from '../Dialog' | ||
import { Dropdown, DropdownContent, DropdownScrollArea, DropdownTrigger } from '../Dropdown' | ||
import { dropdownMenuButton } from '../Dropdown/DropdownMenuButton/DropdownMenuButton' | ||
import { FaCaretDownIcon } from '../Icon' | ||
|
||
import { appNaviItemStyle } from './style' | ||
|
||
type AppNaviDropdownMenuButtonProps = PropsWithChildren<{ | ||
/** 引き金となるボタンラベル */ | ||
label: ReactNode | ||
}> | ||
|
||
type ActionItemTruthyType = | ||
| ReactElement<ComponentProps<typeof Button>> | ||
| ReactElement<ComponentProps<typeof AnchorButton>> | ||
| ReactElement<ComponentProps<typeof RemoteDialogTrigger>> | ||
|
||
const { | ||
slots: { triggerButton, actionList }, | ||
} = dropdownMenuButton | ||
const appNaviDropdownMenuButton = tv({ | ||
extend: appNaviItemStyle, | ||
slots: { | ||
wrapper: ['smarthr-ui-AppNavi-dropdownMenuButton', triggerButton], | ||
actionList, | ||
}, | ||
}) | ||
|
||
export const AppNaviDropdownMenuButton: FC<AppNaviDropdownMenuButtonProps> = ({ | ||
children, | ||
label, | ||
}) => { | ||
const [hasCurrentPage, setHasCurrentPage] = useState(false) | ||
const actualChildren = useMemo( | ||
() => | ||
React.Children.map(children, (item, i) => { | ||
if (React.isValidElement(item) && item.props['aria-current'] === 'page') { | ||
setHasCurrentPage(true) | ||
} | ||
|
||
// MEMO: {flag && <Button/>}のような書き方に対応させる為、型を変換する | ||
// itemの存在チェックでfalsyな値は弾かれている想定 | ||
return item ? <li key={i}>{item as ActionItemTruthyType}</li> : null | ||
}), | ||
[children], | ||
) | ||
|
||
const { wrapper: wrapperStyle, actionList: actionListStyle } = appNaviDropdownMenuButton({ | ||
active: hasCurrentPage, | ||
}) | ||
|
||
return ( | ||
<Dropdown> | ||
<DropdownTrigger> | ||
<UnstyledButton className={wrapperStyle()}> | ||
{label} | ||
<FaCaretDownIcon /> | ||
</UnstyledButton> | ||
</DropdownTrigger> | ||
<DropdownContent> | ||
<DropdownScrollArea as="ul" className={actionListStyle()}> | ||
{actualChildren} | ||
</DropdownScrollArea> | ||
</DropdownContent> | ||
</Dropdown> | ||
) | ||
} |
Oops, something went wrong.