Skip to content

Commit

Permalink
fix: 字段选择组件多选问题修复 (#132)
Browse files Browse the repository at this point in the history
* fix: 字段选择组件多选问题修复

* fix: 类型报错

* chore: 优化代码

---------

Co-authored-by: yxh01132861 <[email protected]>
Co-authored-by: yunji <[email protected]>
  • Loading branch information
3 people authored Jan 24, 2024
1 parent e0a3134 commit cb01b6f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions packages/li-p2/src/components/Formily/FieldSelect/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,32 @@ import React, { useState } from 'react';
import useStyle from './style';
import type { FieldSelectOptionType } from './types';

const InternalSelect: React.FC<SelectProps<string, FieldSelectOptionType>> = (props) => {
const InternalSelect: React.FC<SelectProps<string | string[], FieldSelectOptionType>> = (props) => {
const { options, open: outerOpen = false, ...prop } = props;
const prefixCls = usePrefixCls('formily-field-select');
const [wrapSSR, hashId] = useStyle(prefixCls);
const [internalOpen, setInternalOpen] = useState(outerOpen);
// Select 是否多选
const isMultiple = prop.mode;

useUpdateEffect(() => {
setInternalOpen(outerOpen);
}, [outerOpen]);

const onOptionClick = (val: string) => {
if (props.onChange) {
if (!props.onChange) return;
// 单选
if (!isMultiple) {
props.onChange(val, options ?? []);
setInternalOpen(false);
} else {
// 多选
if (!props.value) {
props.onChange([val], options ?? []);
} else if (Array.isArray(props.value)) {
const selectValues = Array.from(new Set(props.value.concat(val)));
props.onChange(selectValues, options ?? []);
}
}
};

Expand All @@ -41,10 +53,11 @@ const InternalSelect: React.FC<SelectProps<string, FieldSelectOptionType>> = (pr
<div className={`${prefixCls}-dropdown`} style={{ height: dropdownHeight }}>
<div className={`${prefixCls}-dropdown-container`}>
{options?.map((item, index) => {
const isSelected = isMultiple ? props.value?.includes(item.value) : item.value === props.value;
return (
<div
className={cls(`${prefixCls}-item`, hashId, {
[`${prefixCls}-item_selected`]: item.value === props.value,
[`${prefixCls}-item_selected`]: isSelected,
})}
key={index}
onClick={() => onOptionClick(item.value)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React from 'react';
import InternalSelect from './Select';
import type { FieldSelectOptionType } from './Select/types';

const FieldSelect: ReactFC<SelectProps<string, FieldSelectOptionType>> = connect(
const FieldSelect: ReactFC<SelectProps<string | string[], FieldSelectOptionType>> = connect(
InternalSelect,
mapProps(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const FilterContent: React.FC<FilterContentProps> = (props) => {
const openFieldSelect = outterValue.field ? false : true;

// 筛选字段变更
const onFieldChange = (field: string) => {
const onFieldChange = (field: string | string[]) => {
const _field = options.find((item) => item.value === field);
if (_field) {
setDomain(_field?.domain ?? []);
Expand Down

0 comments on commit cb01b6f

Please sign in to comment.