|
| 1 | +import { useEffect, useRef, useState } from "react"; |
| 2 | +import styled, { css } from "styled-components"; |
| 3 | +import { RecordConstructorToView } from "lowcoder-core"; |
| 4 | +import { styleControl } from "comps/controls/styleControl"; |
| 5 | +import _ from "lodash"; |
| 6 | +import { |
| 7 | + IconStyle, |
| 8 | + IconStyleType, |
| 9 | + heightCalculator, |
| 10 | + widthCalculator, |
| 11 | +} from "comps/controls/styleControlConstants"; |
| 12 | +import { UICompBuilder } from "comps/generators/uiCompBuilder"; |
| 13 | +import { withDefault } from "../../generators"; |
| 14 | +import { |
| 15 | + NameConfigHidden, |
| 16 | + withExposingConfigs, |
| 17 | +} from "comps/generators/withExposing"; |
| 18 | +import { Section, sectionNames } from "lowcoder-design"; |
| 19 | +import { hiddenPropertyView } from "comps/utils/propertyUtils"; |
| 20 | +import { trans } from "i18n"; |
| 21 | +import { NumberControl } from "comps/controls/codeControl"; |
| 22 | +import { IconControl } from "comps/controls/iconControl"; |
| 23 | +import ReactResizeDetector from "react-resize-detector"; |
| 24 | +import { AutoHeightControl } from "../../controls/autoHeightControl"; |
| 25 | +import { |
| 26 | + clickEvent, |
| 27 | + eventHandlerControl, |
| 28 | +} from "../../controls/eventHandlerControl"; |
| 29 | +import { useContext } from "react"; |
| 30 | +import { EditorContext } from "comps/editorState"; |
| 31 | + |
| 32 | +const Container = styled.div<{ $style: IconStyleType | undefined }>` |
| 33 | + display: flex; |
| 34 | + align-items: center; |
| 35 | + justify-content: center; |
| 36 | +
|
| 37 | + ${(props) => |
| 38 | + props.$style && |
| 39 | + css` |
| 40 | + height: calc(100% - ${props.$style.margin}); |
| 41 | + width: calc(100% - ${props.$style.margin}); |
| 42 | + padding: ${props.$style.padding}; |
| 43 | + margin: ${props.$style.margin}; |
| 44 | + border: ${props.$style.borderWidth} solid ${props.$style.border}; |
| 45 | + border-radius: ${props.$style.radius}; |
| 46 | + background: ${props.$style.background}; |
| 47 | + svg { |
| 48 | + max-width: ${widthCalculator(props.$style.margin)}; |
| 49 | + max-height: ${heightCalculator(props.$style.margin)}; |
| 50 | + color: ${props.$style.fill}; |
| 51 | + object-fit: contain; |
| 52 | + pointer-events: auto; |
| 53 | + } |
| 54 | + `} |
| 55 | +`; |
| 56 | + |
| 57 | +const EventOptions = [clickEvent] as const; |
| 58 | + |
| 59 | +const childrenMap = { |
| 60 | + style: styleControl(IconStyle), |
| 61 | + icon: withDefault(IconControl, "/icon:antd/homefilled"), |
| 62 | + autoHeight: withDefault(AutoHeightControl, "auto"), |
| 63 | + iconSize: withDefault(NumberControl, 20), |
| 64 | + onEvent: eventHandlerControl(EventOptions), |
| 65 | +}; |
| 66 | + |
| 67 | +const IconView = (props: RecordConstructorToView<typeof childrenMap>) => { |
| 68 | + const conRef = useRef<HTMLDivElement>(null); |
| 69 | + const [width, setWidth] = useState(0); |
| 70 | + const [height, setHeight] = useState(0); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + if (height && width) { |
| 74 | + onResize(); |
| 75 | + } |
| 76 | + }, [height, width]); |
| 77 | + |
| 78 | + const onResize = () => { |
| 79 | + const container = conRef.current; |
| 80 | + setWidth(container?.clientWidth ?? 0); |
| 81 | + setHeight(container?.clientHeight ?? 0); |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <ReactResizeDetector onResize={onResize}> |
| 86 | + <Container |
| 87 | + ref={conRef} |
| 88 | + $style={props.style} |
| 89 | + style={{ |
| 90 | + fontSize: props.autoHeight |
| 91 | + ? `${height < width ? height : width}px` |
| 92 | + : props.iconSize, |
| 93 | + background: props.style.background, |
| 94 | + }} |
| 95 | + onClick={() => props.onEvent("click")} |
| 96 | + > |
| 97 | + {props.icon} |
| 98 | + </Container> |
| 99 | + </ReactResizeDetector> |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +let ShapeBasicComp = (function () { |
| 104 | + return new UICompBuilder(childrenMap, (props) => <IconView {...props} />) |
| 105 | + .setPropertyViewFn((children) => ( |
| 106 | + <> |
| 107 | + <Section name={sectionNames.basic}> |
| 108 | + {children.icon.propertyView({ |
| 109 | + label: trans("iconComp.icon"), |
| 110 | + IconType: "All", |
| 111 | + })} |
| 112 | + </Section> |
| 113 | + |
| 114 | + {["logic", "both"].includes( |
| 115 | + useContext(EditorContext).editorModeStatus |
| 116 | + ) && ( |
| 117 | + <Section name={sectionNames.interaction}> |
| 118 | + {children.onEvent.getPropertyView()} |
| 119 | + {hiddenPropertyView(children)} |
| 120 | + </Section> |
| 121 | + )} |
| 122 | + |
| 123 | + {["layout", "both"].includes( |
| 124 | + useContext(EditorContext).editorModeStatus |
| 125 | + ) && ( |
| 126 | + <> |
| 127 | + <Section name={sectionNames.layout}> |
| 128 | + {children.autoHeight.propertyView({ |
| 129 | + label: trans("iconComp.autoSize"), |
| 130 | + })} |
| 131 | + {!children.autoHeight.getView() && |
| 132 | + children.iconSize.propertyView({ |
| 133 | + label: trans("iconComp.iconSize"), |
| 134 | + })} |
| 135 | + </Section> |
| 136 | + <Section name={sectionNames.style}> |
| 137 | + {children.style.getPropertyView()} |
| 138 | + </Section> |
| 139 | + </> |
| 140 | + )} |
| 141 | + </> |
| 142 | + )) |
| 143 | + .build(); |
| 144 | +})(); |
| 145 | + |
| 146 | +ShapeBasicComp = class extends ShapeBasicComp { |
| 147 | + override autoHeight(): boolean { |
| 148 | + return false; |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +export const ShapeComp = withExposingConfigs(ShapeBasicComp, [NameConfigHidden]); |
0 commit comments