|
| 1 | +import 'tdesign-web-components/range-input'; |
| 2 | +import 'tdesign-web-components/popup'; |
| 3 | + |
| 4 | +import { classNames, Component, createRef, tag } from 'omi'; |
| 5 | + |
| 6 | +import { getClassPrefix } from '../_util/classname'; |
| 7 | +import { StyledProps } from '../common'; |
| 8 | +import useOverlayInnerStyle from '../select-input/useOverlayInnerStyle'; |
| 9 | +import { TdRangeInputPopupProps } from './type'; |
| 10 | + |
| 11 | +export interface RangeInputPopupProps extends TdRangeInputPopupProps, StyledProps {} |
| 12 | + |
| 13 | +@tag('t-range-input-popup') |
| 14 | +export default class RangeInputPopup extends Component<RangeInputPopupProps> { |
| 15 | + classPrefix = getClassPrefix(); |
| 16 | + |
| 17 | + inputRef = createRef(); |
| 18 | + |
| 19 | + innerInputValue = undefined; |
| 20 | + |
| 21 | + cachedOverlayWidth?: string; |
| 22 | + |
| 23 | + lockedTriggerElement?: HTMLElement; |
| 24 | + |
| 25 | + lockedTriggerOriginalWidth?: string; |
| 26 | + |
| 27 | + install() { |
| 28 | + const { inputValue, defaultInputValue } = this.props; |
| 29 | + this.innerInputValue = inputValue !== undefined ? inputValue : defaultInputValue; |
| 30 | + } |
| 31 | + |
| 32 | + receiveProps(nextProps: { inputValue: any; visible?: boolean }) { |
| 33 | + // 受控场景下,同步外部值到内部缓存 |
| 34 | + if (nextProps.inputValue !== undefined) { |
| 35 | + this.innerInputValue = nextProps.inputValue; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private handleRangeInputChange = (evt: CustomEvent) => { |
| 40 | + const detail = (evt && evt.detail) || {}; |
| 41 | + const { value: nextValue, context } = detail; |
| 42 | + |
| 43 | + // 未受控则更新内部值 |
| 44 | + if (this.props.inputValue === undefined) { |
| 45 | + this.innerInputValue = nextValue; |
| 46 | + this.update(); |
| 47 | + } |
| 48 | + // 透传给外部 |
| 49 | + this.props.onInputChange?.(nextValue, context); |
| 50 | + }; |
| 51 | + |
| 52 | + render(props: RangeInputPopupProps) { |
| 53 | + const { |
| 54 | + panel, |
| 55 | + popupVisible, |
| 56 | + disabled, |
| 57 | + popupProps, |
| 58 | + status, |
| 59 | + tips, |
| 60 | + inputValue, |
| 61 | + rangeInputProps, |
| 62 | + readonly, |
| 63 | + className, |
| 64 | + style, |
| 65 | + innerStyle, |
| 66 | + } = props; |
| 67 | + |
| 68 | + const { tOverlayInnerStyle, innerPopupVisible, onInnerPopupVisibleChange } = useOverlayInnerStyle( |
| 69 | + { ...props, allowInput: false }, |
| 70 | + undefined, |
| 71 | + this, |
| 72 | + ); |
| 73 | + |
| 74 | + const isVisible = popupVisible ?? innerPopupVisible; |
| 75 | + |
| 76 | + if (!isVisible) { |
| 77 | + this.cachedOverlayWidth = undefined; |
| 78 | + if (this.lockedTriggerElement && this.lockedTriggerOriginalWidth !== undefined) { |
| 79 | + this.lockedTriggerElement.style.width = this.lockedTriggerOriginalWidth; |
| 80 | + } |
| 81 | + this.lockedTriggerElement = undefined; |
| 82 | + this.lockedTriggerOriginalWidth = undefined; |
| 83 | + } |
| 84 | + |
| 85 | + // 计算 panel 宽度,支持自定义或和输入框宽度保持一致 |
| 86 | + const overlayInnerStyle = (triggerEl: HTMLElement, popupEl: HTMLElement) => { |
| 87 | + if (!this.cachedOverlayWidth && triggerEl) { |
| 88 | + const { width } = triggerEl.getBoundingClientRect(); |
| 89 | + if (Number.isFinite(width) && width > 0) { |
| 90 | + this.cachedOverlayWidth = `${Math.round(width)}px`; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (triggerEl && !this.lockedTriggerElement) { |
| 95 | + this.lockedTriggerElement = triggerEl; |
| 96 | + this.lockedTriggerOriginalWidth = triggerEl.style.width; |
| 97 | + } |
| 98 | + |
| 99 | + const baseStyle = tOverlayInnerStyle?.() || {}; |
| 100 | + const resolvedBase = typeof baseStyle === 'function' ? baseStyle(triggerEl, popupEl) || {} : baseStyle; |
| 101 | + const resolvedExternal = |
| 102 | + typeof popupProps?.overlayInnerStyle === 'function' |
| 103 | + ? popupProps.overlayInnerStyle(triggerEl, popupEl) || {} |
| 104 | + : popupProps?.overlayInnerStyle || {}; |
| 105 | + |
| 106 | + const merged = { |
| 107 | + ...resolvedBase, |
| 108 | + ...resolvedExternal, |
| 109 | + marginTop: '16px', |
| 110 | + } as Record<string, any>; |
| 111 | + |
| 112 | + if (this.cachedOverlayWidth) { |
| 113 | + merged.width = this.cachedOverlayWidth; |
| 114 | + } |
| 115 | + |
| 116 | + if (this.lockedTriggerElement && this.cachedOverlayWidth) { |
| 117 | + this.lockedTriggerElement.style.width = this.cachedOverlayWidth; |
| 118 | + } |
| 119 | + |
| 120 | + return merged; |
| 121 | + }; |
| 122 | + |
| 123 | + const popupConfig = popupProps ?? {}; |
| 124 | + const rangeInputConfig = rangeInputProps ?? {}; |
| 125 | + const value = inputValue !== undefined ? inputValue : this.innerInputValue ?? []; |
| 126 | + const name = `${this.classPrefix}-range-input-popup`; |
| 127 | + const wrapperClassName = classNames(name, className, { |
| 128 | + [`${name}--visible`]: isVisible, |
| 129 | + }); |
| 130 | + const wrapperStyle = { ...(style || {}), ...(innerStyle || {}) }; |
| 131 | + |
| 132 | + return ( |
| 133 | + <div className={wrapperClassName} style={wrapperStyle} ref={this.inputRef}> |
| 134 | + <t-popup |
| 135 | + hideEmptyPopup |
| 136 | + content={panel} |
| 137 | + trigger="click" |
| 138 | + placement="bottom-left" |
| 139 | + showArrow={false} |
| 140 | + visible={isVisible} |
| 141 | + onVisibleChange={onInnerPopupVisibleChange} |
| 142 | + disabled={disabled} |
| 143 | + {...popupConfig} |
| 144 | + overlayInnerStyle={overlayInnerStyle} |
| 145 | + > |
| 146 | + <t-range-input |
| 147 | + disabled={disabled} |
| 148 | + status={status} |
| 149 | + tips={tips} |
| 150 | + value={value} |
| 151 | + onChange={this.handleRangeInputChange} |
| 152 | + readonly={readonly} |
| 153 | + {...rangeInputConfig} |
| 154 | + /> |
| 155 | + </t-popup> |
| 156 | + </div> |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
0 commit comments