简体   繁体   English

在 React 中使用 svgr 时,如何自定义复选框的选中 state?

[英]How can I customize the checked state of a checkbox when using svgr in React?

I'm working on a React project and I'm using styled-component and typescript .我正在开发一个React项目,我正在使用styled-componenttypescript

I'm customizing the checkbox like this:我正在像这样自定义复选框:

Source资源

import React, { ReactElement } from 'react';
import styled from 'styled-components';
import IconChecked from '@assets/Icons/ico_checked.svg';

interface Props {
  id: string;
}

const StyledCheckBox = styled.input`
  box-sizing: border-box;
  width: 20px;
  height: 20px;
  background: #ffffff;
  border: 1px solid #d4dae4;
  border-radius: 4px;
  appearance: none;
  &:checked {
    background-image: url(${IconChecked});
    background-repeat: no-repeat;
    background-color: #ffffff;
    background-position: 50%;
  }
`;

const CheckBox = ({ id }: Props): ReactElement => (
  <StyledCheckBox type="checkbox" id={id} />
);

export default CheckBox;

Question问题

To use the svg icon without the <img> tag, I installed the svgr package and set it as an svg type loader through webpack . To use the svg icon without the <img> tag, I installed the svgr package and set it as an svg type loader through webpack . The problem is that svgr has the logic to convert svg to component, so I can't set the checked icon in the following way.问题是svgr具有将 svg 转换为组件的逻辑,所以我无法通过以下方式设置选中的图标。

 &:checked {
    background-image: url(${IconChecked});
    ...
 }

As a result, the check icon( IconChecked ) does not appear when I click the checkbox after using svgr .结果,使用svgr后单击复选框时,不会出现检查图标( IconChecked )。 How can I solve this?我该如何解决这个问题?

You can move your ico_checked.svg file to public folder and use the svg directly from the url like this:您可以将您的ico_checked.svg文件移动到public并直接从 url 使用 svg,如下所示:

 &:checked {
    background-image: url("/ico_checked.svg");
    ...
 }

You can take a look at this sandbox for a live working example of this approach.您可以查看此沙箱,了解此方法的实时工作示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TestCafe测试脚本checkbox.checked即使被选中也总是返回false,如何在if条件下检查checkbox状态? - TestCafe test script checkbox.checked always return false even when checked, how can I check the checkbox state in an if-condition? 选中/取消选中复选框时管理状态-React - Manage State when checkbox is checked/unchecked - React 选中另一个复选框后,如何检查该复选框? [JavaScript的] - How can I check a checkbox when another checkbox is checked? [JavaScript] 选中主复选框时,如何将复选框设置为在数据表列中选中 - How can I set checkbox to checked in a datatable column when a master checkbox is checked 如果选中任何复选框并在未选中任何复选框时禁用它,如何启用提交按钮? - How can I enable a submit button if any checkbox is checked and disable it when none checkbox is checked? 使用JQuery选中复选框时,如何隐藏然后显示多个Div - How can I hide then show multiple Divs when checkbox is checked using JQuery 选中初始复选框时(如何使用PHP / Javascript)如何显示多个复选框 - How can I have multiple checkboxes appear when initial checkbox is checked (using PHP/Javascript) 在ASP.NET中使用JavaScript进行检查时,如何传递复选框的值和对应名称? - How can I pass the value and corresponding name of a checkbox when checked using javascript in ASP.NET? 如何在选中Checkbox时调用javascript函数 - How can I call a javascript function when Checkbox is checked 反应:以编程方式更改选中的 state 时在复选框输入上触发 onChange? - React: trigger onChange on checkbox input when changing checked state programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM