Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I style react-datepicker?

I'm using webpack, react-datepicker and have managed to import its css with the provided css module.

import 'react-datepicker/dist/react-datepicker-cssmodules.css

The component looks fine and dandy, but now I want to make it full width like the time element above it.

enter image description here

Looking at the CSS, what it needs is for the react-datepicker-wrapper element that gets dynamically added by the library to have display: block. Any modifications I make to react-datepicker-wrapper in my own css does nothing.

What should I do?

enter image description here

date-picker.component.jsx

import React from 'react'; import DatePicker from 'react-datepicker'; import 'react-datepicker/dist/react-datepicker-cssmodules.css'; import './date-picker.component.bootstrap.css';  // eslint-disable-next-line no-confusing-arrow const buildClassNames = (touched, isInvalid) => touched && isInvalid ? 'form-control is-invalid' : 'form-control';  export const DatePickerBootstrap = (props) => {   const { setFieldValue, setFieldTouched, errors, touched } = props;   const { name, value, label, ...rest } = props;    return ( <div className="form-group">     <label className='datePickerLabel' htmlFor={name}>{label}</label>     <DatePicker     selected={value}     onChange={(e) => {       setFieldValue(name, e);       setFieldTouched(name);     }}     className={buildClassNames(touched, !!errors)}     customInput={         <input         type="text"         id={name}         placeholder={label} />     }     {...rest}     />      <div className="invalid-feedback">         {errors}     </div> </div>   ); };  export default DatePickerBootstrap; 
like image 280
Sebastian Patten Avatar asked Apr 22 '19 12:04

Sebastian Patten


People also ask

How do you style datepicker in react?

To style react-datepicker, we can set the wrapperClassName prop to a class name. And then we can apply styles to that class name. We set wrapperClassName to 'date-picker' . And then we add some styles to the class in the style element.


1 Answers

I think you're just missing some CSS. Try this in your custom stylesheet (anywhere after the datepicker's stylesheet):

.react-datepicker-wrapper, .react-datepicker__input-container, .react-datepicker__input-container input {     display: block;     width: 100%; } 

Demo

like image 158
isherwood Avatar answered Sep 22 '22 15:09

isherwood