Skip to content

Commit

Permalink
feat/fix: 优化 Inputtype='number' 开启 coin 下输入溢出内容时的交互逻辑、修复 `In…
Browse files Browse the repository at this point in the history
…put` 开启 `coin` 情况下初始化数据不展示千分号的问题 (#919)

* fix: 修复 `Input` 开启 `coin` 情况下初始化数据不展示千分号的问题

* feat: 优化 `Input` 在 `type='number'` 开启 `coin` 下输入溢出内容时的交互逻辑


---------

Co-authored-by: 颜宇浩 <[email protected]>
  • Loading branch information
KMS-Bismarck and 颜宇浩 authored Jan 10, 2025
1 parent 5ea9529 commit ef76054
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sheinx",
"private": true,
"version": "3.5.7-beta.6",
"version": "3.5.7-beta.7",
"description": "A react library developed with sheinx",
"module": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/base/src/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Input = (props: InputProps) => {
numType: commonProps.numType,
trim: commonProps.trim ?? config.trim ?? false,
// 移除 formName,避免渲染到原生 input 上
formName: undefined
formName: undefined,
};
const inputFormatProps = useInputFormat({
value: commonProps.value,
Expand All @@ -30,14 +30,14 @@ const Input = (props: InputProps) => {
const forwardProps = util.removeProps(commonProps, {
...inputFormatParams,
});

return (
<SimpleInputInput
{...forwardProps}
{...inputFormatProps}
value={inputFormatProps.value ?? ''}
hasSuffix={!!props.suffix}
onKeyDown={e => {
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.defaultPrevented) {
const value = (e.target as HTMLInputElement).value;
props.onChange?.(value);
Expand Down
10 changes: 9 additions & 1 deletion packages/hooks/src/components/use-input/use-input-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const useInputFormat = (props: InputFormatProps) => {
coin,
cancelBlurChange,
} = props;
const [showCoin, setShowCoin] = React.useState(false);
const [showCoin, setShowCoin] = React.useState(coin);
function isValidNumber(val: string) {
const { numType } = props;
const noNeg = numType === 'non-negative' || numType === 'positive';
Expand All @@ -44,6 +44,7 @@ const useInputFormat = (props: InputFormatProps) => {
}

const noNeg = numType === 'non-negative' || numType === 'positive';

const regExp = new RegExp(
`^(${noNeg ? '' : '-'})?(\\d${regLength(integerLimit)})(${
digits !== 0 ? `\\.\\d${regLength(digits)}` : ''
Expand All @@ -52,6 +53,13 @@ const useInputFormat = (props: InputFormatProps) => {
);

value = value.replace(regExp, '$1$2$3');

// 修正小数位数
const _value = v.split('.');
const __value = value.split('.');
if (_value[1] !== undefined && __value[1] === undefined) {
value = `${value}.${_value[1]}`;
}
}
onChange(value);
});
Expand Down
11 changes: 11 additions & 0 deletions packages/shineout/src/input/__doc__/changelog.cn.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 3.5.7-beta.7
2025-01-10

### 🐞 BugFix

- 修复 `Input` 开启 `coin` 情况下初始化数据不展示千分号的问题 ([#919](https://github.com/sheinsight/shineout-next/pull/919))

### 💎 Enhancement

- 优化 `Input``type='number'` 开启 `coin` 下输入溢出内容时的交互逻辑 ([#919](https://github.com/sheinsight/shineout-next/pull/919))

## 3.5.7-beta.5
2025-01-09

Expand Down
2 changes: 1 addition & 1 deletion packages/shineout/src/input/__test__/input.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('input number', () => {
}
const result =
v.indexOf('.') >= 0
? `${v.split('.')[0].substring(0, integerLimit)}`
? `${v.split('.')[0].substring(0, integerLimit)}.${v.split('.')[1]}`
: v.substring(0, integerLimit);
expect(input?.getAttribute('value')).toBe(result);
});
Expand Down

0 comments on commit ef76054

Please sign in to comment.