|
| 1 | +import { ReactElement } from "react"; |
| 2 | +import { DropdownOptionControl } from "comps/controls/optionsControl"; |
| 3 | +import { StringControl } from "comps/controls/codeControl"; |
| 4 | +import { trans } from "i18n"; |
| 5 | +import { ColumnTypeCompBuilder, ColumnTypeViewFn } from "../columnTypeCompBuilder"; |
| 6 | +import Menu from "antd/es/menu"; |
| 7 | +import Dropdown from "antd/es/dropdown"; |
| 8 | +import { dropdownControl } from "comps/controls/dropdownControl"; |
| 9 | +import { IconControl } from "comps/controls/iconControl"; |
| 10 | +import { withDefault } from "comps/generators"; |
| 11 | +import { IconWrapper } from "util/bottomResUtils"; |
| 12 | +import { ButtonTypeOptions } from "../simpleColumnTypeComps"; |
| 13 | +import { useStyle } from "comps/controls/styleControl"; |
| 14 | +import { ButtonStyle } from "comps/controls/styleControlConstants"; |
| 15 | +import { Button100 } from "comps/comps/buttonComp/buttonCompConstants"; |
| 16 | + |
| 17 | +const childrenMap = { |
| 18 | + buttonType: dropdownControl(ButtonTypeOptions, "primary"), |
| 19 | + label: withDefault(StringControl, 'Menu'), |
| 20 | + prefixIcon: IconControl, |
| 21 | + suffixIcon: IconControl, |
| 22 | + options: DropdownOptionControl, |
| 23 | +}; |
| 24 | + |
| 25 | +const getBaseValue: ColumnTypeViewFn<typeof childrenMap, string, string> = (props) => props.label; |
| 26 | + |
| 27 | +export const ColumnDropdownComp = (function () { |
| 28 | + return new ColumnTypeCompBuilder( |
| 29 | + childrenMap, |
| 30 | + (props) => { |
| 31 | + const hasOptionIcon = props.options.findIndex((option) => (option.prefixIcon as ReactElement)?.props.value) > -1; |
| 32 | + const items = props.options |
| 33 | + .filter((option) => !option.hidden) |
| 34 | + .map((option, index) => ({ |
| 35 | + title: option.label, |
| 36 | + label: option.label, |
| 37 | + key: option.label + " - " + index, |
| 38 | + disabled: option.disabled, |
| 39 | + icon: hasOptionIcon && <span>{option.prefixIcon}</span>, |
| 40 | + onEvent: option.onEvent, |
| 41 | + })); |
| 42 | + |
| 43 | + const hasPrefixIcon = (props.prefixIcon as ReactElement)?.props.value; |
| 44 | + const hasSuffixIcon = (props.suffixIcon as ReactElement)?.props.value; |
| 45 | + const buttonStyle = useStyle(ButtonStyle); |
| 46 | + |
| 47 | + const menu = ( |
| 48 | + <Menu |
| 49 | + items={items} |
| 50 | + onClick={({ key }) => items.find((o) => o.key === key)?.onEvent?.("click")} |
| 51 | + /> |
| 52 | + ); |
| 53 | + |
| 54 | + return ( |
| 55 | + |
| 56 | + <Dropdown |
| 57 | + trigger={["click"]} |
| 58 | + placement="bottom" |
| 59 | + menu={{items}} |
| 60 | + dropdownRender={() => menu} |
| 61 | + > |
| 62 | + <Button100 |
| 63 | + type={props.buttonType} |
| 64 | + style={{ |
| 65 | + display: 'flex', |
| 66 | + alignItems: 'center', |
| 67 | + gap: '0', |
| 68 | + minWidth: '30px', |
| 69 | + width: 'auto', |
| 70 | + }} |
| 71 | + $buttonStyle={ |
| 72 | + props.buttonType === "primary" |
| 73 | + ? buttonStyle |
| 74 | + : undefined |
| 75 | + } |
| 76 | + > |
| 77 | + { |
| 78 | + hasPrefixIcon && ( |
| 79 | + <IconWrapper style={{ |
| 80 | + margin: '0px', |
| 81 | + marginRight: props.label || hasSuffixIcon ? '3px' : '0x', |
| 82 | + }}> |
| 83 | + {props.prefixIcon} |
| 84 | + </IconWrapper> |
| 85 | + ) |
| 86 | + } |
| 87 | + { |
| 88 | + props.label || (hasPrefixIcon || hasSuffixIcon ? undefined : " ") // Avoid button disappearing |
| 89 | + } |
| 90 | + { |
| 91 | + hasSuffixIcon && ( |
| 92 | + <IconWrapper style={{ |
| 93 | + margin: '0px', |
| 94 | + marginLeft: props.label || hasPrefixIcon ? '3px' : '0x', |
| 95 | + }}> |
| 96 | + {props.suffixIcon} |
| 97 | + </IconWrapper> |
| 98 | + ) |
| 99 | + } |
| 100 | + </Button100> |
| 101 | + </Dropdown> |
| 102 | + ); |
| 103 | + }, |
| 104 | + (nodeValue) => nodeValue.label.value, |
| 105 | + getBaseValue, |
| 106 | + ) |
| 107 | + .setPropertyViewFn((children) => { |
| 108 | + return ( |
| 109 | + <> |
| 110 | + {children.buttonType.propertyView({ |
| 111 | + label: trans("table.type"), |
| 112 | + radioButton: true, |
| 113 | + })} |
| 114 | + {children.label.propertyView({ |
| 115 | + label: trans("text"), |
| 116 | + })} |
| 117 | + {children.prefixIcon.propertyView({ |
| 118 | + label: trans("button.prefixIcon"), |
| 119 | + })} |
| 120 | + {children.suffixIcon.propertyView({ |
| 121 | + label: trans("button.suffixIcon"), |
| 122 | + })} |
| 123 | + {children.options.propertyView({ |
| 124 | + title: trans("optionsControl.optionList"), |
| 125 | + })} |
| 126 | + </> |
| 127 | + ); |
| 128 | + }) |
| 129 | + .build(); |
| 130 | +})(); |
0 commit comments