【问题标题】:How to change the style of a Lit Element when a certain event is triggered?如何在触发特定事件时更改 Lit Element 的样式?
【发布时间】:2023-01-28 09:24:50
【问题描述】:

(Lit 元素环境的新手,对于天真的问题深表歉意)

代码摘要

下面的代码是一个使用 Lit Element 制作的文件上传器 Web 组件。 选择文件后,_inputHandler 函数会验证文件,如果有效,则启用/触发上传到服务器端的功能。

问题

如何将#cancel-button css 样式设置为可见性:选择输入文件时可见?

(默认/加载页面时)只要没有选择文件,取消按钮就会保持隐藏状态。

选择有效文件后,应出现“取消”按钮(可见性:可见或其他)

Uploader.ts 的完整代码:


import { html, LitElement, css } from "lit";
import { customElement, property, query } from "lit/decorators.js";

import { styles } from "./Style.Uploader.js";

@customElement("file-uploader")
export class Uploader extends LitElement {
  // CSS Styling
  static get styles() {
    return styles;
  }

  // Properties
  @property()
  fileName = `Accepted File Types : ${getValidFileTypes().toString()}`;

  @property() fileType = "";

  @property() fileSizeInKB: Number = 0;

  private files: FileList | null = null;

  // Actual html elements Rendered

  render() {
    return html`
      <section>
        <form action="" autocomplete="off" enctype="multipart/form-data">
          <h3>Upload your File</h3>
          <div class="box" id="box">
            <input
              type="file"
              name="uploadedFile"
              id="input-box"
              class="input-box"
              style="display: none"
              @change="${this._inputHandler}"
            />
            <label for="input-box" class="input-label" id="input-label">
              ${this.fileName}
            </label>
          </div>

          <div class="buttons">
            <button
              class="upload-btn"
              id="upload-btn"
              @click="${this._uploadHandler}"
            >
              <span class="upload-btn-span" id="upload-btn-span">
                &#8682; Upload
              </span>
              <span class="uploading-span" id="uploading-span">
                Uploading...
              </span>
            </button>

            <button
              class="cancel-btn"
              id="cancel-btn"
              @click="${this._cancelHandler}"
            >
              Cancel
            </button>
          </div>

        </form>
      </section>
    `;
  }

  @query("upload-btn") uploadButton!: HTMLButtonElement;

  get cancelButton() {
    return this.querySelector("#cancel-btn"); // ?? null
  }

  // @query("cancel-btn") cancelButton!: HTMLButtonElement;

  private async _inputHandler(e: Event) {
    const input = e.target as HTMLInputElement;

    // array of files selected to upload
    this.files = input.files;

    if (!(this.files && this.files[0])) {
      return;
    } else {
      const uploadedFileSize = this.files[0]?.size;
      const uploadedFileName = this.files[0]?.name;

      const uploadedFileType =
        FileExtensionService.getFileExtension(uploadedFileName);


      // Trying to Change the visibility of the Cancel button to visible
      this.cancelButton!.style.visibility = "visible";

      // Validating file size and file type
      const validFileSize = await validateFileSize(uploadedFileSize);
      const validFileType = await validateFileType(uploadedFileType);
      if (!validFileSize.isValid) {
          ...
      }
    }
  }

  /**
   *
   * @returns
   */
  private _cancelHandler(e: Event) {
    e.preventDefault();
    this.files = null;
    this.fileName = "";
    this.fileType = "";
    this.fileSizeInKB = 0;
    this.requestUpdate();
  }
}

我正在尝试更改按钮的样式(我想我可以使用 getter 或通过 @query 获取)但出现错误: Property 'style' does not exist on type 'Element'.ts(2339) 为: this.cancelButton!.**style**.visibility = "visible";

【问题讨论】:

    标签: typescript web-component shadow-dom lit-element lit


    【解决方案1】:

    您可以使用 styleMap() 内置指令根据其他一些属性/状态在取消按钮上进行动态样式设置。

    例如

    render() {
      return html`
        ...
                <button
                  class="cancel-btn"
                  id="cancel-btn"
                  style=${styleMap({ visibility: this.files?.length ? 'visible' : 'hidden' })}
                  @click="${this._cancelHandler}"
                >
        ...
      `;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-12
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多