Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增inputnumber组件formatter格式化属性 #863

Merged
merged 57 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
b0eeca3
fix: 修复 InputNumber 组件文档demo的样例模版
junjun666 Dec 9, 2022
2b1cbc7
feat: add animate新增动画/动效组件
junjun666 Dec 12, 2022
3bfd77f
feat: 代码合并
junjun666 Dec 13, 2022
a3abaa2
Merge branch 'jdf2e:main' into main
junjun666 Dec 29, 2022
5fcd23f
Merge branch 'jdf2e:main' into main
junjun666 Jan 3, 2023
0ef176c
feat: 新增watermark水印组件taro编译h5环境下适配
junjun666 Jan 3, 2023
36330de
Merge branch 'jdf2e:main' into main
junjun666 Jan 9, 2023
30b6e50
Merge branch 'jdf2e:main' into main
junjun666 Jan 11, 2023
62492da
fix: 修改inputnumber输入框删除不了的问题
junjun666 Jan 11, 2023
e97a728
Merge branch 'main' of https://github.com/junjun666/nutui-react
junjun666 Jan 11, 2023
1b723e8
Merge branch 'jdf2e:main' into main
junjun666 Jan 30, 2023
93c04e3
Merge branch 'jdf2e:main' into main
junjun666 Jan 30, 2023
75a17e1
fix: 修改uploader组件beforeUpload方法ts兼容
junjun666 Jan 30, 2023
2d6bb47
Merge branch 'jdf2e:main' into main
junjun666 Feb 2, 2023
ea4fcc3
fix: 修复小程序环境下overlay组件阴影滑动的问题
junjun666 Feb 3, 2023
654678f
Merge branch 'main' of https://github.com/junjun666/nutui-react
junjun666 Feb 3, 2023
4c4fa71
fix: 修改popup和popover组件相关ts校验
junjun666 Feb 3, 2023
a8b0374
Merge branch 'jdf2e:main' into main
junjun666 Feb 6, 2023
162df0d
fix: 修改多余行代码
junjun666 Feb 6, 2023
752ee8c
Merge branch 'jdf2e:main' into main
junjun666 Feb 9, 2023
e476167
Merge branch 'jdf2e:main' into main
junjun666 Feb 13, 2023
6f2a774
Merge branch 'jdf2e:main' into main
junjun666 Feb 13, 2023
8a6c094
Merge branch 'jdf2e:main' into main
junjun666 Feb 13, 2023
5fb1d19
Merge branch 'jdf2e:main' into main
junjun666 Feb 15, 2023
05963d7
fix: 新增form表单初始值
junjun666 Feb 15, 2023
99aa1c9
fix: 新增初始值demo示例
junjun666 Feb 15, 2023
57b0cea
Merge branch 'jdf2e:main' into main
junjun666 Feb 16, 2023
3353572
feat: 新增表单组件Form.useForm()创建form控制实例的功能
junjun666 Feb 16, 2023
212e94e
fix: form表单新增Form.useForm()的form prop属性
junjun666 Feb 16, 2023
bc643c6
fix: 提交文档修改
junjun666 Feb 16, 2023
ee8a0c2
Merge branch 'jdf2e:main' into main
junjun666 Feb 17, 2023
845236e
Merge branch 'jdf2e:main' into main
junjun666 Feb 17, 2023
f91a605
feat: 新增form-item的validator校验和重置提示状态功能
junjun666 Feb 17, 2023
ecba13b
Merge branch 'jdf2e:main' into main
junjun666 Feb 20, 2023
98b3eab
Merge branch 'jdf2e:main' into main
junjun666 Feb 20, 2023
0537bb3
fix: 修改taro的inputnumber输入框键盘输入异常问题
junjun666 Feb 20, 2023
b477397
Merge branch 'jdf2e:main' into main
junjun666 Feb 22, 2023
300de2b
Merge branch 'jdf2e:main' into main
junjun666 Feb 24, 2023
2a5c896
Merge branch 'jdf2e:main' into main
junjun666 Feb 28, 2023
0b6343f
Merge branch 'jdf2e:main' into main
junjun666 Mar 1, 2023
f83b521
Merge branch 'jdf2e:main' into main
junjun666 Mar 1, 2023
f771ac7
Merge branch 'jdf2e:main' into main
junjun666 Mar 1, 2023
8ba78dc
Merge branch 'jdf2e:main' into main
junjun666 Mar 6, 2023
d2290eb
Merge branch 'jdf2e:main' into main
junjun666 Mar 9, 2023
130c989
fix: 修改steps样式
junjun666 Mar 9, 2023
db80ab1
Merge branch 'jdf2e:main' into main
junjun666 Mar 14, 2023
659ce0b
Merge branch 'jdf2e:main' into main
junjun666 Mar 16, 2023
5d85a11
Merge branch 'jdf2e:main' into main
junjun666 Mar 20, 2023
bc1622a
Merge branch 'jdf2e:main' into main
junjun666 Mar 20, 2023
f20b2c0
Merge branch 'jdf2e:main' into main
junjun666 Mar 21, 2023
cc34d7f
Merge branch 'jdf2e:main' into main
junjun666 Mar 21, 2023
9f55703
Merge branch 'jdf2e:main' into main
junjun666 Mar 21, 2023
eba79b0
Merge branch 'jdf2e:main' into main
junjun666 Mar 23, 2023
042debd
feat: 新增inputnumber组件formatter格式化属性
junjun666 Mar 30, 2023
65c3cd5
Merge branch 'jdf2e:main' into main
junjun666 Mar 30, 2023
082b59b
Merge branch 'main' into feat/inputnumber
junjun666 Mar 30, 2023
ad89f38
fix: 增加小数点的适配
junjun666 Mar 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/packages/inputnumber/demo.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.format-width {
input,
.nut-number-input {
--nutui-inputnumber-input-width: 60px;
}
}
27 changes: 27 additions & 0 deletions src/packages/inputnumber/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InputNumber } from './inputnumber'
import ConfigProvider from '@/packages/configprovider'
import Cell from '@/packages/cell'
import Toast from '@/packages/toast'
import './demo.scss'

