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

Docs: Fix Modal with Dropdown composition #1455

Merged
merged 3 commits into from
Jun 4, 2024
Merged
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
@@ -1,48 +1,54 @@
import React, { useState, Ref } from 'react';
import { Button, Modal, ModalDialog, ModalHeader, ModalBody, Text, Dropdown } from '../../../src/components';
import { DropdownRenderProps } from '../../../src/types';
import React from 'react';
import {
Button,
Dropdown,
DropdownPopover,
DropdownTrigger,
Modal,
ModalBody,
ModalDialog,
ModalHeader,
Text,
} from '../../../src/components';
import { useToggle } from '../../../src/hooks';

export default {
title: 'Examples/Compositions/Modals',
};

const dropdownTrigger = ({ isOpen, trigger: { className, ref, ...restOf } }: DropdownRenderProps) => (
<Button UNSAFE_className={className} ref={ref as Ref<HTMLButtonElement>} {...restOf}>
Dropdown is {isOpen ? 'open' : 'closed'}
</Button>
);

export const ModalWithDropdown = () => {
const [isOpen, setOpen] = useState(false);

const toggleModal = () => setOpen(!isOpen);
const [isModalOpen, toggleModal] = useToggle(false);
const [isDropdownOpen, toggleDropdown] = useToggle(false);

const handleClose = () => {
setOpen(false);
toggleModal();
};

return (
<>
<Button onClick={toggleModal} aria-expanded={isOpen} aria-controls="ModalExample">
{isOpen ? 'Close' : 'Open'} Modal
<Button onClick={toggleModal} aria-expanded={isModalOpen} aria-controls="ModalExample">
{isModalOpen ? 'Close' : 'Open'} Modal
</Button>
<Modal id="ModalExample" isOpen={isOpen} onClose={handleClose}>
<Modal id="ModalExample" isOpen={isModalOpen} onClose={handleClose}>
<ModalDialog>
<ModalHeader />
<ModalBody>
<Dropdown renderTrigger={dropdownTrigger}>
<a href="#info" className="d-flex mb-400">
<Text UNSAFE_className="ml-300">Information</Text>
</a>
<a href="#link" className="d-flex mb-400">
<Text UNSAFE_className="ml-300">More links</Text>
</a>
<a href="#profile" className="d-flex mb-400">
<Text UNSAFE_className="ml-300">Profile</Text>
</a>
<a href="#help" className="d-flex">
<Text UNSAFE_className="ml-300">Help</Text>
</a>
<Dropdown id="modal-with-dropdown" isOpen={isDropdownOpen} onToggle={toggleDropdown}>
<DropdownTrigger elementType="button">Dropdown is {isDropdownOpen ? 'open' : 'closed'}</DropdownTrigger>
<DropdownPopover>
<a href="#info" className="d-flex mb-400">
<Text UNSAFE_className="ml-300">Information</Text>
</a>
<a href="#link" className="d-flex mb-400">
<Text UNSAFE_className="ml-300">More links</Text>
</a>
<a href="#profile" className="d-flex mb-400">
<Text UNSAFE_className="ml-300">Profile</Text>
</a>
<a href="#help" className="d-flex">
<Text UNSAFE_className="ml-300">Help</Text>
</a>
</DropdownPopover>
</Dropdown>
</ModalBody>
</ModalDialog>
Expand Down
4 changes: 3 additions & 1 deletion packages/web-react/src/components/Dropdown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Enhance your DropdownPopover by incorporating the versatile [Item][item] compone
Explore additional examples and insights within the dedicated documentation for the [Item][item] component.

```jsx
const [isOpen, setIsOpen] = React.useState(false);
import { useState } from 'react';

const [isOpen, setIsOpen] = useState(false);
const onToggle = () => setIsOpen(!isOpen);

<Dropdown id="DropdownExample" isOpen={isOpen} onToggle={onToggle}>
Expand Down
5 changes: 0 additions & 5 deletions packages/web-react/src/types/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ export type DropdownTriggerRenderProps = {
ref: LegacyRef<HTMLButtonElement & HTMLAnchorElement>;
};

export type DropdownRenderProps = {
isOpen: boolean;
trigger: DropdownTriggerRenderProps;
};

export interface DropdownProps extends ChildrenProps, StyleProps {
id: string;
}
Expand Down
Loading