Skip to content

Commit 21ba2c0

Browse files
committed
fix(display): restore text on empty values
1 parent 0cd194a commit 21ba2c0

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/eventHandlers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ function handleInputChange() {
2626

2727
if (label) {
2828
const element = findFirstChildNode(label)
29-
element.innerHTML = getSelectedFiles(this)
29+
const inputValue = getSelectedFiles(this)
30+
31+
if (inputValue.length) {
32+
element.innerHTML = inputValue
33+
} else {
34+
restoreDefaultText(this)
35+
}
3036
}
3137
}
3238

tests/units/eventHandlers.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ describe('eventHandlers.js', function () {
5757
input.dispatchEvent(new Event('change'))
5858
})
5959

60+
it('should restore default text if value is empty', function (done) {
61+
bsCustomFileInput.init()
62+
63+
var label = document.querySelector('.custom-file-label')
64+
65+
function firstListener() {
66+
expect(label.innerHTML).equal('myFakeFile.exe')
67+
input.removeEventListener('change', firstListener)
68+
69+
input.addEventListener('change', secondListener)
70+
input.value = ''
71+
input.dispatchEvent(new Event('change'))
72+
}
73+
74+
function secondListener() {
75+
expect(label.innerHTML).equal('Choose file')
76+
done()
77+
}
78+
79+
input.addEventListener('change', firstListener)
80+
81+
Object.defineProperty(input, 'value', {
82+
value: 'myFakeFile.exe',
83+
configurable: true,
84+
writable: true,
85+
})
86+
87+
input.dispatchEvent(new Event('change'))
88+
})
89+
6090
it('should change the label when files are selected', function (done) {
6191
bsCustomFileInput.init()
6292

0 commit comments

Comments
 (0)