完善 ResultBox

This commit is contained in:
2betop 2020-05-15 09:37:26 +08:00
parent c4dd46bafc
commit 1df2cfb5e6
1 changed files with 18 additions and 3 deletions

View File

@ -14,6 +14,7 @@ export interface ResultBoxProps
onChange?: (value: Array<any>) => void; onChange?: (value: Array<any>) => void;
allowInput?: boolean; allowInput?: boolean;
inputPlaceholder: string;
inputValue?: string; inputValue?: string;
onInputChange?: (value: string) => void; onInputChange?: (value: string) => void;
} }
@ -21,10 +22,11 @@ export interface ResultBoxProps
export class ResultBox extends React.Component<ResultBoxProps> { export class ResultBox extends React.Component<ResultBoxProps> {
static defaultProps: Pick< static defaultProps: Pick<
ResultBoxProps, ResultBoxProps,
'clearable' | 'placeholder' | 'itemRender' 'clearable' | 'placeholder' | 'itemRender' | 'inputPlaceholder'
> = { > = {
clearable: false, clearable: false,
placeholder: '暂无结果', placeholder: '暂无结果',
inputPlaceholder: '手动输入内容',
itemRender: (option: any) => <span>{String(option.label)}</span> itemRender: (option: any) => <span>{String(option.label)}</span>
}; };
@ -62,6 +64,18 @@ export class ResultBox extends React.Component<ResultBoxProps> {
}); });
} }
@autobind
removeItem(e: React.MouseEvent<HTMLElement>) {
e.stopPropagation();
e.preventDefault();
const {value, onChange} = this.props;
const index = parseInt(e.currentTarget.getAttribute('data-index')!, 10);
const newValue = Array.isArray(value) ? value.concat() : [];
newValue.splice(index, 1);
onChange && onChange(newValue);
}
render() { render() {
const { const {
className, className,
@ -78,6 +92,7 @@ export class ResultBox extends React.Component<ResultBoxProps> {
onInputChange, onInputChange,
inputValue, inputValue,
allowInput, allowInput,
inputPlaceholder,
...rest ...rest
} = this.props; } = this.props;
const isFocused = this.state.isFocused; const isFocused = this.state.isFocused;
@ -98,7 +113,7 @@ export class ResultBox extends React.Component<ResultBoxProps> {
<span className={cx('ResultBox-valueLabel')}> <span className={cx('ResultBox-valueLabel')}>
{itemRender(item)} {itemRender(item)}
</span> </span>
<a> <a data-index={index} onClick={this.removeItem}>
<Icon icon="close" /> <Icon icon="close" />
</a> </a>
</div> </div>
@ -116,7 +131,7 @@ export class ResultBox extends React.Component<ResultBoxProps> {
onChange={this.handleInputChange} onChange={this.handleInputChange}
placeholder={ placeholder={
Array.isArray(value) && value.length Array.isArray(value) && value.length
? '手动输入内容' ? inputPlaceholder
: placeholder : placeholder
} }
onFocus={this.handleFocus} onFocus={this.handleFocus}