Closed daisycrego closed 3 years ago
Found a datepicker: https://reactdatepicker.com/
Install
yarn add react-datepicker
Import and use the DatePicker
component to add a start date and end date for the created
range:
import DatePicker from "react-datepicker";
() => { const [startDate, setStartDate] = useState(null); const [endDate, setEndDate] = useState(null); return ( <> <DatePicker selected={startDate} onChange={(date) => setStartDate(date)} /> <DatePicker selected={endDate} onChange={(date) => setEndDate(date)} /> </> ); };
Ended up using a styled Datepicker
component from datepicker-react
: https://www.npmjs.com/package/@datepicker-react/styled because that other approach resulted in some issues loading the CSS, whereas this is a styled component, there's no need to import a CSS file from the bundle.
I was using a different approach for the date pickers, but I don't love it. I'm going to use material UI before I get any further entrenched... https://material-ui-pickers.dev/getting-started/installation
@material-ui/pickers
yarn add @material-ui/pickers
@material-ui/pickers
was designed to use the date management library of your choice. We are providing interfaces for moment
, date-fns 2
, luxon
and dayjs
.luxon
, not moment
. Reasons below. moment
luxon
moment
is "done". Not dead, but not being actively maintained. And from the mouths of the devs themselves:
Reasons to keep using Moment
In most cases, you should not choose Moment for new projects. However there are some possible reasons you might want to keep using it.
Recommendations
There are several great options to consider using instead of Moment.
Luxon
Day.js
date-fns
luxon
moment
devs say:
Luxon can be thought of as the evolution of Moment. It is authored by Isaac Cambron, a long-time contributor to Moment. Provides international locales and timezones.
luxon
are even linked to moment: https://moment.github.io/luxon/#/?id=luxon
Luxon is a library for dealing with dates and times in JavaScript.
DateTime.now().setZone('America/New_York').minus({weeks:1}).endOf('day').toISO();
Install - Node/Webpack
@date-io/luxon@1.x luxon
Tell pickers which date management library it should use with
MuiPickersUtilsProvider
. This component takes autils
prop, and makes it available down the React tree with React Context. It should be used at the root of your component tree, or at the highest level you wish the pickers to be available.
import { MuiPickersUtilsProvider } from '@material-ui/pickers';
// pick a date util library
import MomentUtils from '@date-io/moment';
import DateFnsUtils from '@date-io/date-fns';
import LuxonUtils from '@date-io/luxon';
function App() {
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<Root />
</MuiPickersUtilsProvider>
);
}
ReactDOM.render(<App />, document.querySelector('#app'));
@material-ui/pickers
relies only on material-ui controls and the date management library of your choice. Please note that all components are controlled, meaning that it's required to pass the value
and onChange
props.
import React, { useState } from 'react';
import DateFnsUtils from '@date-io/date-fns'; // choose your lib
import {
DatePicker,
TimePicker,
DateTimePicker,
MuiPickersUtilsProvider,
} from '@material-ui/pickers';
function App() { const [selectedDate, handleDateChange] = useState(new Date());
return (
); }
Todo
Add
created
range as state to theEventsTable
.Plan
created
range selected be applied to the search query./events
api at the backend, and then the events api 's mongoose query will need to be revised to use the date range provided (if one is provided).