interface ValState {
val1: number | string
Expand All @@ -29,6 +30,7 @@ interface T {
'3a42134b': string
'65bafb1d': string
'7e2394ae': string
'7e2394be': string
}

const customTheme = {
Expand Down Expand Up @@ -63,6 +65,7 @@ const InputNumberDemo = () => {
'3a42134b': '支持小数点',
'65bafb1d': '支持异步修改',
'7e2394ae': '自定义按钮大小',
'7e2394be': '支持formatter',
},
'zh-TW': {
'6333c786': '超出限制事件觸發',
Expand All @@ -77,6 +80,7 @@ const InputNumberDemo = () => {
'3a42134b': '支持小數點',
'65bafb1d': '支持異步修改',
'7e2394ae': '自定義按鈕大小',
'7e2394be': '支持formatter',
},
'en-US': {
'6333c786': 'Exceeded limit event triggered',
Expand All @@ -91,6 +95,7 @@ const InputNumberDemo = () => {
'3a42134b': 'support decimal point',
'65bafb1d': 'Support for asynchronous modification',
'7e2394ae': 'custom button size',
'7e2394be': 'support formatter',
},
})

Expand Down Expand Up @@ -181,6 +186,28 @@ const InputNumberDemo = () => {
inputWidth="50"
/>
</Cell>

<h2>支持formatter</h2>
<Cell>
<InputNumber
className="format-width"
modelValue="1000"
min={10}
max={15020}
formatter={(value) =>
`$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
/>
</Cell>
<Cell>
<InputNumber
className="format-width"
modelValue="100"
min={0}
max={100}
formatter={(value) => `${value}%`}
/>
</Cell>
</div>
</>
)
Expand Down
34 changes: 34 additions & 0 deletions src/packages/inputnumber/doc.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,39 @@ export default App;
```
:::

### support formatter

:::demo
```tsx
import React from "react";
import { InputNumber } from '@nutui/nutui-react';

const App = () => {
return (
<>
<InputNumber
style={{"--nutui-inputnumber-input-width": "60px"}}
modelValue="1000"
min={10}
max={15020}
formatter={(value) =>
`$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
/>
<InputNumber
style={{"--nutui-inputnumber-input-width": "60px"}}
modelValue="100"
min={0}
max={100}
formatter={(value) => `${value}%`}
/>
</>
)
}
export default App;
```
:::

## API

### Props
Expand All @@ -220,6 +253,7 @@ export default App;
| disabled | Disable all features | boolean | `false` |
| readonly | Read only status disables input box operation behavior | boolean | `false` |
| isAsync | Support for asynchronous modification | boolean | `false` |
| formatter`v1.4.14` | Specifies the format of the value displayed in the input box | function(value: number | string): string | - |

### Events

Expand Down
34 changes: 34 additions & 0 deletions src/packages/inputnumber/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,39 @@ export default App;
```
:::

### 支持formatter

:::demo
```tsx
import React from "react";
import { InputNumber } from '@nutui/nutui-react';

const App = () => {
return (
<>
<InputNumber
style={{"--nutui-inputnumber-input-width": "60px"}}
modelValue="1000"
min={10}
max={15020}
formatter={(value) =>
`$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
/>
<InputNumber
style={{"--nutui-inputnumber-input-width": "60px"}}
modelValue="100"
min={0}
max={100}
formatter={(value) => `${value}%`}
/>
</>
)
}
export default App;
```
:::

## API

### Props
Expand All @@ -265,6 +298,7 @@ export default App;
| disabled | 禁用所有功能 | boolean | `false` |
| readonly | 只读状态禁用输入框操作行为 | boolean | `false` |
| isAsync | 支持异步修改 | boolean | `false` |
| formatter`v1.4.14` | 指定输入框展示值的格式 | function(value: number | string): string | - |

### Events

Expand Down
138 changes: 117 additions & 21 deletions src/packages/inputnumber/inputnumber.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {
useState,
useEffect,
useRef,
FunctionComponent,
ChangeEvent,
FocusEvent,
Expand All @@ -24,6 +25,7 @@ export interface InputNumberProps extends BasicComponent {
isAsync: boolean
className: string
style: React.CSSProperties
formatter?: (displayValue: string | number) => string
add: (e: MouseEvent) => void
reduce: (e: MouseEvent) => void
overlimit: (e: MouseEvent) => void
Expand Down Expand Up @@ -77,6 +79,7 @@ export const InputNumber: FunctionComponent<
isAsync,
className,
style,
formatter,
add,
reduce,
change,
Expand All @@ -97,9 +100,15 @@ export const InputNumber: FunctionComponent<
...props,
}
const [inputValue, setInputValue] = useState(modelValue)
const inputRef = useRef('')
useEffect(() => {
setInputValue(modelValue)
}, [modelValue])
if (formatter) {
inputRef.current = formatter(modelValue)
setInputValue(formatter(modelValue))
} else {
setInputValue(modelValue)
}
}, [modelValue, formatter])

const b = bem('inputnumber')
const classes = classNames(
Expand All @@ -114,13 +123,21 @@ export const InputNumber: FunctionComponent<
...style,
}
const addAllow = (value = inputValue) => {
if (formatter) {
const numValue = String(value).replace(/[^0-9|\.]/gi, '')
return Number(numValue) < Number(max) && !disabled
}
if (value || typeof value === 'number') {
return value < Number(max) && !disabled
}
return false
}

const reduceAllow = (value = inputValue) => {
if (formatter) {
const numValue = String(value).replace(/[^0-9|\.]/gi, '')
return Number(numValue) > Number(min) && !disabled
}
if (value || typeof value === 'number') {
return value > Number(min) && !disabled
}
Expand Down Expand Up @@ -148,11 +165,17 @@ export const InputNumber: FunctionComponent<
change && change(outputValue, e)
if (!isAsync) {
if (Number(outputValue) < Number(min)) {
setInputValue(Number(min))
formatter
? setInputValue(formatter(Number(min)))
: setInputValue(Number(min))
} else if (Number(outputValue) > Number(max)) {
setInputValue(Number(max))
formatter
? setInputValue(formatter(Number(max)))
: setInputValue(Number(max))
} else {
setInputValue(outputValue)
formatter
? setInputValue(formatter(outputValue))
: setInputValue(outputValue)
}
}
}
Expand All @@ -161,8 +184,15 @@ export const InputNumber: FunctionComponent<
onReduce && onReduce(e)
reduce && reduce(e)
if (reduceAllow()) {
const outputValue = Number(inputValue) - Number(step)
emitChange(outputValue, e)
if (formatter) {
const numValue = String(inputValue).replace(/[^0-9|\.]/gi, '')
const outputValue = Number(numValue) - Number(step)
inputRef.current = formatter(outputValue)
emitChange(outputValue, e)
} else {
const outputValue = Number(inputValue) - Number(step)
emitChange(outputValue, e)
}
} else {
onOverlimit && onOverlimit(e)
overlimit && overlimit(e)
Expand All @@ -173,8 +203,15 @@ export const InputNumber: FunctionComponent<
onAdd && onAdd(e)
add && add(e)
if (addAllow()) {
const outputValue = Number(inputValue) + Number(step)
emitChange(outputValue, e)
if (formatter) {
const numValue = String(inputValue).replace(/[^0-9|\.]/gi, '')
const outputValue = Number(numValue) + Number(step)
inputRef.current = formatter(outputValue)
emitChange(outputValue, e)
} else {
const outputValue = Number(inputValue) + Number(step)
emitChange(outputValue, e)
}
} else {
onOverlimit && onOverlimit(e)
overlimit && overlimit(e)
Expand All @@ -194,6 +231,48 @@ export const InputNumber: FunctionComponent<
}
}

const changeFormatValue = (e: ChangeEvent<HTMLInputElement>) => {
const input = e.target.value

const numReg = new RegExp('^[0-9]*$')
const numValue = input.replace(/[^0-9|\.]/gi, '')

if (formatter) {
if (!numReg.test(input[0]) && numValue) {
setInputValue(formatter(numValue))
} else if (!numReg.test(input[0]) && !numValue) {
setInputValue(input)
} else if (numReg.test(input[0])) {
console.log('inputRef.current', inputRef.current)
console.log('formatter(numValue)', formatter(numValue))

// 针对于100%这种尾字符例子,直接删除会进行匹配
if (formatter(numValue) === inputRef.current) {
setInputValue(numValue)
} else {
setInputValue(formatter(numValue))
inputRef.current = formatter(numValue)
}
}
}
}

const burFormatValue = (e: ChangeEvent<HTMLInputElement>) => {
const input = e.target.value

const numReg = new RegExp('^[0-9]*$')
const numValue = input.replace(/[^0-9|\.]/gi, '')
if (formatter) {
if (formatter(numValue) === input) {
emitChange(numValue, e)
return
}
if (!numReg.test(input) || !input) {
setInputValue(formatter(''))
}
}
}

const focusValue = (e: FocusEvent<HTMLInputElement>) => {
if (disabled) return
if (readonly) return
Expand Down Expand Up @@ -227,18 +306,35 @@ export const InputNumber: FunctionComponent<
onClick={reduceNumber}
/>
</div>
<input
type="number"
min={min}
max={max}
style={{ width: pxCheck(inputWidth) }}
disabled={disabled}
readOnly={readonly}
value={inputValue}
onInput={changeValue}
onBlur={burValue}
onFocus={focusValue}
/>
<>
{formatter ? (
<input
type="text"
min={min}
max={max}
style={{ width: pxCheck(inputWidth) }}
disabled={disabled}
readOnly={readonly}
value={inputValue}
onInput={changeFormatValue}
onBlur={burFormatValue}
onFocus={focusValue}
/>
) : (
<input
type="number"
min={min}
max={max}
style={{ width: pxCheck(inputWidth) }}
disabled={disabled}
readOnly={readonly}
value={inputValue}
onInput={changeValue}
onBlur={burValue}
onFocus={focusValue}
/>
)}
</>
<div className="nut-input-add">
<Icon
classPrefix={iconClassPrefix}
Expand Down