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): add NInputNumber slots #626

Merged
merged 4 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Feats

- `n-message` add `destroyAll` method.
- `n-input-number` add `prefix`, `suffix` slots, closes [#609](https://github.com/TuSimple/naive-ui/issues/609).

### Fixes

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Feats

- `n-message` 增加 `destroyAll` 方法
- `n-input-number` 增加 `prefix`、`suffix` slots, 关闭 [#609](https://github.com/TuSimple/naive-ui/issues/609)

### Fixes

Expand Down
29 changes: 29 additions & 0 deletions src/input-number/demos/enUS/icon.demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Prefix & Suffix

Add content to prefix and suffix.

```html
<n-space vertical>
<n-input-number v-model:value="value">
<template #prefix>$</template>
</n-input-number>
<n-input-number v-model:value="value">
<template #suffix>%</template>
</n-input-number>
<n-input-number :show-button="false" v-model:value="value">
<template #suffix>%</template>
</n-input-number>
</n-space>
```

```js
import { defineComponent, ref } from 'vue'

export default defineComponent({
setup () {
return {
value: ref(0)
}
}
})
```
8 changes: 8 additions & 0 deletions src/input-number/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ If you want just input number, use it.
basic
disabled
event
icon
min-max
size
step
Expand All @@ -33,3 +34,10 @@ show-button
| on-blur | `(event: FocusEvent) => void` | `undefined` | Callback when blur. |
| on-focus | `(event: FocusEvent) => void` | `undefined` | Callback when focused. |
| on-update:value | `(value: number \| null) => void` | `undefined` | Callback when the component's value changes. |

### Input Slots

| Name | Parameters | Description |
| ------ | ---------- | ------------------------------ |
| prefix | `()` | Input box prefix content slot. |
| suffix | `()` | Input box suffix content slot. |
29 changes: 29 additions & 0 deletions src/input-number/demos/zhCN/icon.demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 前缀 & 后缀

在前缀后缀添加内容

```html
<n-space vertical>
<n-input-number v-model:value="value">
<template #prefix>¥</template>
</n-input-number>
<n-input-number v-model:value="value">
<template #suffix>%</template>
</n-input-number>
<n-input-number :show-button="false" v-model:value="value">
<template #suffix>%</template>
</n-input-number>
</n-space>
```

```js
import { defineComponent, ref } from 'vue'

export default defineComponent({
setup () {
return {
value: ref(0)
}
}
})
```
8 changes: 8 additions & 0 deletions src/input-number/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
basic
disabled
event
icon
min-max
size
step
Expand All @@ -34,3 +35,10 @@ debug
| on-blur | `(event: FocusEvent) => void` | `undefined` | 移除焦点的回调 |
| on-focus | `(event: FocusEvent) => void` | `undefined` | 获取焦点的回调 |
| on-update:value | `(value: number \| null) => void` | `undefined` | 组件值发生变化的回调 |

### Slots

| 属性 | 类型 | 说明 |
| ------ | ---- | ------------------ |
| prefix | `()` | 输入框头部内容插槽 |
| suffix | `()` | 输入框尾部内容插槽 |
17 changes: 11 additions & 6 deletions src/input-number/src/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { warn, call, MaybeArray, ExtractPublicPropTypes } from '../../_utils'
import { inputNumberLight, InputNumberTheme } from '../styles'
import { parse, validator, format, parseNumber } from './utils'
import type { OnUpdateValue } from './interface'
import style from './styles/input-number.cssr'

const inputNumberProps = {
...(useTheme.props as ThemeProps<InputNumberTheme>),
Expand Down Expand Up @@ -68,7 +69,7 @@ export default defineComponent({
const themeRef = useTheme(
'InputNumber',
'InputNumber',
undefined,
style,
inputNumberLight,
props,
mergedClsPrefixRef
Expand Down Expand Up @@ -368,9 +369,13 @@ export default defineComponent({
onKeydown={this.handleKeyDown}
onMousedown={this.handleMouseDown}
>
{this.showButton
? {
suffix: () => [
{{
prefix: this.$slots.prefix,
suffix: this.showButton
? () => [
<span class={`${mergedClsPrefix}-input-number-suffix`}>
{{ default: this.$slots.suffix }}
</span>,
<NButton
text
disabled={!this.minusable || this.disabled}
Expand Down Expand Up @@ -408,8 +413,8 @@ export default defineComponent({
}}
</NButton>
]
}
: null}
: this.$slots.suffix
}}
</NInput>
</div>
)
Expand Down
8 changes: 8 additions & 0 deletions src/input-number/src/styles/input-number.cssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { cB, c } from '../../../_utils/cssr'

export default c([
cB('input-number-suffix', `
display: inline-block;
margin-right: 10px;
`)
])
13 changes: 13 additions & 0 deletions src/input-number/tests/InputNumber.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,17 @@ describe('n-input-number', () => {
expect(onBlur).toHaveBeenCalledTimes(1)
wrapper.unmount()
})

it('should work with `prefix` & `suffix` slots', async () => {
const wrapper = mount(NInputNumber, {
slots: { prefix: () => '$', suffix: () => '%' }
})
expect(wrapper.find('.n-input__prefix').exists()).toBe(true)
expect(wrapper.find('.n-input__prefix').text()).toBe('$')
expect(wrapper.find('.n-input__suffix').exists()).toBe(true)
expect(wrapper.find('.n-input-number-suffix-has-button').exists()).toBe(
true
)
expect(wrapper.find('.n-input-number-suffix-has-button').text()).toBe('%')
})
})
2 changes: 1 addition & 1 deletion src/input/src/styles/input.cssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export default c([
transition: color .3s var(--bezier);
flex-wrap: nowrap;
flex-shrink: 0;
line-height: 1.5;
line-height: var(--height);
white-space: nowrap;
display: inline-flex;
align-items: center;
Expand Down