修复:留言板新建按钮失效的 AlpineJS x-data 作用域问题

原因:按钮元素被移除了原先包裹它的 body-data 组件树中。
解决:将留言板的所有内容(包括表单和按钮)都包裹在一个顶级 x-data=showWriteForm... 容器内。
This commit is contained in:
2026-02-27 01:46:03 +08:00
parent db95ee0e29
commit 8063200f0b

View File

@@ -24,215 +24,216 @@
@section('body-data', "x-data=\"{ showWriteForm: false, towho: '{{ $defaultTo }}' }\"")
@section('content')
{{-- 验证错误信息 --}}
@if (isset($errors) && $errors->any())
<div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-4 mx-4 mt-4 shadow-sm">
<ul class="list-disc pl-5">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{{-- 写信/留言表单区 (Alpine 控制显隐) --}}
<div x-show="showWriteForm" x-collapse class="bg-white border-b border-gray-200 shadow-inner">
<div class="max-w-4xl mx-auto p-6">
<form action="{{ route('guestbook.store') }}" method="POST" class="space-y-4">
@csrf
<div class="flex items-center space-x-4">
<div class="flex-1">
<label class="block text-sm font-medium text-gray-700 mb-1">接收人 (留空或填"大家"表示公共留言)</label>
<input type="text" name="towho" x-model="towho" placeholder="系统自动处理"
class="w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
</div>
<div class="flex items-center h-full pt-6">
<label
class="flex items-center space-x-2 text-sm text-gray-700 cursor-pointer bg-pink-50 px-3 py-2 rounded-md hover:bg-pink-100 transition border border-pink-100">
<input type="checkbox" name="secret" value="1"
class="rounded text-pink-500 focus:ring-pink-500 w-4 h-4">
<span class="font-bold text-pink-700">🔒 悄悄话 (仅双方可见)</span>
</label>
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">正文内容 <span
class="text-red-500">*</span></label>
<textarea name="text_body" x-ref="textBody" rows="3" required
class="w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="相逢何必曾相识,留下您的足迹吧..."></textarea>
</div>
<div class="flex justify-end">
<button type="submit"
class="bg-indigo-600 hover:bg-indigo-700 text-white py-2 px-6 rounded-md shadow flex items-center font-bold">
<svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path>
</svg>
发送
</button>
</div>
</form>
</div>
</div>
{{-- 主体内容区 --}}
<div class="flex max-w-7xl mx-auto mt-4 px-4 sm:px-6 lg:px-8">
{{-- 左侧:分类导航 --}}
<div
class="w-64 bg-white border border-gray-200 rounded-lg shrink-0 hidden md:flex flex-col mb-10 self-start sticky top-20">
<div class="p-6 flex-1">
{{-- 新建留言按钮 --}}
<button
@click="showWriteForm = !showWriteForm; if(showWriteForm) setTimeout(() => $refs.textBody.focus(), 100)"
class="w-full bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-3 rounded-xl font-bold mb-6 shadow-md transition flex justify-center items-center">
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z">
</path>
</svg>
<span x-text="showWriteForm ? '取消撰写' : '写新留言'"></span>
</button>
<nav class="space-y-2">
<a href="{{ route('guestbook.index', ['tab' => 'public']) }}"
class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'public' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
<span class="mr-3 text-lg">🌍</span> 公共留言墙
</a>
<a href="{{ route('guestbook.index', ['tab' => 'inbox']) }}"
class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'inbox' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
<span class="mr-3 text-lg">📥</span> 我收件的
</a>
<a href="{{ route('guestbook.index', ['tab' => 'outbox']) }}"
class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'outbox' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
<span class="mr-3 text-lg">📤</span> 我发出的
</a>
</nav>
<div x-data="{ showWriteForm: false, towho: '{{ $defaultTo }}' }" class="flex flex-col flex-1 h-full">
{{-- 验证错误信息 --}}
@if (isset($errors) && $errors->any())
<div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-4 mx-4 mt-4 shadow-sm">
<ul class="list-disc pl-5">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
</div>
@endif
{{-- 右侧:留言流列表 --}}
<main class="flex-1 p-4 sm:p-0 md:pl-6 pb-20">
<div class="max-w-4xl mx-auto space-y-4">
@forelse($messages as $msg)
@php
$isSecret = $msg->secret == 1;
$isToMe = Auth::check() && $msg->towho === Auth::user()->username;
$isFromMe = Auth::check() && $msg->who === Auth::user()->username;
@endphp
<div
class="bg-white rounded-xl shadow-sm border {{ $isSecret ? 'border-pink-200' : 'border-gray-200' }} p-5 relative group overflow-hidden">
@if ($isSecret)
<div
class="absolute top-0 right-0 bg-pink-100 text-pink-700 text-[10px] font-bold px-2 py-1 rounded-bl-lg">
🔒 私密信件
</div>
@endif
<div class="flex justify-between items-start mb-2">
<div class="flex items-center text-sm">
<span class="font-bold text-indigo-700">{{ $msg->who }}</span>
<span class="text-gray-400 mx-2"></span>
<span
class="font-bold {{ $msg->towho ? 'text-indigo-700' : 'text-gray-500' }}">{{ $msg->towho ?: '大家' }}</span>
<span class="text-gray-400 mx-2">留言:</span>
</div>
<div class="text-xs text-gray-400 flex items-center space-x-3">
<span>{{ \Carbon\Carbon::parse($msg->post_time)->diffForHumans() }}</span>
@if ($isFromMe || $isToMe || (Auth::check() && Auth::user()->user_level >= 15))
<form action="{{ route('guestbook.destroy', $msg->id) }}" method="POST"
onsubmit="return confirm('确定要抹除这条留言吗?');" class="inline">
@csrf
@method('DELETE')
<button type="submit"
class="text-red-400 hover:text-red-600 transition opacity-0 group-hover:opacity-100 bg-red-50 px-2 py-1 rounded">
删除
</button>
</form>
@endif
</div>
{{-- 写信/留言表单区 (Alpine 控制显隐) --}}
<div x-show="showWriteForm" x-collapse class="bg-white border-b border-gray-200 shadow-inner">
<div class="max-w-4xl mx-auto p-6">
<form action="{{ route('guestbook.store') }}" method="POST" class="space-y-4">
@csrf
<div class="flex items-center space-x-4">
<div class="flex-1">
<label class="block text-sm font-medium text-gray-700 mb-1">接收人 (留空或填"大家"表示公共留言)</label>
<input type="text" name="towho" x-model="towho" placeholder="系统自动处理"
class="w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
</div>
<div
class="text-gray-800 leading-relaxed text-sm whitespace-pre-wrap {{ $isSecret ? 'bg-pink-50 p-3 rounded-lg border border-pink-100' : 'bg-gray-50 p-3 rounded-lg border border-gray-100' }}">
{!! nl2br(e($msg->text_body)) !!}
<div class="flex items-center h-full pt-6">
<label
class="flex items-center space-x-2 text-sm text-gray-700 cursor-pointer bg-pink-50 px-3 py-2 rounded-md hover:bg-pink-100 transition border border-pink-100">
<input type="checkbox" name="secret" value="1"
class="rounded text-pink-500 focus:ring-pink-500 w-4 h-4">
<span class="font-bold text-pink-700">🔒 悄悄话 (仅双方可见)</span>
</label>
</div>
@if (!Auth::check() || $msg->who !== Auth::user()->username)
<div class="mt-3 flex justify-end">
<button
@click="showWriteForm = true; towho = '{{ $msg->who }}'; setTimeout(() => $refs.textBody.focus(), 100); window.scrollTo({top:0, behavior:'smooth'})"
class="text-xs text-indigo-500 hover:text-indigo-700 font-medium flex items-center transition">
<svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"></path>
</svg>
回复TA
</button>
</div>
@endif
</div>
@empty
<div class="bg-white rounded-xl shadow-sm border border-gray-200 p-12 text-center">
<span class="text-4xl">📭</span>
<h3 class="mt-4 text-lg font-medium text-gray-900">暂无信件</h3>
<p class="mt-2 text-sm text-gray-500">这里是空空如也的荒原。</p>
<button @click="showWriteForm = true; towho = ''; setTimeout(() => $refs.textBody.focus(), 100)"
class="mt-4 text-indigo-600 font-bold hover:underline">
来抢沙发留言吧!
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">正文内容 <span
class="text-red-500">*</span></label>
<textarea name="text_body" x-ref="textBody" rows="3" required
class="w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="相逢何必曾相识,留下您的足迹吧..."></textarea>
</div>
<div class="flex justify-end">
<button type="submit"
class="bg-indigo-600 hover:bg-indigo-700 text-white py-2 px-6 rounded-md shadow flex items-center font-bold">
<svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path>
</svg>
发送
</button>
</div>
@endforelse
{{-- 分页 --}}
<div class="mt-6">
{{ $messages->links() }}
</div>
</form>
</div>
</main>
</div>
</div>
{{-- 移动端悬浮写留言按钮 --}}
<button @click="showWriteForm = !showWriteForm; if(showWriteForm) setTimeout(() => $refs.textBody.focus(), 100)"
class="md:hidden fixed bottom-20 right-4 w-14 h-14 bg-indigo-600 text-white rounded-full shadow-lg flex justify-center items-center hover:bg-indigo-700 transition z-50">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" x-show="!showWriteForm">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z">
</path>
</svg>
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" x-show="showWriteForm"
style="display: none;">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
{{-- 主体内容区 --}}
<div class="flex max-w-7xl mx-auto mt-4 px-4 sm:px-6 lg:px-8">
{{-- 移动端底部分类栏 --}}
<div class="md:hidden bg-white border-t border-gray-200 flex justify-around p-2 shrink-0 relative z-20">
<a href="{{ route('guestbook.index', ['tab' => 'public']) }}"
class="flex flex-col items-center {{ $tab === 'public' ? 'text-indigo-600' : 'text-gray-500' }}">
<span class="text-xl">🌍</span>
<span class="text-xs mt-1">公共墙</span>
</a>
<a href="{{ route('guestbook.index', ['tab' => 'inbox']) }}"
class="flex flex-col items-center {{ $tab === 'inbox' ? 'text-indigo-600' : 'text-gray-500' }}">
<span class="text-xl">📥</span>
<span class="text-xs mt-1">收件箱</span>
</a>
<a href="{{ route('guestbook.index', ['tab' => 'outbox']) }}"
class="flex flex-col items-center {{ $tab === 'outbox' ? 'text-indigo-600' : 'text-gray-500' }}">
<span class="text-xl">📤</span>
<span class="text-xs mt-1">发件箱</span>
</a>
{{-- 左侧:分类导航 --}}
<div
class="w-64 bg-white border border-gray-200 rounded-lg shrink-0 hidden md:flex flex-col mb-10 self-start sticky top-20">
<div class="p-6 flex-1">
{{-- 新建留言按钮 --}}
<button
@click="showWriteForm = !showWriteForm; if(showWriteForm) setTimeout(() => $refs.textBody.focus(), 100)"
class="w-full bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-3 rounded-xl font-bold mb-6 shadow-md transition flex justify-center items-center">
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z">
</path>
</svg>
<span x-text="showWriteForm ? '取消撰写' : '写新留言'"></span>
</button>
<nav class="space-y-2">
<a href="{{ route('guestbook.index', ['tab' => 'public']) }}"
class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'public' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
<span class="mr-3 text-lg">🌍</span> 公共留言墙
</a>
<a href="{{ route('guestbook.index', ['tab' => 'inbox']) }}"
class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'inbox' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
<span class="mr-3 text-lg">📥</span> 我收件的
</a>
<a href="{{ route('guestbook.index', ['tab' => 'outbox']) }}"
class="flex items-center px-4 py-3 rounded-lg font-medium transition-colors {{ $tab === 'outbox' ? 'bg-indigo-50 text-indigo-700' : 'text-gray-600 hover:bg-gray-50' }}">
<span class="mr-3 text-lg">📤</span> 我发出的
</a>
</nav>
</div>
</div>
{{-- 右侧:留言流列表 --}}
<main class="flex-1 p-4 sm:p-0 md:pl-6 pb-20">
<div class="max-w-4xl mx-auto space-y-4">
@forelse($messages as $msg)
@php
$isSecret = $msg->secret == 1;
$isToMe = Auth::check() && $msg->towho === Auth::user()->username;
$isFromMe = Auth::check() && $msg->who === Auth::user()->username;
@endphp
<div
class="bg-white rounded-xl shadow-sm border {{ $isSecret ? 'border-pink-200' : 'border-gray-200' }} p-5 relative group overflow-hidden">
@if ($isSecret)
<div
class="absolute top-0 right-0 bg-pink-100 text-pink-700 text-[10px] font-bold px-2 py-1 rounded-bl-lg">
🔒 私密信件
</div>
@endif
<div class="flex justify-between items-start mb-2">
<div class="flex items-center text-sm">
<span class="font-bold text-indigo-700">{{ $msg->who }}</span>
<span class="text-gray-400 mx-2"></span>
<span
class="font-bold {{ $msg->towho ? 'text-indigo-700' : 'text-gray-500' }}">{{ $msg->towho ?: '大家' }}</span>
<span class="text-gray-400 mx-2">留言:</span>
</div>
<div class="text-xs text-gray-400 flex items-center space-x-3">
<span>{{ \Carbon\Carbon::parse($msg->post_time)->diffForHumans() }}</span>
@if ($isFromMe || $isToMe || (Auth::check() && Auth::user()->user_level >= 15))
<form action="{{ route('guestbook.destroy', $msg->id) }}" method="POST"
onsubmit="return confirm('确定要抹除这条留言吗?');" class="inline">
@csrf
@method('DELETE')
<button type="submit"
class="text-red-400 hover:text-red-600 transition opacity-0 group-hover:opacity-100 bg-red-50 px-2 py-1 rounded">
删除
</button>
</form>
@endif
</div>
</div>
<div
class="text-gray-800 leading-relaxed text-sm whitespace-pre-wrap {{ $isSecret ? 'bg-pink-50 p-3 rounded-lg border border-pink-100' : 'bg-gray-50 p-3 rounded-lg border border-gray-100' }}">
{!! nl2br(e($msg->text_body)) !!}
</div>
@if (!Auth::check() || $msg->who !== Auth::user()->username)
<div class="mt-3 flex justify-end">
<button
@click="showWriteForm = true; towho = '{{ $msg->who }}'; setTimeout(() => $refs.textBody.focus(), 100); window.scrollTo({top:0, behavior:'smooth'})"
class="text-xs text-indigo-500 hover:text-indigo-700 font-medium flex items-center transition">
<svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"></path>
</svg>
回复TA
</button>
</div>
@endif
</div>
@empty
<div class="bg-white rounded-xl shadow-sm border border-gray-200 p-12 text-center">
<span class="text-4xl">📭</span>
<h3 class="mt-4 text-lg font-medium text-gray-900">暂无信件</h3>
<p class="mt-2 text-sm text-gray-500">这里是空空如也的荒原。</p>
<button
@click="showWriteForm = true; towho = ''; setTimeout(() => $refs.textBody.focus(), 100)"
class="mt-4 text-indigo-600 font-bold hover:underline">
来抢沙发留言吧!
</button>
</div>
@endforelse
{{-- 分页 --}}
<div class="mt-6">
{{ $messages->links() }}
</div>
</div>
</main>
</div>
{{-- 移动端悬浮写留言按钮 --}}
<button @click="showWriteForm = !showWriteForm; if(showWriteForm) setTimeout(() => $refs.textBody.focus(), 100)"
class="md:hidden fixed bottom-20 right-4 w-14 h-14 bg-indigo-600 text-white rounded-full shadow-lg flex justify-center items-center hover:bg-indigo-700 transition z-50">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" x-show="!showWriteForm">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z">
</path>
</svg>
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" x-show="showWriteForm"
style="display: none;">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
{{-- 移动端底部分类栏 --}}
<div class="md:hidden bg-white border-t border-gray-200 flex justify-around p-2 shrink-0 relative z-20">
<a href="{{ route('guestbook.index', ['tab' => 'public']) }}"
class="flex flex-col items-center {{ $tab === 'public' ? 'text-indigo-600' : 'text-gray-500' }}">
<span class="text-xl">🌍</span>
<span class="text-xs mt-1">公共墙</span>
</a>
<a href="{{ route('guestbook.index', ['tab' => 'inbox']) }}"
class="flex flex-col items-center {{ $tab === 'inbox' ? 'text-indigo-600' : 'text-gray-500' }}">
<span class="text-xl">📥</span>
<span class="text-xs mt-1">收件箱</span>
</a>
<a href="{{ route('guestbook.index', ['tab' => 'outbox']) }}"
class="flex flex-col items-center {{ $tab === 'outbox' ? 'text-indigo-600' : 'text-gray-500' }}">
<span class="text-xl">📤</span>
<span class="text-xs mt-1">发件箱</span>
</a>
</div>
</div>
@endsection