Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 | 139x 157x 157x 32x 157x 3x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 127x 127x 127x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 127x 129x 14x 267x 267x 267x 267x 267x 267x 11x 267x 77x 77x 587x 267x 267x 1444x 267x 267x 127x 9x 9x 9x 9x 8x 1x 1x 1x 1x 1x 8x 1x 1x 8x 3x 3x 1x 3x 1x 1x 8x 3x 2x 1x 1x 1x 1x 1x 1x 1x 127x 5x 5x 2x 3x 127x 1x 1x 1x 1x 127x 4x 4x 4x 127x 1216x 1216x 1216x 1216x 1216x 1216x 4x 1x 174x 10x 127x 2x 139x 139x 139x 139x 139x 139x 5x 11x 41x 41x 1x 40x 40x 297x 903x 32x | import React from 'react'; import PropTypes from 'prop-types'; import _ from 'lodash'; import { lucidClassNames, uniqueName } from '../../util/style-helpers'; import { StandardProps, getFirst, rejectTypes, findTypes, Overwrite, } from '../../util/component-types'; import { scrollParentTo } from '../../util/dom-helpers'; import { buildModernHybridComponent } from '../../util/state-management'; import * as KEYCODE from '../../constants/key-code'; import * as reducers from './DropMenu.reducers'; import ContextMenu from '../ContextMenu/ContextMenu'; function joinArray( array: React.ReactNode[], getSeparator: ( element: React.ReactNode, index: number, array: React.ReactNode[] ) => React.ReactNode ) { return _.reduce( array, (newArray: React.ReactNode[], element, index) => { newArray.push(element); if (index < _.size(array) - 1) { newArray.push(getSeparator(element, index, array)); } return newArray; }, [] ); } function isOptionVisible(option: IOptionsData) { return !option.optionProps.isHidden; } const cx = lucidClassNames.bind('&-DropMenu'); const { any, arrayOf, bool, func, node, number, object, oneOf, string } = PropTypes; /** Header */ export interface IDropMenuHeaderProps extends StandardProps { description?: string; } const Header = (_props: IDropMenuHeaderProps): null => null; Header.displayName = 'DropMenu.Header'; Header.peek = { description: ` An optional header to be displayed within the expanded Flyout, above all \`Option\`s. `, }; Header.propName = 'Header'; Header.propTypes = {}; /** Control */ export interface IDropMenuControlProps extends StandardProps { description?: string; } const Control = (_props: IDropMenuControlProps): null => null; Control.displayName = 'DropMenu.Control'; Control.peek = { description: `Renders a \`<div>\` that acts as the control target which the flyout menu is anchored to. Only one \`Control\` is used.`, }; Control.propName = 'Control'; Control.propTypes = {}; /** Option Group */ export interface IDropMenuOptionGroupProps extends StandardProps { description?: string; isHidden?: boolean; } export type OptionGroupFC = (props: IDropMenuOptionGroupProps) => null; const OptionGroup = (_props: IDropMenuOptionGroupProps): null => null; OptionGroup.displayName = 'DropMenu.OptionGroup'; OptionGroup.peek = { description: `A special kind of \`Option\` that is always rendered at the top of the menu and has an \`optionIndex\` of \`null\`. Useful for unselect.`, }; OptionGroup.propName = 'OptionGroup'; OptionGroup.propTypes = { /** hides the \`OptionGroup\` from the list. */ isHidden: bool, }; OptionGroup.defaultProps = { isHidden: false, }; /** Option */ export interface IDropMenuOptionProps extends StandardProps { isDisabled?: boolean; isHidden?: boolean; isWrapped?: boolean; Selected?: any; } const Option = (_props: IDropMenuOptionProps): null => null; Option.displayName = 'DropMenu.Option'; Option.peek = { description: ` Renders a \`<div>\` that acts as an option in the menu. `, }; Option.propName = 'Option'; Option.propTypes = { /** disables selection of the \`Option\`. */ isDisabled: bool, /** hides the \`Option\` from the list. */ isHidden: bool, /** controls wrapping of the text. */ isWrapped: bool, }; Option.defaultProps = { isDisabled: false, isHidden: false, isWrapped: true, }; /** Null Option */ export interface IDropMenuNullOptionProps extends StandardProps { description?: string; } const NullOption = (_props: IDropMenuNullOptionProps): null => null; NullOption.displayName = 'DropMenu.NullOption'; NullOption.peek = { description: `A special kind of \`Option\` that is always rendered at the top of the menu and has an \`optionIndex\` of \`null\` used for deselecting.`, }; NullOption.propName = 'NullOption'; NullOption.propTypes = {}; /** Fixed Option */ export interface IDropMenuFixedOptionProps extends StandardProps { description?: string; isDisabled: boolean; isHidden: boolean; isWrapped: boolean; } const FixedOption = (_props: IDropMenuFixedOptionProps): null => null; FixedOption.displayName = 'DropMenu.FixedOption'; FixedOption.peek = { description: ` A special kind of \`Option\` that is always rendered at the top of the menu. `, }; FixedOption.propName = 'FixedOption'; FixedOption.propTypes = { /** disables selection of the \`Option\`. */ isDisabled: bool, /** hides the \`Option\` from the list. */ isHidden: bool, /** controls wrapping of the text. */ isWrapped: bool, }; FixedOption.defaultProps = { isDisabled: false, isHidden: false, isWrapped: true, }; /** Context Menu */ export interface IDropMenuContextMenuProps extends StandardProps { description?: string; } const DropMenuContextMenu = (_props: IDropMenuContextMenuProps): null => null; DropMenuContextMenu.displayName = 'DropMenu.ContextMenu'; DropMenuContextMenu.peek = { description: `Props that are passed through to the underlying ContextMenu.`, }; DropMenuContextMenu.propName = 'ContextMenu'; DropMenuContextMenu.propTypes = { children: node /**/, }; /** Drop Menu */ export interface IDropMenuProps extends StandardProps { /** Disables the DropMenu from being clicked or focused. */ isDisabled?: boolean; /** Renders the flyout menu adjacent to the control. */ isExpanded?: boolean; /** Sets the direction the flyout menu will render relative to the control. */ direction?: 'down' | 'up'; /** Sets the alignment the flyout menu will render relative to the control. */ alignment?: 'start' | 'center' | 'end'; /** An array of currently selected \`DropMenu.Option\` indices. */ selectedIndices?: number[] | null; /** The currently focused index of \`DropMenu.Option\`. Can also be \`null\`. */ focusedIndex?: number | null; /** The \`id\` of the flyout menu portal element that is appended to \`document.body\`. Defaults to a generated id. */ portalId?: string; /** Styles that are passed through to the ContextMenu FlyOut element. */ flyOutStyle?: object; /** Styles that are passed through to the option container element. */ optionContainerStyle?: object; /** Called when collapsed and the control is clicked, or when the control has focus and the Down Arrow is pressed. */ onExpand?: ({ props, event, }: { props: IDropMenuPropsWithPassThroughs; event: React.KeyboardEvent | React.MouseEvent; }) => void; /** Called when expanded and the user clicks the control or outside of the menu, or when the control has focus and the Escape key is pressed */ onCollapse?: ({ props, event, }: { props: IDropMenuPropsWithPassThroughs; event: React.KeyboardEvent | React.MouseEvent; }) => void; /** Called when an option is clicked, or when an option has focus and the Enter key is pressed. */ onSelect?: ( optionIndex: any, { props, event, }: { props: IDropMenuOptionProps | undefined; event: React.KeyboardEvent | React.MouseEvent; } ) => void; /** Called when expanded and the the Down Arrow key is pressed. Not called when focus is on the last option. */ onFocusNext?: ({ props, event, }: { props: IDropMenuPropsWithPassThroughs; event: React.KeyboardEvent; }) => void; /** Called when expanded and the the Up Arrow key is pressed. Not called when focus is on the first option. */ onFocusPrev?: ({ props, event, }: { props: IDropMenuPropsWithPassThroughs; event: React.KeyboardEvent; }) => void; onFocusOption?: ( optionIndex: number | null, { props, event, }: { props: IDropMenuPropsWithPassThroughs; event: React.KeyboardEvent | React.MouseEvent; } ) => void; /** *Child Element* - The control target which the flyout menu is anchored to. Only one \`Control\` is used. */ Control?: React.ReactNode; /** *Child Element* - These are menu options. The \`optionIndex\` is in-order of rendering regardless of group nesting, starting with index \`0\`. Each \`Option\` may be passed a prop called \`isDisabled\` to disable selection of that \`Option\`. Any other props pass to Option will be available from the \`onSelect\` handler. */ Option?: React.ReactNode; /** *Child Element* - Used to group \`Option\`s within the menu. Any non-\`Option\`s passed in will be rendered as a label for the group. */ OptionGroup?: React.ReactNode; /** *Child Element* - A special kind of \`Option\` that is always rendered at the top of the menu and has an \`optionIndex\` of \`null\`. Useful for unselect. */ NullOption?: React.ReactNode; /** *Child Element* - An optional header to be displayed within the expanded Flyout, above all \`Option\`s. */ Header?: React.ReactNode; /** *Child Element* - props pass through to the underlying ContextMenu component */ ContextMenu?: React.ReactNode; /** *Child Element* - A special kind of \`Option\` that is always rendered at the top of the menu. */ FixedOption?: React.ReactNode; } type IDropMenuPropsWithPassThroughs = Overwrite< React.DetailedHTMLProps<React.DOMAttributes<HTMLDivElement>, HTMLDivElement>, IDropMenuProps >; /** TODO: Remove the nonPassThroughs when the component is converted to a functional component */ const nonPassThroughs = [ 'children', 'className', 'style', 'isDisabled', 'isExpanded', 'direction', 'alignment', 'selectedIndices', 'focusedIndex', 'portalId', 'flyOutStyle', 'optionContainerStyle', 'onExpand', 'onCollapse', 'onSelect', 'onFocusNext', 'onFocusPrev', 'onFocusOption', 'Control', 'Option', 'OptionGroup', 'NullOption', 'Header', 'ContextMenu', 'FixedOption', 'initialState', 'callbackId', ]; export interface IOptionsData { localOptionIndex: number; optionIndex: number; optionGroupIndex: number | null; optionProps: IDropMenuOptionProps; } export interface IHasOptionChildren< OptionGroupProps, OptionProps, NullOptionProps, FixedOptionProps > { OptionGroup: (_props: OptionGroupProps) => null; Option: (_props: OptionProps) => null; NullOption: (_props: NullOptionProps) => null; FixedOption: (_props: FixedOptionProps) => null; } export interface IDropMenuState { isMouseTriggered: boolean; optionGroups: IDropMenuOptionGroupProps[]; flattenedOptionsData: IOptionsData[]; ungroupedOptionData: IOptionsData[]; optionGroupDataLookup: { [key: number]: IOptionsData[] }; fixedOptionData: IOptionsData[]; portalId: string; isExpanded: boolean; focusedIndex: number | null; selectedIndices: number[]; nullOptions: IDropMenuNullOptionProps[]; optionGroupIndex: number | null; optionProps: []; } class DropMenu extends React.Component< IDropMenuPropsWithPassThroughs, IDropMenuState > { private _header: React.RefObject<HTMLDivElement>; constructor(props: IDropMenuPropsWithPassThroughs) { super(props); this.state = { optionGroups: [], flattenedOptionsData: [], ungroupedOptionData: [], optionGroupDataLookup: {}, nullOptions: [], fixedOptionData: [], isMouseTriggered: false, portalId: this.props.portalId || uniqueName('DropMenu-Portal-'), isExpanded: false, focusedIndex: null, optionGroupIndex: null, optionProps: [], selectedIndices: [], ...(this.getPreprocessedOptionData(props) as any), // TODO: typescript hack that should be removed }; this._header = React.createRef<HTMLDivElement>(); } static displayName = 'DropMenu'; static ContextMenu = DropMenuContextMenu; static FixedOption = FixedOption; static NullOption = NullOption; static Option = Option; static OptionGroup = OptionGroup; static Control = Control; static Header = Header; static peek = { ContextMenu: DropMenuContextMenu, description: `\`DropMenu\` is a helper component used to render a menu of options attached to any control. Supports option groups with and without labels as well as special options with a \`null\` index for unselect.`, categories: ['helpers'], madeFrom: ['ContextMenu'], }; static reducers = reducers; static propTypes = { /** Should be instances of: \`DropMenu.Control\`, \`DropMenu.Option\`, \`DropMenu.OptionGroup\`, \`DropMenu.Nulloption\`. Other direct child elements will not render. */ children: node, className: string /** Appended to the component-specific class names set on the root elements. Applies to *both* the control and the flyout menu. */, /** Styles that are passed through to root element. */ style: object, /** Disables the DropMenu from being clicked or focused. */ isDisabled: bool, /** Renders the flyout menu adjacent to the control. */ isExpanded: bool, /** Sets the direction the flyout menu will render relative to the control. */ direction: oneOf(['down', 'up']), /** Sets the alignment the flyout menu will render relative to the control. */ alignment: oneOf(['start', 'center', 'end']), /** An array of currently selected \`DropMenu.Option\` indices. */ selectedIndices: arrayOf(number), /** The currently focused index of \`DropMenu.Option\`. Can also be \`null\`. */ focusedIndex: number, /** The \`id\` of the flyout menu portal element that is appended to \`document.body\`. Defaults to a generated id. */ portalId: string, /** Styles that are passed through to the ContextMenu FlyOut element. */ flyOutStyle: object, /** Styles that are passed through to the option container element. */ optionContainerStyle: object, /** Called when collapsed and the control is clicked, or when the control has focus and the Down Arrow is pressed. Has the signature \`({ props, event }) => {}\` */ onExpand: func, /** Called when expanded and the user clicks the control or outside of the menu, or when the control has focus and the Escape key is pressed Has the signature \`({ props, event }) => {}\` */ onCollapse: func, /** Called when an option is clicked, or when an option has focus and the Enter key is pressed. Has the signature \`(optionIndex, {props, event}) => {}\` where optionIndex can be a number or \`null\`. */ onSelect: func, /** Called when expanded and the the Down Arrow key is pressed. Not called when focus is on the last option. Has the signature \`({ props, event }) => {}\` */ onFocusNext: func, /** Called when expanded and the the Up Arrow key is pressed. Not called when focus is on the first option. Has the signature \`({ props, event }) => {}\` */ onFocusPrev: func, /** Called when the mouse moves over an option. Has the signature \`(optionIndex) => {}\` where optionIndex can be a number or \`null\`. */ onFocusOption: func, Control: any /** *Child Element* - The control target which the flyout menu is anchored to. Only one \`Control\` is used. */, Option: any /** *Child Element* - These are menu options. The \`optionIndex\` is in-order of rendering regardless of group nesting, starting with index \`0\`. Each \`Option\` may be passed a prop called \`isDisabled\` to disable selection of that \`Option\`. Any other props pass to Option will be available from the \`onSelect\` handler. */, OptionGroup: any /** *Child Element* - Used to group \`Option\`s within the menu. Any non-\`Option\`s passed in will be rendered as a label for the group. */, NullOption: any /** *Child Element* - A special kind of \`Option\` that is always rendered at the top of the menu and has an \`optionIndex\` of \`null\`. Useful for unselect. */, Header: any /** *Child Element* - An optional header to be displayed within the expanded Flyout, above all \`Option\`s. */, ContextMenu: any /** *Child Element* - props pass through to the underlying ContextMenu component */, FixedOption: any /** *Child Element* - A special kind of \`Option\` that is always rendered at the top of the menu. */, }; static defaultProps = { isDisabled: false, isExpanded: false, direction: 'down' as const, alignment: 'start' as const, selectedIndices: [], focusedIndex: null, flyOutStyle: { maxHeight: '18em' }, onExpand: _.noop, onCollapse: _.noop, onSelect: _.noop, onFocusNext: _.noop, onFocusPrev: _.noop, onFocusOption: _.noop, portalId: '', optionContainerStyle: {}, ContextMenu: ContextMenu.defaultProps, }; getPreprocessedOptionData = (props: IDropMenuPropsWithPassThroughs) => { return DropMenu.preprocessOptionData(props, DropMenu); }; static preprocessOptionData = < OptionGroupProps extends IDropMenuOptionGroupProps, OptionProps extends IDropMenuOptionProps, NullOptionProps extends IDropMenuNullOptionProps, FixedOptionProps extends IDropMenuFixedOptionProps >( props: StandardProps, ParentType: IHasOptionChildren< OptionGroupProps, OptionProps, NullOptionProps, FixedOptionProps > ) => { const { OptionGroup, Option, NullOption, FixedOption } = ParentType; const optionGroups = _.map(findTypes(props, OptionGroup), 'props'); // find all OptionGroup props const fixedOptions = _.map(findTypes(props, FixedOption), 'props'); // find all FixedOption props const ungroupedOptions = _.map(findTypes(props, Option), 'props'); // find all ungrouped Option props const nullOptions = NullOption ? _.map(findTypes(props, NullOption), 'props') : []; // find all NullOption props const fixedOptionData: IOptionsData[] = _.map( fixedOptions, (optionProps, localOptionIndex) => { return { localOptionIndex, optionIndex: localOptionIndex, optionGroupIndex: null, // ungrouped options have no `optionGroupIndex` optionProps, }; } ); // flatten grouped options into array of objects to associate { index, group index, and props } for each option const groupedOptionData: IOptionsData[] = _.reduce( optionGroups, (memo: IOptionsData[], optionGroupProps, optionGroupIndex) => { const groupedOptions = _.map( findTypes(optionGroupProps, Option), 'props' ); // find all Option props for current group return memo.concat( _.map(groupedOptions, (optionProps, localOptionIndex) => { return { localOptionIndex, optionIndex: _.size(memo) + _.size(fixedOptionData) + localOptionIndex, // add current index to current array length to get final option index optionGroupIndex, // store option group index to associate option back to group optionProps: { isHidden: false, ...optionProps, }, }; }) ); }, [] ); // create lookup object for options by their group index const optionGroupDataLookup = _.groupBy( groupedOptionData, 'optionGroupIndex' ); // store ungrouped options into array of objects to associate { index, and props } for each option const ungroupedOptionData: IOptionsData[] = _.map( ungroupedOptions, (optionProps, localOptionIndex) => { return { localOptionIndex, optionIndex: _.size(groupedOptionData) + _.size(fixedOptionData) + localOptionIndex, // add current index to grouped options array length to get final option index (grouped options rendered first) optionGroupIndex: null, // ungrouped options have no `optionGroupIndex` optionProps: { isHidden: false, ...optionProps, }, }; } ); // concatenate grouped options array with ungrouped options array to get flat list of all options const flattenedOptionsData = _.concat( fixedOptionData, groupedOptionData, ungroupedOptionData ); return { optionGroups, optionGroupDataLookup, fixedOptionData, ungroupedOptionData, flattenedOptionsData, nullOptions, }; }; handleKeydown = (event: React.KeyboardEvent) => { const { props, props: { isExpanded, focusedIndex, onExpand, onCollapse, onSelect, onFocusOption, }, } = this; const { flattenedOptionsData, nullOptions } = this.state; this.setState({ isMouseTriggered: false, }); if (isExpanded) { if (event.keyCode === KEYCODE.Enter) { event.preventDefault(); const focusedOptionData = _.get( flattenedOptionsData, _.isNil(focusedIndex) ? '' : focusedIndex, null ); const focusedOptionProps = _.get( focusedOptionData, 'optionProps', Option.defaultProps ) as IDropMenuPropsWithPassThroughs; if (focusedOptionData && !focusedOptionProps.isDisabled) { onSelect && onSelect(focusedIndex, { props: focusedOptionProps, event }); } else Eif (_.isNull(focusedIndex)) { onSelect && onSelect(null, { props: _.first(nullOptions), event }); } } if (event.keyCode === KEYCODE.Escape) { event.preventDefault(); onCollapse && onCollapse({ props, event }); } if (event.keyCode === KEYCODE.ArrowUp) { if (_.isNumber(focusedIndex) || _.isNull(focusedIndex)) { if (focusedIndex === 0) { Iif (!_.isEmpty(nullOptions)) { event.preventDefault(); onFocusOption && onFocusOption(null, { props, event }); } } if (!_.isNil(focusedIndex) && focusedIndex > 0) { event.preventDefault(); onFocusOption && onFocusOption( _.findLastIndex( flattenedOptionsData, isOptionVisible, focusedIndex - 1 ), { props, event } ); } } else E{ event.preventDefault(); focusedIndex && onFocusOption && onFocusOption( _.findLastIndex( flattenedOptionsData, isOptionVisible, focusedIndex - 1 ), { props, event } ); } } if (event.keyCode === KEYCODE.ArrowDown) { if (_.isNumber(focusedIndex)) { if (focusedIndex < _.size(flattenedOptionsData) - 1) { event.preventDefault(); onFocusOption && onFocusOption( _.findIndex( flattenedOptionsData, isOptionVisible, focusedIndex + 1 ), { props, event } ); } } else { event.preventDefault(); onFocusOption && onFocusOption( _.findIndex( flattenedOptionsData, isOptionVisible, !_.isNil(focusedIndex) ? focusedIndex : undefined ), { props, event } ); } } } else { if (event.keyCode === KEYCODE.ArrowDown) { event.preventDefault(); onExpand && onExpand({ props, event }); } } }; handleClick = (event: React.MouseEvent) => { const { props, props: { isExpanded, onExpand, onCollapse }, } = this; if (isExpanded) { onCollapse && onCollapse({ props, event }); } else { onExpand && onExpand({ props, event }); } }; handleMouseFocusOption = ( optionIndex: number | null, optionProps: IDropMenuOptionProps, event: React.MouseEvent ) => { const { focusedIndex, onFocusOption } = this.props; this.setState({ isMouseTriggered: true, }); if (!optionProps.isDisabled && focusedIndex !== optionIndex) { onFocusOption && onFocusOption(optionIndex, { props: this.props, event }); } }; handleSelectOption = ( optionIndex: number | null, optionProps: IDropMenuOptionProps, event: React.KeyboardEvent | React.MouseEvent ) => { const { onSelect } = this.props; if (!optionProps.isDisabled) { onSelect && onSelect(optionIndex, { props: optionProps, event }); } }; renderOption = ( optionProps: IDropMenuOptionProps, optionIndex: number | null, isGrouped = false ) => { const { selectedIndices, focusedIndex } = this.props; const { isMouseTriggered } = this.state; const { isDisabled, isHidden, isWrapped } = optionProps; const isFocused = optionIndex === focusedIndex; const isSelected = _.includes(selectedIndices, optionIndex); return isHidden ? null : ( <div key={'DropMenuOption' + optionIndex} {..._.omit(optionProps, [ 'isDisabled', 'isHidden', 'isWrapped', 'Selected', 'Selection', 'initialState', 'callbackId', ])} onClick={(event) => this.handleSelectOption(optionIndex, optionProps, event) } onMouseMove={(event) => this.handleMouseFocusOption(optionIndex, optionProps, event) } className={cx( '&-Option', { '&-Option-is-grouped': isGrouped, '&-Option-is-focused': isFocused, '&-Option-is-selected': isSelected, '&-Option-is-disabled': isDisabled, '&-Option-is-null': _.isNull(optionIndex), '&-Option-is-wrapped': isWrapped, }, optionProps.className )} ref={(optionHTMLElement) => { if (isFocused && !isMouseTriggered) { scrollParentTo( optionHTMLElement, this._header && this._header.current ? this._header.current.offsetHeight : undefined ); } }} /> ); }; UNSAFE_componentWillReceiveProps = ( nextProps: IDropMenuPropsWithPassThroughs ) => { // only preprocess options data when it changes (via new props) - better performance than doing this each render this.setState(this.getPreprocessedOptionData(nextProps)); }; render() { const { className, style, isDisabled, isExpanded, direction, alignment, onCollapse, flyOutStyle, optionContainerStyle, ...passThroughs } = this.props; const { optionGroups, fixedOptionData, ungroupedOptionData, optionGroupDataLookup, nullOptions, portalId, } = this.state; const contextMenuProps = _.get( getFirst(this.props, DropMenu.ContextMenu), 'props', {} ); const controlProps = _.get( getFirst(this.props, DropMenu.Control), 'props', {} ); const headerProps = _.get( getFirst(this.props, DropMenu.Header), 'props', {} ); return ( <div className={cx( '&', '&-base', { '&-is-expanded': isExpanded, '&-direction-down': isExpanded && direction === 'down', '&-direction-up': isExpanded && direction === 'up', }, className )} style={style} {...(_.omit(passThroughs, nonPassThroughs) as any)} > <ContextMenu {...contextMenuProps} portalId={portalId} isExpanded={isExpanded} direction={direction} alignment={alignment} onClickOut={onCollapse} > <ContextMenu.Target> <div {...(!isDisabled ? { tabIndex: 0, onClick: this.handleClick, onKeyDown: this.handleKeydown, } : null)} {...controlProps} className={cx('&-Control', _.get(controlProps, 'className'))} /> </ContextMenu.Target> <ContextMenu.FlyOut className={cx('&', className)} style={flyOutStyle} > {!_.isEmpty(headerProps) && ( <div {...headerProps} className={cx('&-Header', headerProps.className)} onKeyDown={this.handleKeydown} ref={this._header} /> )} <div className={cx('&-option-container')} style={_.assign({}, flyOutStyle, optionContainerStyle)} > {_.map(nullOptions, (optionProps) => this.renderOption(optionProps, null) ).concat( _.isEmpty(nullOptions) ? [] : [ <div key={'OptionGroup-divider-NullOption'} className={cx('&-OptionGroup-divider')} />, ] )} { // fixed options go first _.map(fixedOptionData, ({ optionProps, optionIndex }) => this.renderOption(optionProps, optionIndex) ) } {joinArray( // for each option group, _.map( optionGroups, (optionGroupProps, optionGroupIndex): React.ReactNode => { const groupedOptions = optionGroupDataLookup[optionGroupIndex]; if ( optionGroupProps.isHidden || _.every(groupedOptions, { optionProps: { isHidden: true }, }) ) { return null; } const labelElements = rejectTypes( optionGroupProps.children, [ DropMenu.Control, DropMenu.OptionGroup, DropMenu.Option, DropMenu.NullOption, ] ); // render label if there is one return ( _.isEmpty(labelElements) ? ([] as React.ReactNode[]) : [ <div {..._.omit(optionGroupProps, [ 'isHidden', 'initialState', 'callbackId', ])} key={'OptionGroup-label' + optionGroupIndex} className={cx( '&-label', optionGroupProps.className )} > {labelElements} </div>, // render the options in the group ] ).concat( _.map( optionGroupDataLookup[optionGroupIndex], ({ optionProps, optionIndex }) => this.renderOption(optionProps, optionIndex, true) ) ); // append all ungrouped options as another unlabeled group } ).concat( _.isEmpty(ungroupedOptionData) ? [] : [ _.map( ungroupedOptionData, ({ optionProps, optionIndex }) => this.renderOption(optionProps, optionIndex) ), ] ), (element, index) => element && ( <div key={`OptionGroup-divider-${index}`} className={cx('&-OptionGroup-divider')} /> ) // separate each group with divider )} </div> </ContextMenu.FlyOut> </ContextMenu> </div> ); } } export default buildModernHybridComponent< IDropMenuPropsWithPassThroughs, IDropMenuState, typeof DropMenu >(DropMenu as any, { reducers }); export { DropMenu as DropMenuDumb, NullOption, OptionGroup }; |