dropdownMenu.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classnames from 'classnames';
  4. import { cssClasses } from '@douyinfe/semi-foundation/dropdown/constants';
  5. import Foundation from '@douyinfe/semi-foundation/dropdown/menuFoundation';
  6. import DropdownContext from './context';
  7. import BaseComponent, { BaseProps } from '../_base/baseComponent';
  8. const prefixCls = cssClasses.PREFIX;
  9. export type DropdownMenuProps = BaseProps;
  10. class DropdownMenu extends BaseComponent<DropdownMenuProps> {
  11. static propTypes = {
  12. children: PropTypes.node,
  13. className: PropTypes.string,
  14. style: PropTypes.object,
  15. };
  16. static contextType = DropdownContext;
  17. menuRef: React.RefObject<HTMLUListElement>
  18. constructor(props: DropdownMenuProps) {
  19. super(props);
  20. this.menuRef = React.createRef();
  21. this.foundation = new Foundation(this.adapter);
  22. }
  23. get adapter() {
  24. return {
  25. ...super.adapter,
  26. };
  27. }
  28. componentDidMount() {
  29. this.foundation.autoFocus(this.menuRef.current);
  30. }
  31. render() {
  32. const { children, className, style, ...rest } = this.props;
  33. return (
  34. <ul role="menu" aria-orientation="vertical" ref={this.menuRef} {...rest} className={classnames(`${prefixCls}-menu`, className)} style={style} onKeyDown={e => this.foundation.onMenuKeydown(e)}>
  35. {children}
  36. </ul>
  37. );
  38. }
  39. }
  40. export default DropdownMenu;