Skip to content

Commit

Permalink
fix(SearchSelect): 修复 form 在没有 initialValue 情况默认赋值了空字符问题 (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
nullptr-z committed Mar 10, 2022
1 parent 9bfa248 commit d5a2bd7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
42 changes: 21 additions & 21 deletions packages/react-popover/README.md
Expand Up @@ -38,7 +38,7 @@ class Demo extends React.Component {
}
render() {
return (
<Row style={{ alignItems:'center' }}>
<Row style={{ alignItems: 'center' }}>
<Popover
trigger="click"
placement="top"
Expand All @@ -60,27 +60,27 @@ class Demo extends React.Component {
>
<Button active={this.state.isOpen}>弹出目标</Button>
</Popover>
<div style={{marginLeft:20}}>
<Popover
trigger="click"
placement="top"
content={
<Card bordered={false} title="Card标题" extra={<a href="#">更多</a>} style={{ width: 200 }}>
<div>Are you sure you want to delete these items? You won't be able to recover them.</div>
<div style={{ display: "flex", justifyContent: "flex-end", marginTop: 15 }}>
<Button size="small" onClick={this.onClick.bind(this)}>
Cancel
</Button>
<Button type="danger" size="small" onClick={this.onClick.bind(this)}>
Delete
</Button>
</div>
</Card>
}
>
<Icon type="setting" color="#343a40" style={{ fontSize: 20 }} />
</Popover>
<div style={{ marginLeft: 20 }}>
<Popover
trigger="click"
placement="top"
content={
<Card bordered={false} title="Card标题" extra={<a href="#">更多</a>} style={{ width: 200 }}>
<div>Are you sure you want to delete these items? You won't be able to recover them.</div>
<div style={{ display: "flex", justifyContent: "flex-end", marginTop: 15 }}>
<Button size="small" onClick={this.onClick.bind(this)}>
Cancel
</Button>
<Button type="danger" size="small" onClick={this.onClick.bind(this)}>
Delete
</Button>
</div>
</Card>
}
>
<Icon type="setting" color="#343a40" style={{ fontSize: 20 }} />
</Popover>
</div>
</Row>
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-search-select/README.md
Expand Up @@ -237,7 +237,7 @@ ReactDOM.render(<Demo />, _mount_);
| placeholder | 选择框默认文字 | String | - | - |
| maxTagCount | 多选模式下展示tag的个数,默认所有 | number | - | - |
| labelInValue | 开启会把 Select 的 value 类型从 `string/number` 变为 `{ value: string/number, label: string }` | Boolean | `false` | - |
| showSearch | 使单选模式可搜索 | Boolean | - | - |
| showSearch | 是否可搜索 | Boolean | - | - |
| size | 选择框尺寸 | Enum{large, default, small } | `default` | - |
| tagProps | 将参数传递给 [`<Tag>`](https://uiwjs.github.io/#/components/tag) 组件 | `TagProps` | `{}` | `4.13.0` |
| onChange | 选中 option,或 input 的 value,调用此函数 | function(value:String \| Number \| String[] \| Number[] \| LabeledValue \| LabeledValue[]) | - | - |
Expand Down
28 changes: 20 additions & 8 deletions packages/react-search-select/src/index.tsx
Expand Up @@ -72,17 +72,21 @@ export default function SearchSelect(props: SearchSelectProps) {
);
const divRef = useRef<HTMLDivElement>(null);

const valueVerify = (value: ValueType | Array<ValueType> | undefined) => {
return value !== undefined && value !== '';
};

const valueRef = useRef<Array<SearchSelectOptionData>>();
valueRef.current = useMemo(() => selectedValue, [selectedValue]);

useEffect(() => {
if (value === undefined && defaultValue !== undefined) {
selectedValueChange(defaultValue);
if (!valueVerify(value) && valueVerify(defaultValue)) {
selectedValueChange(defaultValue!);
}
}, []);

useEffect(() => {
if (value !== undefined) {
if (valueVerify(value)) {
selectedValueChange(value!);
}
}, [JSON.stringify(value)]);
Expand Down Expand Up @@ -125,6 +129,11 @@ export default function SearchSelect(props: SearchSelectProps) {
return values;
}

const selectedLabelChange = (value: string) => {
setSelectedLabel(value);
showSearch && onSearch?.(value);
};

function handleItemClick(item: SearchSelectOptionData) {
setInnerIsOpen(false);
const values = [item];
Expand Down Expand Up @@ -160,9 +169,10 @@ export default function SearchSelect(props: SearchSelectProps) {
// handle change
function handleInputChange(value: string) {
setInnerIsOpen(true);
setSelectedLabel(value);
setSelectIconType(showSearch && value ? 'loading' : '');
showSearch && onSearch && onSearch(value);
// setSelectedLabel(value);
// showSearch && onSearch && onSearch(value);
selectedLabelChange(value);
}
// 清除选中的值
function resetSelectedValue(e: React.MouseEvent<any, MouseEvent>) {
Expand All @@ -185,9 +195,10 @@ export default function SearchSelect(props: SearchSelectProps) {
}
}

function onVisibleChange(open: boolean) {
setInnerIsOpen(open);
if (!open) setSelectedLabel('');
function onVisibleChange(isOpen: boolean) {
const selectedValue = valueRef.current as SearchSelectOptionData[];
setInnerIsOpen(isOpen);
if (!isOpen) selectedLabelChange('');
if (!isMultiple && selectedValue.length > 0) {
setSelectedLabel(selectedValue[0].label);
}
Expand Down Expand Up @@ -235,6 +246,7 @@ export default function SearchSelect(props: SearchSelectProps) {
ref={divRef}
onMouseOver={() => renderSelectIcon('enter')}
onMouseLeave={() => renderSelectIcon('leave')}
onClick={() => inputRef.current?.focus()}
style={{ width: '100%', maxWidth: 'none', ...style }}
>
{isMultiple ? (
Expand Down

0 comments on commit d5a2bd7

Please sign in to comment.