Skip to content

Commit

Permalink
fix(select): support 0 value to be selected
Browse files Browse the repository at this point in the history
  • Loading branch information
uyarn committed Dec 21, 2021
1 parent 4eacffc commit c716e92
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
40 changes: 19 additions & 21 deletions src/select/base/PopupContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,28 @@ const PopupContent = (props: SelectPopupProps) => {
if (!children && !props.options) return null;

const onSelect: SelectOptionProps['onSelect'] = (selectedValue, { label, selected, restData }) => {
if (selectedValue) {
const isValObj = valueType === 'object';
let objVal = {};
if (isValObj) {
objVal = { ...restData };
if (!keys?.label) {
Object.assign(objVal, { label });
}
if (!keys?.value) {
Object.assign(objVal, { value: selectedValue });
}
const isValObj = valueType === 'object';
let objVal = {};
if (isValObj) {
objVal = { ...restData };
if (!keys?.label) {
Object.assign(objVal, { label });
}
if (!keys?.value) {
Object.assign(objVal, { value: selectedValue });
}
}

if (multiple) {
// calc multiple select values
const values = getSelectValueArr(value, selectedValue, selected, valueType, keys, objVal);
onChange(values, { label });
} else {
// calc single select value
const selectVal = valueType === 'object' ? objVal : selectedValue;
if (multiple) {
// calc multiple select values
const values = getSelectValueArr(value, selectedValue, selected, valueType, keys, objVal);
onChange(values, { label });
} else {
// calc single select value
const selectVal = valueType === 'object' ? objVal : selectedValue;

onChange(selectVal, { label });
setShowPopup(!showPopup);
}
onChange(selectVal, { label });
setShowPopup(!showPopup);
}
};

Expand Down
35 changes: 16 additions & 19 deletions src/select/base/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import isFunction from 'lodash/isFunction';
import get from 'lodash/get';
import isString from 'lodash/isString';

import { isNumber } from 'lodash';
import { useLocaleReceiver } from '../../locale/LocalReceiver';
import useConfig from '../../_util/useConfig';
import composeRefs from '../../_util/composeRefs';
Expand Down Expand Up @@ -94,9 +95,7 @@ const Select = forwardRefWithStatics(
const selectRef = useRef(null);
const overlayRef = useRef(null);

if (value) {
selectedLabel = getLabel(children, value, currentOptions, keys);
}
selectedLabel = getLabel(children, value, currentOptions, keys);

// 计算Select的宽高
useEffect(() => {
Expand Down Expand Up @@ -203,7 +202,7 @@ const Select = forwardRefWithStatics(
<span
className={classNames(
className,
{ [`${name}-placeholder`]: !value || (Array.isArray(value) && value.length < 1) },
{ [`${name}-placeholder`]: (!value && !isNumber(value)) || (Array.isArray(value) && value.length < 1) },
{
[`${name}-selectedSingle`]: selectedLabel,
},
Expand Down Expand Up @@ -260,25 +259,23 @@ const Select = forwardRefWithStatics(
return !filterable ? defaultLabel : null;
};

const renderInput = () => {
const isEmpty = !value || (Array.isArray(value) && value.length === 0);
return (
<Input
value={isString(inputVal) ? inputVal : selectedLabel}
placeholder={isEmpty ? placeholder || '-请选择-' : undefined}
className={`${name}-input`}
onChange={handleInputChange}
onFocus={(_, context) => onFocus?.({ value, e: context?.e })}
onBlur={(_, context) => onBlur?.({ value, e: context?.e })}
onEnter={(_, context) => onEnter?.({ inputValue: inputVal, value, e: context?.e })}
/>
);
};
const renderInput = () => (
<Input
value={isString(inputVal) ? inputVal : selectedLabel}
placeholder={multiple && get(value, 'length') > 0 ? null : selectedLabel || placeholder || '-请选择-'}
className={`${name}-input`}
onChange={handleInputChange}
onFocus={(_, context) => onFocus?.({ value, e: context?.e })}
onBlur={(_, context) => onBlur?.({ value, e: context?.e })}
onEnter={(_, context) => onEnter?.({ inputValue: inputVal, value, e: context?.e })}
/>
);

const onInputClick = (e: React.MouseEvent) => {
e.preventDefault();
if (!disabled) {
setShowPopup(!showPopup);
setShowPopup(true);
setInputVal('');
}
};

Expand Down

0 comments on commit c716e92

Please sign in to comment.