import { createElement, Component, CSSProperties } from 'react';
import { storiesOf } from '@storybook/react';
import DropPortal from './index';
// classNames are defined in `.storybook/preview-head.html`
const dropdownClassName = 'dropdown-example';
const dropdownClassNames = {
enter: 'dropdown-example_enter',
enterActive: 'dropdown-example_enter-active',
exit: 'dropdown-example_exit',
exitActive: 'dropdown-example_exit-active',
};
storiesOf('DropPortal/Top Left', module)
.add('left', () => )
.add('center', () => )
.add('right', () => );
storiesOf('DropPortal/Top Center', module)
.add('left', () => )
.add('center', () => )
.add('right', () => );
storiesOf('DropPortal/Top Right', module)
.add('left', () => )
.add('center', () => )
.add('right', () => );
storiesOf('DropPortal/Bottom Left', module)
.add('left', () => )
.add('center', () => )
.add('right', () => );
storiesOf('DropPortal/Bottom Center', module)
.add('left', () => )
.add('center', () => (
))
.add('right', () => );
storiesOf('DropPortal/Bottom Right', module)
.add('left', () => )
.add('center', () => )
.add('right', () => );
storiesOf('DropPortal/Offset', module)
.add('offset x', () => (
))
.add('offset y', () => (
))
.add('offset x negative', () => (
))
.add('offset y negative', () => (
))
.add('offset x + y', () => (
))
.add('offset x + y negative', () => (
))
.add('offset y reverse when position is top', () => (
))
.add('offset y reverse when target is at bottom', () => (
));
storiesOf('DropPortal/Top', module)
.add('default', () => (
))
.add('at top', () => (
))
.add('with offset', () => (
));
storiesOf('DropPortal/Children function', module)
.add('at bottom', () => (
))
.add('at top', () => (
))
.add('at top because of limit size', () => (
))
.add('at bottom because of limit size', () => (
));
storiesOf('DropPortal/With click outside', module).add('default', () => (
));
storiesOf('DropPortal/With long body', module).add('default', () => (
Scroll down to see dropdown button
));
interface ExampleProps {
buttonClassName: string;
alignment: 'left' | 'center' | 'right';
position?: 'top' | 'bottom';
offset?: { x: number; y: number };
withChildFunction?: boolean;
withClickOutside?: boolean;
}
const CHOICES_1 = ['Choice 1', 'Choice 2', 'Choice 3'];
const CHOICES_2 = ['Choice 4', 'Choice 5'];
class DropdownExample extends Component {
button: HTMLButtonElement | null = null;
constructor(props: ExampleProps) {
super(props);
this.state = { opened: false, choices: CHOICES_1 };
this.setButton = this.setButton.bind(this);
this.toogleDropdown = this.toogleDropdown.bind(this);
this.toogleChoices = this.toogleChoices.bind(this);
}
setButton(ref: HTMLButtonElement | null) {
this.button = ref;
}
toogleDropdown() {
this.setState(({ opened }) => ({
opened: !opened,
}));
}
toogleChoices() {
this.setState(({ choices }) => ({
choices: choices === CHOICES_1 ? CHOICES_2 : CHOICES_1,
}));
}
render() {
const {
buttonClassName,
alignment,
position = 'bottom',
offset,
withChildFunction = false,
withClickOutside = false,
} = this.props;
const { opened, choices } = this.state;
const buttonLabel = opened ? 'Close' : 'Open';
return (
{opened && this.button && (
{withChildFunction ? (
({ position }) => (
{position}
{choices.map(choice => (
{choice}
))}
)
) : (
{choices.map(choice => (
{choice}
))}
)}
)}
);
}
}