Last active
June 9, 2019 20:05
-
-
Save JwanKhalaf/3dc2878d7408c3f0af06d8e8a79d098a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import { render } from "react-dom"; | |
import Dropdown from "./components/Dropdown/Dropdown"; | |
import InlineDropdowns from "./components/InlineDropdowns/InlineDropdowns"; | |
import "./reset.css"; | |
import "./App.css"; | |
function App() { | |
const [selectedMonth, setSelectedMonth] = useState(""); | |
const [selectedYear, setSelectedYear] = useState(""); | |
const handleMonthChange = event => { | |
const selectedValue = event.target.value; | |
setSelectedMonth(selectedValue); | |
}; | |
const handleYearChange = event => { | |
const selectedValue = event.target.value; | |
setSelectedYear(selectedValue); | |
}; | |
function optionDisabler(value) { | |
const currentDate = new Date(); | |
const currentMonth = currentDate.getMonth(); | |
if (selectedYear && selectedYear === currentDate.getFullYear().toString()) { | |
if (parseInt(value) > currentMonth) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function getValidYears() { | |
const startYear = 2014; | |
const currentYear = new Date().getFullYear(); | |
const validYears = []; | |
for (var i = 0; i <= currentYear - startYear; i++) { | |
validYears.push({ id: startYear + i, name: startYear + i }); | |
} | |
return validYears.reverse(); | |
} | |
const months = [ | |
{ id: "01", name: "Jan" }, | |
{ id: "02", name: "Feb" }, | |
{ id: "03", name: "Mar" }, | |
{ id: "04", name: "Apr" }, | |
{ id: "05", name: "May" }, | |
{ id: "06", name: "Jun" }, | |
{ id: "07", name: "Jul" }, | |
{ id: "08", name: "Aug" }, | |
{ id: "09", name: "Sep" }, | |
{ id: "10", name: "Oct" }, | |
{ id: "11", name: "Nov" }, | |
{ id: "12", name: "Dec" } | |
]; | |
return ( | |
<> | |
<aside> | |
<h1 className="app__title">MyApp</h1> | |
<form> | |
<InlineDropdowns label="Date"> | |
<Dropdown | |
options={months} | |
onChange={handleMonthChange} | |
value={selectedMonth} | |
optionDisabler={optionDisabler} | |
/> | |
<Dropdown | |
options={getValidYears()} | |
onChange={handleYearChange} | |
value={selectedYear} | |
/> | |
</InlineDropdowns> | |
</form> | |
</aside> | |
<section> | |
<h2 className="section__title">Placeholder</h2> | |
</section> | |
</> | |
); | |
} | |
render(<App />, document.getElementById("root")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import "./Dropdown.css"; | |
function Dropdown(props) { | |
const dropdownOptions = props.options.map(opt => ( | |
<option | |
key={opt.id} | |
disabled={props.optionDisabler(opt.id)} | |
value={opt.id} | |
className="input__option" | |
> | |
{opt.name} | |
</option> | |
)); | |
const disabled = props.isDisabled; | |
let selectionPrompt; | |
if (disabled) { | |
selectionPrompt = ""; | |
} else { | |
selectionPrompt = "Please select"; | |
} | |
const initialOption = <option value="" className="input__option" />; | |
return ( | |
<select | |
disabled={disabled} | |
className="input__select" | |
onChange={props.onChange} | |
value={props.value} | |
> | |
{initialOption} | |
{dropdownOptions} | |
</select> | |
); | |
} | |
export default Dropdown; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment