|
| 1 | +import classNames from 'classnames'; |
| 2 | +import { JsxChildren } from 'dom-renderer'; |
| 3 | +import { observable } from 'mobx'; |
| 4 | +import { FC, WebCellProps, attribute, component, observer } from 'web-cell'; |
| 5 | + |
| 6 | +import { Button, ButtonProps } from './Button'; |
| 7 | + |
| 8 | +export const Dropdown: FC<WebCellProps<HTMLDivElement>> = ({ |
| 9 | + className = '', |
| 10 | + children, |
| 11 | + ...props |
| 12 | +}) => ( |
| 13 | + <div className={`dropdown ${className}`} {...props}> |
| 14 | + {children} |
| 15 | + </div> |
| 16 | +); |
| 17 | + |
| 18 | +export const DropdownToggle: FC<ButtonProps> = ({ |
| 19 | + className = '', |
| 20 | + children, |
| 21 | + ...props |
| 22 | +}) => ( |
| 23 | + <Button {...props} className={`dropdown-toggle ${className}`} type="button"> |
| 24 | + {children} |
| 25 | + </Button> |
| 26 | +); |
| 27 | + |
| 28 | +export const DropdownMenu: FC<WebCellProps> = ({ |
| 29 | + className = '', |
| 30 | + children, |
| 31 | + ...props |
| 32 | +}) => ( |
| 33 | + <nav className={`dropdown-menu ${className}`} {...props}> |
| 34 | + {children} |
| 35 | + </nav> |
| 36 | +); |
| 37 | + |
| 38 | +export const DropdownItem: FC<WebCellProps<HTMLAnchorElement>> = ({ |
| 39 | + className = '', |
| 40 | + children, |
| 41 | + ...props |
| 42 | +}) => ( |
| 43 | + <a className={`dropdown-item ${className}`} {...props}> |
| 44 | + {children} |
| 45 | + </a> |
| 46 | +); |
| 47 | + |
| 48 | +export interface DropdownButtonProps extends WebCellProps, ButtonProps { |
| 49 | + caption: JsxChildren; |
| 50 | +} |
| 51 | + |
| 52 | +@component({ |
| 53 | + tagName: 'dropdown-button', |
| 54 | + mode: 'open' |
| 55 | +}) |
| 56 | +@observer |
| 57 | +export class DropdownButton extends HTMLElement { |
| 58 | + declare props: DropdownButtonProps; |
| 59 | + |
| 60 | + @attribute |
| 61 | + @observable |
| 62 | + accessor variant: ButtonProps['variant']; |
| 63 | + |
| 64 | + @attribute |
| 65 | + @observable |
| 66 | + accessor size: ButtonProps['size']; |
| 67 | + |
| 68 | + @observable |
| 69 | + accessor caption: JsxChildren; |
| 70 | + |
| 71 | + @attribute |
| 72 | + @observable |
| 73 | + accessor show = false; |
| 74 | + |
| 75 | + renderContent() { |
| 76 | + const { variant, size, caption, show } = this; |
| 77 | + |
| 78 | + return ( |
| 79 | + <Dropdown className={classNames({ show })}> |
| 80 | + <DropdownToggle |
| 81 | + className={classNames({ show })} |
| 82 | + {...{ variant, size }} |
| 83 | + onClick={() => (this.show = !show)} |
| 84 | + > |
| 85 | + {caption} |
| 86 | + </DropdownToggle> |
| 87 | + <DropdownMenu className={classNames({ show })}> |
| 88 | + <slot /> |
| 89 | + </DropdownMenu> |
| 90 | + </Dropdown> |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + render() { |
| 95 | + return ( |
| 96 | + <> |
| 97 | + <link |
| 98 | + rel="stylesheet" |
| 99 | + href="https://unpkg.com/bootstrap@5.3.2/dist/css/bootstrap.min.css" |
| 100 | + /> |
| 101 | + {this.renderContent()} |
| 102 | + </> |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments