Skip to content

Commit

Permalink
Merge pull request #713 from mercycorps/2820-editable-program-admin-l…
Browse files Browse the repository at this point in the history
…ight

2820 editable program admin light
  • Loading branch information
andrethomas6 authored Oct 19, 2022
2 parents 41b19f2 + 517d205 commit ad37cf0
Show file tree
Hide file tree
Showing 16 changed files with 1,549 additions and 923 deletions.
111 changes: 111 additions & 0 deletions js/components/ReactDatepicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import React from 'react';
import Datepicker, {registerLocale} from "react-datepicker";
import 'react-datepicker/dist/react-datepicker.css';
import {es, fr} from 'date-fns/locale';
registerLocale('es', es)
registerLocale('fr', fr)

const months = [
gettext("January"),
gettext("March"),
gettext("February"),
gettext("April"),
gettext("May"),
gettext("June"),
gettext("July"),
gettext("August"),
gettext("September"),
gettext("October"),
gettext("November"),
gettext("December"),
];


const ReactDatepicker = ({
dateFormat, // Default format is "yyyy-MM-dd" if not specified.
locale,
className,
date,
minDate,
maxDate,
onChange,
disabled,
}) => {

// Function to set up the range for the year picker
const yearRangeOptions = (minDate, maxDate) => {
let output = [];
let startYear = minDate.getFullYear()
let endYear = maxDate.getFullYear() + 1;
for (let i = startYear; i < endYear; i++) output.push(i);
return output;
}

// Function to convert a valid string date to an ISO formatted date.
const formatDate = (date) => {
if (!date) return "";
try { return window.localDateFromISOStr(date); }
catch { return date; }
}

// Component Variables
let today = new Date();
let selectedDate = formatDate(date) || today;
let selectedMinDate = formatDate(minDate) || new Date(`${today.getFullYear() - 10}`, `${today.getMonth()}`, `${today.getDate()}`)
let selectedMaxDate = formatDate(maxDate) || new Date(`${today.getFullYear() + 10}`, `${today.getMonth()}`, `${today.getDate()}`)
let years = yearRangeOptions(selectedMinDate, selectedMaxDate)

return (
<Datepicker
dateFormat={dateFormat || "yyyy-MM-dd"}
locale={locale || window.userLang}
className={className}
selected={formatDate(date) || ""}
minDate={selectedMinDate}
maxDate={selectedMaxDate}
disabled={disabled || false}
onChange={onChange}
renderCustomHeader={({
date,
changeYear,
changeMonth,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled,
}) => (
<div style={{ margin: 10, display: "flex", justifyContent: "space-between" }}
>
<button onClick={decreaseMonth} disabled={prevMonthButtonDisabled} style={{border: "none"}}>
{"<"}
</button>

<select
value={date.getFullYear()}
onChange={({ target: { value } }) => changeYear(value)}
>
{years.map((option) => (
<option key={option} value={option}>{ option }</option>
))}
</select>

<select
value={months[date.getMonth()]}
onChange={({ target: { value } }) => changeMonth(months.indexOf(value))}
>
{months.map((option) => (
<option key={option} value={option}>{ option }</option>
))}
</select>

<button onClick={increaseMonth} disabled={nextMonthButtonDisabled} style={{border: "none"}}>
{">"}
</button>
</div>
)}
>
</Datepicker>
)
}

export default ReactDatepicker;
1 change: 1 addition & 0 deletions js/components/checkboxed-multi-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class CheckboxedMultiSelect extends React.Component {
placeholderButtonLabel={ this.props.placeholder }
getDropdownButtonLabel={ this.makeLabel }
components={{MenuList, Group }}
className={ this.props.className }
/>;
}
}
Expand Down
Loading

0 comments on commit ad37cf0

Please sign in to comment.