import {TestIdBuilder, ThemeProps, themeable} from 'amis-core'; import React from 'react'; import omit from 'lodash/omit'; import isInteger from 'lodash/isInteger'; import {InputBoxProps} from './InputBox'; import {uncontrollable} from 'amis-core'; import {Icon} from './icons'; import Input from './Input'; import {autobind, ucFirst} from 'amis-core'; import {LocaleProps, localeable} from 'amis-core'; import isPlainObject from 'lodash/isPlainObject'; import TooltipWrapper, {TooltipObject} from './TooltipWrapper'; import AutoFoldedList from './AutoFoldedList'; export interface ResultBoxProps extends ThemeProps, LocaleProps, Omit { onChange?: (value: string) => void; onResultClick?: (e: React.MouseEvent) => void; result?: Array | any; itemRender: (value: any) => JSX.Element | string; onResultChange?: (value: Array) => void; onItemClick?: (item: Object) => void; onClear?: (e: React.MouseEvent) => void; allowInput?: boolean; inputPlaceholder: string; hasDropDownArrow?: boolean; maxTagCount?: number; overflowTagPopover?: TooltipObject; actions?: JSX.Element | JSX.Element[]; showInvalidMatch?: boolean; popOverContainer?: any; showArrow?: boolean; testIdBuilder?: TestIdBuilder; } export class ResultBox extends React.Component { static defaultProps: Pick< ResultBoxProps, | 'clearable' | 'placeholder' | 'itemRender' | 'inputPlaceholder' | 'showArrow' > = { clearable: false, placeholder: 'placeholder.noData', inputPlaceholder: 'placeholder.enter', showArrow: true, itemRender: (option: any) => ( {`${option.scopeLabel || ''}${option.label}`} ) }; state = { isFocused: false }; inputRef: React.RefObject = React.createRef(); focus() { this.inputRef.current?.focus(); } blur() { this.inputRef.current?.blur(); } @autobind clearValue(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); this.props.onClear && this.props.onClear(e); this.props.onResultChange && this.props.onResultChange([]); } @autobind handleFocus(e: any) { const onFocus = this.props.onFocus; onFocus && onFocus(e); this.setState({ isFocused: true }); } @autobind handleBlur(e: any) { const onBlur = this.props.onBlur; onBlur && onBlur(e); this.setState({ isFocused: false }); } @autobind removeItem(e: React.MouseEvent) { e.stopPropagation(); e.preventDefault(); const {result, onResultChange} = this.props; const index = parseInt(e.currentTarget.getAttribute('data-index')!, 10); const newResult = Array.isArray(result) ? result.concat() : []; newResult.splice(index, 1); onResultChange && onResultChange(newResult); } @autobind handleItemClick(e: React.MouseEvent) { e.stopPropagation(); e.preventDefault(); const {result, onItemClick} = this.props; const index = parseInt(e.currentTarget.getAttribute('data-index')!, 10); const newResult = Array.isArray(result) ? result.concat() : []; onItemClick && onItemClick(newResult[index] || {}); } @autobind handleChange(e: React.ChangeEvent) { const {onChange} = this.props; onChange?.(e.currentTarget.value); } renderMultipleTags(tags: any[]) { const { maxTagCount, overflowTagPopover, itemRender, classnames: cx, showInvalidMatch, popOverContainer, testIdBuilder } = this.props; if (typeof maxTagCount === 'number' && maxTagCount > 0) { const tooltipProps: TooltipObject = { offset: [0, -10], tooltipClassName: cx( 'ResultBox-overflow', overflowTagPopover?.tooltipClassName ), ...omit(overflowTagPopover, ['children', 'content', 'tooltipClassName']) }; return ( { const itemTIB = testIdBuilder?.getChild(item.value || index); const isShowInvalid = showInvalidMatch && item?.__unmatched; const body = (
{itemRender(item)}
); return folded ? ( body ) : ( {body} ); }} >
); } return tags.map((item, index) => { const itemTIB = testIdBuilder?.getChild(index); return (
{itemRender(item)}
); }); } render() { /** 不需要透传给Input的属性要解构出来 */ const { className, classnames: cx, classPrefix, clearable, disabled, hasError, result, value, placeholder, children, itemRender, allowInput, inputPlaceholder, onResultChange, onChange, onResultClick, translate: __, locale, onKeyPress, onFocus, onBlur, borderMode, mobileUI, hasDropDownArrow, actions, onClear, maxTagCount, overflowTagPopover, showArrow, popOverContainer, testIdBuilder, ...rest } = this.props; const isFocused = this.state.isFocused; return (
{Array.isArray(result) && result.length ? ( this.renderMultipleTags(result) ) : result && !Array.isArray(result) ? ( {isPlainObject(result) ? itemRender(result) : result} ) : allowInput && !disabled ? null : ( {__(placeholder || 'placeholder.noData')} )} {allowInput && !disabled ? ( 0 ? inputPlaceholder : placeholder : result ? '' : placeholder )} onFocus={this.handleFocus} onBlur={this.handleBlur} testIdBuilder={testIdBuilder?.getChild('input')} /> ) : null} {children}
{clearable && !disabled && (Array.isArray(result) ? result.length : result) ? (
) : null} {actions} {hasDropDownArrow && !mobileUI && ( )} {!allowInput && mobileUI && showArrow ? ( ) : null}
); } } export default themeable( localeable( uncontrollable(ResultBox, { value: 'onChange', result: 'onResultChange' }) ) );