0

我有一个简单的 React js 项目。

应用程序.js

import React from 'react';
import Radium, { StyleRoot } from 'radium';
import Checkbox from './Checkbox';

function App() {
  // style
  const style = {
    ':hover': {
      color: 'red',
      backgroundColor: 'blue'
    },
    ':focus': {
      color: 'orange'
    }
  };

  return (
    <div className="App">
      <p>Hovering on the button should change its color to red and background color to blue.</p>
      <p>Pressing it makes the text orange.</p>
      <p>Pesudo-element like :hover is not possible unless we use radium.</p>
      <button style={style}>Hover me</button>
      <Checkbox />
    </div>
  );
}

export default Radium(App);

复选框.js

import React from 'react';
import Radium from 'radium';

const checkBox = props => {
    const style = {
        '+ label': {
          color: '#ccc'
        }, 
        ':checked + label': {
          color: '#f00'
        } 
    };

    return (
        <div>
            <input type="checkbox" id="ossm" name="ossm" style={style} /> 
            <label for="ossm">CSS is Awesome</label> 
        </div>
    );
};

export default Radium(checkBox);

首先,我试图设置一个复选框的样式,以便:

  • 它的标签颜色为#ccc,并且
  • 选中后,将其标签的颜色更改为#f00。

不应用样式。如何解决?

其次,在 App.js 内部,我在什么情况下用 包装 JSX 代码<StyleRoot></StyleRoot>

非常感谢!

4

2 回答 2

0

保持状态并有条件地应用样式。

工作演示

像这样

const checkBox = props => {
  const [checked, setChecked] = useState(false);

  const style = {
    input: { fontSize: 20, padding: "20px" },
    base: { background: checked ? "red" : "blue", fontSize: 20 },
    "+ label": {
      color: "#ccc"
    },
    ":checked + label": {
      color: "#f00"
    }
  };

  return (
    <div>
      <input
        checked={checked}
        onChange={() => setChecked(prev => !prev)}
        type="checkbox"
        id="ossm"
        name="ossm"
        style={style.input}
      />
      <label style={style.base} for="ossm">
        CSS is Awesome
      </label>
    </div>
  );
};

const Checkbox = Radium(checkBox);
于 2020-07-07T05:26:05.313 回答
0

镭不支持:checked

https://github.com/FormidableLabs/radium/issues/210

于 2020-07-07T06:15:09.030 回答