Skip to content

Commit

Permalink
ファイル名表示ロジックとイベントハンドラーのロジックを簡潔化
Browse files Browse the repository at this point in the history
  • Loading branch information
hagiya0121 committed Dec 8, 2024
1 parent 7d82332 commit 97b19ed
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions app/javascript/fileinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,10 @@ function initializeDiplomaUploadField() {
removeButton.style.display = fileLink ? 'block' : 'none'

const updateDisplayedFileName = (name = '') => {
if (name) {
fileName.textContent = name
fileName.style.display = 'block'
removeButton.style.display = 'block'
} else {
fileName.textContent = ''
fileName.style.display = 'none'
removeButton.style.display = 'none'
}
fileName.textContent = name
const displayedStatus = name ? 'block' : 'none'
fileName.style.display = displayedStatus
removeButton.style.display = displayedStatus
}

removeButton.addEventListener('click', () => {
Expand All @@ -109,12 +104,9 @@ function initializeDiplomaUploadField() {
})

fileInput.addEventListener('change', () => {
if (fileInput.files && fileInput.files[0]) {
updateDisplayedFileName(fileInput.files[0].name)
removeFlag.value = '0'
} else {
updateDisplayedFileName()
}
const selectedFile = fileInput.files[0]
updateDisplayedFileName(selectedFile.name)
if (selectedFile) removeFlag.value = '0'
})
}

Expand Down

0 comments on commit 97b19ed

Please sign in to comment.