import { Controller } from '@hotwired/stimulus' export default class extends Controller { static targets = ["authorIdField", "preview", "image", "name", "form"] connect() { this.updateUI() } select(event) { const radio = event.target const { value: authorId, authorName, authorImageUrl } = radio.dataset if (this.hasAuthorIdFieldTarget) { this.authorIdFieldTarget.value = radio.value } this.updatePreview(authorName, authorImageUrl) this.updateUI() } updatePreview(authorName, authorImageUrl) { if (this.hasNameTarget) { this.nameTarget.textContent = authorName || '작성자를 선택하세요' } if (this.hasImageTarget) { if (authorImageUrl) { this.imageTarget.src = authorImageUrl this.imageTarget.alt = `${authorName}의 프로필 사진` this.imageTarget.classList.remove('hidden') } else { this.imageTarget.classList.add('hidden') } } } updateUI() { const hasAuthor = this.hasAuthorIdFieldTarget && this.authorIdFieldTarget.value.trim() if (this.hasPreviewTarget) { this.previewTarget.classList.remove('hidden') if (!hasAuthor && this.hasNameTarget && !this.nameTarget.textContent.trim()) { this.nameTarget.textContent = '작성자를 선택하세요' } } if (this.hasFormTarget) { this.formTarget.querySelectorAll('input:not([name="author_id"]), select, textarea, button') .forEach(el => el.disabled = !hasAuthor) } } }

AltStyle によって変換されたページ (->オリジナル) /