Skip to content

Commit

Permalink
enh(NcInputField): Support numeric values - if numeric also emit numeric
Browse files Browse the repository at this point in the history
If the type is set to number and a number is passed as value also emit a number instead of a string.

Co-authored-by: Grigorii K. Shartsev <me@shgk.me>
Co-authored-by: Ferdinand Thiessen <opensource@fthiessen.de>
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux and ShGKme committed Jan 22, 2024
1 parent 0e3e90d commit a41711a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/components/NcInputField/NcInputField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ For a list of all available props and attributes, please check the [HTMLInputEle
'input-field__input--error': error,
'input-field__input--pill': pill,
}]"
:value="value"
:value="value.toString()"
v-on="$listeners"
@input="handleInput">
<!-- Label -->
Expand Down Expand Up @@ -132,9 +132,10 @@ export default {
props: {
/**
* The value of the input field
* If type is 'number' and a number is passed as value than the type of `update:value` will also be 'number'
*/
value: {
type: String,
type: [String, Number],
required: true,
},
Expand Down Expand Up @@ -331,7 +332,7 @@ export default {
},
handleInput(event) {
this.$emit('update:value', event.target.value)
this.$emit('update:value', this.type === 'number' && typeof this.value === 'number' ? parseFloat(event.target.value, 10) : event.target.value)
},
handleTrailingButtonClick(event) {
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/components/NcInputField/NcInputField.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { shallowMount } from '@vue/test-utils'
import NcInputField from '../../../../src/components/NcInputField/index.js'

describe('NcInputField', () => {
it('should emit text when type is text', async () => {
const wrapper = shallowMount(NcInputField, {
propsData: {
label: 'label',
value: '',
type: 'text',
},
})

await wrapper.get('input').setValue('text')

expect(wrapper.emitted('update:value')).toEqual([['text']])
})

it('should emit text when type is number but value is text', () => {
const wrapper = shallowMount(NcInputField, {
propsData: {
value: '1',
type: 'number',
},
})

const input = wrapper.find('input').element as HTMLInputElement
input.value = '2'
input.dispatchEvent(new InputEvent('input'))

expect(wrapper.emitted('update:value')).toEqual([['2']])
})

it('should emit a number when type is number and value is number', () => {
const wrapper = shallowMount(NcInputField, {
propsData: {
value: 1,
type: 'number',
},
})

const input = wrapper.find('input').element as HTMLInputElement
input.value = '2'
input.dispatchEvent(new InputEvent('input'))

expect(wrapper.emitted('update:value')).toEqual([[2]])
})
})

0 comments on commit a41711a

Please sign in to comment.