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: Dropdown 及び FilterDropdown に onOpen/onClose オプションを追加する #4722

Merged
merged 9 commits into from
Jun 25, 2024
16 changes: 15 additions & 1 deletion packages/smarthr-ui/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import { usePortal } from '../../hooks/usePortal'

import { Rect, getFirstTabbable, isEventFromChild } from './dropdownHelper'

type Props = {
onOpen?: () => void
onClose?: () => void
}

type DropdownContextType = {
active: boolean
triggerRect: Rect
Expand Down Expand Up @@ -44,7 +49,7 @@ export const DropdownContext = createContext<DropdownContextType>({
contentId: '',
})

export const Dropdown: FC<PropsWithChildren> = ({ children }) => {
export const Dropdown: FC<PropsWithChildren<Props>> = ({ onOpen, onClose, children }) => {
const [active, setActive] = useState(false)
const [triggerRect, setTriggerRect] = useState<Rect>(initialRect)

Expand Down Expand Up @@ -82,6 +87,15 @@ export const Dropdown: FC<PropsWithChildren> = ({ children }) => {
},
[active, createPortal, isPortalRootMounted],
)

useEffect(() => {
if (isPortalRootMounted()) {
if (active) onOpen?.()
else onClose?.()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [active])

// set the displayName explicit for DevTools
DropdownContentRoot.displayName = 'DropdownContentRoot'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export const Render: React.FC = () => {
setValue('hoge')
setText('')
}}
onOpen={action('onOpen')}
onClose={action('onClose')}
isFiltered={isFiltered}
>
<Fieldset
Expand Down Expand Up @@ -96,6 +98,8 @@ export const Render: React.FC = () => {
isFiltered={isFiltered2}
onApply={() => setIsFiltered2(true)}
onReset={() => setIsFiltered2(false)}
onOpen={action('onOpen')}
onClose={action('onClose')}
>
<p>You can change border color of the trigger button by setting `isFiltered`.</p>
</FilterDropdown>
Expand All @@ -106,6 +110,8 @@ export const Render: React.FC = () => {
isFiltered={isFiltered3}
onApply={() => setIsFiltered3(true)}
onReset={() => setIsFiltered3(false)}
onOpen={action('onOpen')}
onClose={action('onClose')}
hasStatusText
>
<p>
Expand All @@ -122,6 +128,8 @@ export const Render: React.FC = () => {
setValue('hoge')
setText('')
}}
onOpen={action('onOpen')}
onClose={action('onClose')}
isFiltered={isFiltered}
disabled
>
Expand All @@ -134,6 +142,8 @@ export const Render: React.FC = () => {
isFiltered={isFiltered4}
onApply={() => setIsFiltered4(true)}
onReset={() => setIsFiltered4(false)}
onOpen={action('onOpen')}
onClose={action('onClose')}
hasStatusText
decorators={{
status: (txt) => <span data-wovn-enable="true">{`filtered.(${txt})`}</span>,
Expand All @@ -154,6 +164,8 @@ export const Render: React.FC = () => {
isFiltered={isFiltered4}
onApply={() => setIsFiltered4(true)}
onReset={() => setIsFiltered4(false)}
onOpen={action('onOpen')}
onClose={action('onClose')}
responseMessage={responseMessage}
>
<Stack gap={1}>
Expand Down Expand Up @@ -193,6 +205,8 @@ export const Render: React.FC = () => {
isFiltered={isFiltered2}
onApply={() => setIsFiltered2(true)}
onReset={() => setIsFiltered2(false)}
onOpen={action('onOpen')}
onClose={action('onClose')}
triggerSize="s"
>
<p>You can change border color of the trigger button by setting `isFiltered`.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Props = {
onApply: React.MouseEventHandler<HTMLButtonElement>
onCancel?: React.MouseEventHandler<HTMLButtonElement>
onReset?: React.MouseEventHandler<HTMLButtonElement>
onOpen?: () => void
onClose?: () => void
children: ReactNode
hasStatusText?: boolean
decorators?: DecoratorsType<
Expand Down Expand Up @@ -73,6 +75,8 @@ export const FilterDropdown: FC<Props & ElementProps> = ({
onApply,
onCancel,
onReset,
onOpen,
onClose,
children,
hasStatusText,
decorators,
Expand Down Expand Up @@ -140,7 +144,7 @@ export const FilterDropdown: FC<Props & ElementProps> = ({
}, [isFiltered, triggerSize])

return (
<Dropdown>
<Dropdown onOpen={onOpen} onClose={onClose}>
<DropdownTrigger>
<Button
{...props}
Expand Down