自定义rc-time-picker的样式

时间:2019-03-15 19:42:01

标签: javascript css reactjs frontend timepicker

我在项目中使用rc-time-picker包,但是在自定义pop-up组件的time picker样式时遇到问题。这是我的组件的屏幕截图:

screenshot of component

首先,我需要将时间item中时间li的背景颜色从light grey(在屏幕截图中)更改为#edeffe,选择。以下是我的代码:

import React from "react";
import TimePicker from "rc-time-picker";
import "rc-time-picker/assets/index.css";
import styled from 'styled-components';

const StyledTimePicker = styled(TimePicker)`
 &.rc-time-picker-panel-select-option-selected{
    background-color: #edeffe !important;
  }
`;

const DeliTimePicker = ({ value, onChange, ...others }) => {
  return (
    <StyledTimePicker
      showSecond={false}
      onChange={onChange}
      hideDisabledOptions
      minuteStep={5}
      {...others}
      value={value}
      use12Hours
    />
  );
};

export default DeliTimePicker;

从浏览器的检查中,我发现每个项目的className被选中为rc-time-picker-panel-select-option-selected。我还必须使用styled component包在项目中进行样式设置。我不知道为什么它不能通过这种方法。最终组件应如下所示:

final rendering

这是codeandbox链接:https://codesandbox.io/s/kk8lllwwp7?fontsize=14

任何答案将不胜感激!

1 个答案:

答案 0 :(得分:0)

您需要重新排列样式TimePicker的顺序。 styled-components包会生成一个className,需要将其应用于TimePicker。在这种情况下,它将同时应用于其className和其popupClassName

工作示例

Edit Styled Time Picker

components / TimePicker / index.js

import styled from "styled-components";
import TimePicker from "./TimePicker";

const StyledTimePicker = styled(TimePicker)`
  & .rc-time-picker-panel-select-option-selected {
    background-color: #edeffe;
    font-weight: normal;
  }

  & .rc-time-picker-clear,
  & .rc-time-picker-clear-icon:after {
    font-size: 15px;
  }

  & .rc-time-picker-panel-select,
  & .rc-time-picker-input,
  & .rc-time-picker-panel-input {
    font-family: "Consolas", sans-serif;
    font-size: 16px;

    ::-webkit-scrollbar {
      width: 0;
      height: 0;
    }
  }
`;

export default StyledTimePicker;

components / TimePicker / TimePicker.js

import React from "react";
import PropTypes from "prop-types";
import moment from "moment";
import TimePicker from "rc-time-picker";
import "rc-time-picker/assets/index.css";

const DeliTimePicker = ({ className, onChange, value, ...rest }) => (
  <TimePicker
    {...rest}
    className={className}
    popupClassName={className}
    showSecond={false}
    onChange={onChange}
    hideDisabledOptions
    minuteStep={5}
    value={value}
    use12Hours
  />
);

DeliTimePicker.propTypes = {
  className: PropTypes.string.isRequired,
  onChange: PropTypes.func.isRequired,
  value: PropTypes.instanceOf(moment)
};

export default DeliTimePicker;

components / TimeSelectForm / index.js

import React, { Component } from "react";
import moment from "moment";
import TimePicker from "../TimePicker";

class TimeSelectForm extends Component {
  state = {
    value: moment()
  };

  handleChange = value => this.setState({ value });

  handleSubmit = e => {
    e.preventDefault();
    alert(moment(this.state.value).format("hh:mm a"));
  };

  render = () => (
    <form onSubmit={this.handleSubmit}>
      <TimePicker value={this.state.value} onChange={this.handleChange} />
      <br />
      <button type="submit">Submit</button>
    </form>
  );
}

export default TimeSelectForm;