How to customize the style in react-datepicker?

Lusha Li picture Lusha Li · Dec 31, 2018 · Viewed 10.9k times · Source

I want to change the style of react-datepicker:

1.change the input box to a customized style

2.change the style and the language of the calendar

I saw a post (Custom Input Field on datepicker, [react-datepicker components] https://github.com/Hacker0x01/react-datepicker/blob/master/docs/datepicker.md) talking about replacing the whole component. Can I edit the style directly without writing a new component? If yes, how can I achieve this? Moreover, I have no clue how to change the calendar. How can I find out which component should I change?

import React, {Component} from 'react';
import {render} from 'react-dom';

import moment from 'moment';

import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';

const Input = ({onChange, placeholder, value, isSecure, id, onClick}) => (
    <input
        style={{backgroundColor:"black"}}
        onChange={onChange}
        placeholder={placeholder}
        value={value}
        isSecure={isSecure}
        id={id}
        onClick={onClick}
    />
);

const NoClickInput = ({onClick, ...props}) => <Input {...props} />;

class App extends Component {
    state = {
        startDate: moment(),
        endDate: moment()
    };

    render() {
        const {startDate, endDate} = this.state;
        return (
            <div>
                <div>Start:</div>
                <DatePicker
                    selected={startDate}
                    selectsStart
                    startDate={startDate}
                    endDate={endDate}
                    onChange={date=>this.setState({startDate})}
                />
                <div>End:</div>
                <DatePicker
                    selected={endDate}
                    selectsEnd
                    startDate={startDate}
                    endDate={endDate}
                    onChange={date => this.setState({ endDate })}
                />
            </div>
        );
    }
}

render(<App />, document.getElementById('root'));

Edit 8nm3x4y300

Answer

Mohammad Ansari picture Mohammad Ansari · Dec 31, 2018

Hi You need to create your custom css stylesheet and import it in the react

Also try using !important in css codes for applying your custom stylesheet over default styles