定义一个使用StencilJS的div的宽度和背景颜色。

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

Define width and background-color of a div with StencilJS

问题

<div class="SomeClass" style="width: array[0].value; background-color: array[0].color">
render() {
  return (
    this.objArray.forEach(element => {
      if (element.color == undefined || element.color == null || element.color == '') {
        element.color = this.colors[this.index];
        this.index = this.index == (this.colors.length - 1) ? 0 : (this.index += 1);
      }

      if (element.value == null || element.value == 0) {
        element.value = 100;
      }

      if (element.tooltip == null) {
        element.tooltip = '';
      }

      return `<div class style="width:${element.value}; background-color:${element.color}"></div>`;
    });
  )
}

I have removed the unnecessary HTML encoding (&lt; and &gt;) from the code snippets.

英文:

I have an array with some properties (values are variable).

[{&quot;color&quot;: &quot;#2c8646&quot;,
  &quot;value&quot;: 30
 },
 {&quot;color&quot;: &quot;#69c0f9&quot;,
  &quot;value&quot;: 50 }]

And then, I need to return divs with that, something like:

render(){
  return (
 &lt;div class=&quot;SomeClass&quot; style=&quot;width: array[0].value ; background-color: array[0].color&quot; &gt;
)

The matter is that style in StencilJS doesn't work like that. I can't use a static css file due that the Array is different any time.

Any idea?

TIA

I need to do something like

this.objArray.forEach(element =&gt; {

      if(element.color == undefined || element.color == null || element.color == &#39;&#39;)
      {
        element.color = this.colors[this.index];
        this.index = this.index == (this.colors.length - 1) ? 0 : (this.index += 1 );
      }

      if(element.value == null  || element.value == 0  )
        element.value = 100;

      if(element.tooltip == null )
        element.tooltip = &#39;&#39;;

     return &lt;div class style={`width:${element.value}; background-color ${element.color}`&gt; &lt;/div&gt;;

    });

答案1

得分: 1

Stencil 允许样式具有动态性 - 我们可以从一个简单的示例开始,看起来像这样:

import { Component, h } from '@stencil/core';

@Component({
  tag: 'my-component',
  shadow: true,
})
export class MyComponent {
  render() {
    return <div style={{ background: "#ffe7e8", border: "2px solid #e66465" }}>Hello, World! I'm Stencil</div>;
  }
}

在这里,style 属性的内容包含两对花括号 - 一对用于表示我们有一个对象字面量:

{ background: "#ffe7e8", border: "2px solid #e66465" }

以及包围该对象字面量的外部一对,告诉 Stencil 插值/读取这个值。

然后,我们可以添加一个样式数组,并使用 map() 函数来为每个样式显示一个 div

import { Component, h } from '@stencil/core';

type Styles = Record<string, string>[];

const myStyles: Styles = [
  { background: "#ffe7e8", border: "2px solid #e66465" },
  { background: "#26b72b", border: "1px dashed blue" }
]

@Component({
  tag: 'my-component',
  shadow: true,
})
export class MyComponent {
  render() {
    return (<div>{myStyles.map((myStyle) => <div style={myStyle}>Hello, World! I'm Stencil</div>)}</div>);
  }
}

请注意:

  1. Styles 只是一个包含字符串到字符串映射的数组。
  2. myStyles 包含两种不同的样式供我们迭代。
  3. 我们在 render 函数中使用 map() - 这是必要的,以便为 myStyles 中的每个条目返回一个新的 div
  4. 我们只使用一对花括号,因为 render 函数中的 myStyle 已经是一个对象。

使用 map 是重要的,因为 forEach 不会为每次迭代返回一个新值。如果我将我的 render 函数更改为:

  render() {
    return (<div>{myStyles.forEach((myStyle) => <div style={myStyle}>Hello, World! I'm Stencil</div>)}</div>);
  }

则什么都不会渲染。

要使您的 forEach 可行,我建议进行以下更改:

  1. forEach 更改为 map
  2. 更新样式生成逻辑,以构建一个对象字面量,而不是一个字符串,像这样:
const newStyle = {
  width: element.value,
  backgroundColor: element.color
};
return <div style={newStyle}> </div>;
英文:

Stencil allows for styles to be dynamic - a simple example that we can build on would look somethings like this:

import { Component, h } from &#39;@stencil/core&#39;;

@Component({
  tag: &#39;my-component&#39;,
  shadow: true,
})
export class MyComponent {
  render() {
    return &lt;div style={{ background: &quot;#ffe7e8&quot;, border: &quot;2px solid #e66465&quot; }}&gt;Hello, World! I&#39;m Stencil&lt;/div&gt;;
  }
}

where we just output "Hello World, I'm Stencil!" in a red bordered and pink-ish background.

Note that here, the contents of the style attribute has two curly braces - one to denote that we have an object literal:

{ background: &quot;#ffe7e8&quot;, border: &quot;2px solid #e66465&quot; }

and an outer pair surrounding that object literal to tell Stencil to interpolate/read this value.

We can then add an array of styles, and use the map() function in order to show a div for each style:

import { Component, h } from &#39;@stencil/core&#39;;

type Styles = Record&lt;string, string&gt;[];

const myStyles: Styles = [
  { background: &quot;#ffe7e8&quot;, border: &quot;2px solid #e66465&quot; },
  { background: &quot;#26b72b&quot;, border: &quot;1px dashed blue&quot; }
]

@Component({
  tag: &#39;my-component&#39;,
  shadow: true,
})
export class MyComponent {
  render() {
    return (&lt;div&gt;{myStyles.map((myStyle) =&gt; &lt;div style={myStyle}&gt;Hello, World! I&#39;m Stencil&lt;/div&gt;)}&lt;/div&gt;);
  }
}

Note that:

  1. Styles is just an array containing a mapping of strings to strings
  2. myStyles contains two different styles for us to iterate over
  3. We use map() in the render function - this is necessary in order to have a new div returned for each and every entry in myStyles.
  4. We use only one set of curly braces, as myStyle in the render function is already an object

Using map is important, as forEach does not return a new value for each iteration. If I were to change my render function to:

  render() {
    return (&lt;div&gt;{myStyles.forEach((myStyle) =&gt; &lt;div style={myStyle}&gt;Hello, World! I&#39;m Stencil&lt;/div&gt;)}&lt;/div&gt;);
  }

Nothing would render.

To make your forEach workable, I'd suggest changing:

  1. forEach to a map
  2. updating the style generation logic to build an object literal instead of a string like so:
const newStyle = {
  width: element.value,
  background-color: element.color
};
return &lt;div class style={newStyle}&gt; &lt;/div&gt;;

huangapple
  • 本文由 发表于 2023年6月22日 06:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527628.html
匿名

发表评论

匿名网友

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

确定