如何在 Qwik 模板中有条件地添加类?

huangapple go评论68阅读模式
英文:

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.

&lt;div&gt;
    &lt;label for=&quot;email&quot; class=&quot;block mb-2 text-sm font-medium text-gray-900 dark:text-white&quot;&gt;Name&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot;
        class=&quot;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&quot; placeholder=&quot;Write your name&quot; /&gt;
    {
        action.value?.failed &amp;&amp; &lt;p class=&quot;mt-2 text-sm text-red-600 dark:text-red-500&quot;&gt;{action.value.fieldErrors?.name}&lt;/p&gt;
    }
&lt;/div&gt;

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. 如何在 Qwik 模板中有条件地添加类?

&lt;div&gt;
&lt;label for=&quot;email&quot; class=&quot;block mb-2 text-sm font-medium text-gray-900 dark:text-white&quot;&gt;Name&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot;
    class={[&#39;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&#39;, (action.value?.failed &amp;&amp; action.value.fieldErrors?.name) ? &#39;border-red-300&#39; : &#39;border-gray-300&#39;]} placeholder=&quot;Write your name&quot; /&gt;
{
    action.value?.failed &amp;&amp; &lt;p class=&quot;mt-2 text-sm text-red-600 dark:text-red-500&quot;&gt;{action.value.fieldErrors?.name}&lt;/p&gt;
}
&lt;/div&gt;

答案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

&lt;button class={`your_classes ${isError.value ? &#39;border-red-300&#39; : &#39;&#39;}`}&gt;
  Click me
&lt;/button&gt;

or in this way

&lt;button class={{ &#39;your_classes&#39; : true, &#39;border-red-300&#39;: isError.value }}&gt;
  Click me
&lt;/button&gt;

or

&lt;button
  class={[
	&#39;your_classes&#39;,
	isError.value ? &#39;border-red-300&#39; : &#39;&#39;,
    // you can use this alternative syntax
	{ &#39;border-red-300&#39;: isError.value },
  ]}
&gt;
  Click me
&lt;/button&gt;

huangapple
  • 本文由 发表于 2023年6月12日 03:07:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452101.html
  • javascript
  • jsx
  • qwik
  • qwikjs

如何使用` :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定