英文:
How to add classes conditionally in Qwik template?
问题
Sure, here's the translated code snippet:
我有一个表单的 `input` 字段,当在HTML表单中出现验证错误时,我想要添加一些类来使 `input` 框的边框变成红色。我该如何做?
目前,我正在使用以下代码来显示验证错误消息,这个部分是正常工作的。但是,除此之外,我还想将 `input` 字段的边框设置为红色,以突出显示错误。
<div>
<label for="name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
<input type="text" name="name" id="name"
class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Write your name" />
{
action.value?.failed && <p class="mt-2 text-sm text-red-600 dark:text-red-500">{action.value.fieldErrors?.name}</p>
}
</div>
默认情况下,模板中必须包含 `border-gray-300` 类。但是,每当出现验证错误时,我想通过 `border-red-300` 来更新它。
**---更新的工作代码---**
更新后的示例代码如下。我不知道它有多么整洁和优化,但现在它正常工作了。感谢 "Giorgio Boa"。任务完成。:D
<div>
<label for="name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
<input type="text" name="name" id="name"
class={['bg-gray-50 border text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500', (action.value?.failed && action.value.fieldErrors?.name) ? 'border-red-300' : 'border-gray-300']} placeholder="Write your name" />
{
action.value?.failed && <p class="mt-2 text-sm text-red-600 dark:text-red-500">{action.value.fieldErrors?.name}</p>
}
</div>
英文:
I've one form input
field where I want to add some classes to make the border of the input
box as red color when I'll get some validation error in the html form. How can I do this?
Currently, I'm using the following code to show the validation error message which is working fine. But, with this I also want to make the border of input
filed as red color to highlight that.
<div>
<label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
<input type="text" name="name" id="name"
class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Write your name" />
{
action.value?.failed && <p class="mt-2 text-sm text-red-600 dark:text-red-500">{action.value.fieldErrors?.name}</p>
}
</div>
It'll be like by default, that border-gray-300
class must be in the template. But, whenever I'll get a validation error I want to update that by border-red-300
---Updated Working Code---
The updated sample code is like this. I don't know how much it's neat & optimize. But it's working now. Thanks to "Giorgio Boa". Job done.
<div>
<label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
<input type="text" name="name" id="name"
class={['bg-gray-50 border text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500', (action.value?.failed && action.value.fieldErrors?.name) ? 'border-red-300' : 'border-gray-300']} placeholder="Write your name" />
{
action.value?.failed && <p class="mt-2 text-sm text-red-600 dark:text-red-500">{action.value.fieldErrors?.name}</p>
}
</div>
答案1
得分: 2
这是文档。
例如,当isError为true时,将border-red-300类应用于按钮,并在其为false时删除。
您可以使用以下语法来使用条件类:
<button class={`your_classes ${isError.value ? 'border-red-300' : ''}`}>
点我
</button>
或者这种方式:
<button class={{ 'your_classes': true, 'border-red-300': isError.value }}>
点我
</button>
或者:
<button
class={[
'your_classes',
isError.value ? 'border-red-300' : '',
// 您可以使用这种替代语法
{ 'border-red-300': isError.value },
]}
>
点我
</button>
注意:请在代码部分中保留原始的HTML标签和类名。
英文:
Here is the documentation
e.g. This will apply the border-red-300 class to the button when isError is true, and remove it when it's false.
You can use conditional classes with this syntax
<button class={`your_classes ${isError.value ? 'border-red-300' : ''}`}>
Click me
</button>
or in this way
<button class={{ 'your_classes' : true, 'border-red-300': isError.value }}>
Click me
</button>
or
<button
class={[
'your_classes',
isError.value ? 'border-red-300' : '',
// you can use this alternative syntax
{ 'border-red-300': isError.value },
]}
>
Click me
</button>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论