Skip to content

Commit

Permalink
feat(filterpicker): filterPicker numberAttrSelect support positive in…
Browse files Browse the repository at this point in the history
…teger (#1954)
  • Loading branch information
berber1016 committed Apr 8, 2022
1 parent 0116dd3 commit 1b5f16f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface FilterAttrOverlayProps {
values: string[];
exprKey: string;
operationsOption?: operationsOptionType;
numType?: 'positivedecimal' | 'decimal';
numType?: 'positivedecimal' | 'decimal' | 'positiveInteger';
}

function usePrevious<T>(value: T): React.MutableRefObject<T | undefined> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@ interface NumberAttrSelectProps {
attrSelect: string;
attrChange: (v: any) => void;
values: string[];
type?: 'positivedecimal' | 'decimal';
type?: 'positivedecimal' | 'decimal' | 'positiveInteger';
}
function NumberAttrSelect(props: NumberAttrSelectProps) {
const { textObject: t } = useContext(FilterPickerContext);
const { attrSelect, attrChange, values, type } = props;
const [value, setValue] = useState<number | string>(values?.[0] ? parseFloat(values?.[0]) : 0);
const [value1, setValue1] = useState<number | string>(values?.[0] ? parseFloat(values?.[0]) : 0);
const [value2, setValue2] = useState<number | string>(values?.[1] ? parseFloat(values?.[1]) : 0);

const defaultValue = type === 'positiveInteger' ? '1' : '0'
const [value, setValue] = useState<number | string>(values?.[0] ? parseFloat(values?.[0]) : Number(defaultValue));
const [value1, setValue1] = useState<number | string>(values?.[0] ? parseFloat(values?.[0]) : Number(defaultValue));
const [value2, setValue2] = useState<number | string>(values?.[1] ? parseFloat(values?.[1]) : Number(defaultValue));

// 初始化attrValue值
useEffect(() => {
const num = values?.[0] && values?.[0] !== ' ' ? values?.[0] : '0';
setValue(parseFloat(values?.[0]) || 0);
setValue1(parseFloat(values?.[0]) || '0');
// checkRegExp 校验传入的初始值是否符合标准
const num = values?.[0] && values?.[0] !== ' ' && checkRegExp(type,values[0]) ? values?.[0] : defaultValue;
setValue(parseFloat(values?.[0]) || Number(defaultValue));
setValue1(parseFloat(values?.[0]) || defaultValue);
setValue2(Number.isNaN(parseFloat(values?.[1])) ? num : parseFloat(values?.[1]));
if (attrSelect === 'between' || attrSelect === 'not between') {
attrChange([num, values?.[1] || num]);
} else {
attrChange([num]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [attrSelect]);
}, [attrSelect, type]);


const checkRegExp = (numType: string | undefined, v: string) => {
const typeLowCase = numType?.toLowerCase();
Expand All @@ -50,6 +53,9 @@ function NumberAttrSelect(props: NumberAttrSelectProps) {
/^[0](\.)(\d+)?$/.test(`${v}`)
);
}
if(typeLowCase === 'positiveinteger'){
return /^[1-9]\d*$/.test(`${v}`);
}
return v === '-' || /^(-|\+)?[1-9]\d*?$/.test(`${v}`) || /^[0]$/.test(`${v}`);
};

Expand All @@ -62,7 +68,7 @@ function NumberAttrSelect(props: NumberAttrSelectProps) {
}
} else if (!v) {
setValue1(v);
attrChange(['0', value2]);
attrChange([defaultValue, value2]);
}
};

Expand All @@ -74,7 +80,7 @@ function NumberAttrSelect(props: NumberAttrSelectProps) {
attrChange([v]);
} else if (!v) {
setValue(v);
attrChange(['0']);
attrChange([defaultValue]);
}
};
// 设置区间方法
Expand All @@ -85,7 +91,7 @@ function NumberAttrSelect(props: NumberAttrSelectProps) {
attrChange([value1, v]);
} else if (!v) {
setValue2(v);
attrChange([value1, '0']);
attrChange([value1, defaultValue]);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface FilterConditionProps {
values: string[];
exprKey: string;
operationsOption?: operationsOptionType;
numType?: 'positivedecimal' | 'decimal';
numType?: 'positivedecimal' | 'decimal' | 'positiveInteger';
disabled?: boolean;
borderless?: boolean;
size?: 'large' | 'middle' | 'small';
Expand Down

1 comment on commit 1b5f16f

@vercel
Copy link

@vercel vercel bot commented on 1b5f16f Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

gio-design – ./

gio-design-git-master-growingio.vercel.app
gio-design.vercel.app
gio-design-growingio.vercel.app

Please sign in to comment.