github.com/minio/console@v1.4.1/web-app/src/screens/Console/Common/FormComponents/DateSelector/DateSelector.tsx (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2021 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 import React, { 18 forwardRef, 19 useEffect, 20 useImperativeHandle, 21 useState, 22 } from "react"; 23 import { Box, HelpIcon, InputLabel, Select, Tooltip } from "mds"; 24 import { days, months, validDate, years } from "./utils"; 25 26 interface IDateSelectorProps { 27 id: string; 28 label: string; 29 disableOptions?: boolean; 30 tooltip?: string; 31 borderBottom?: boolean; 32 value?: string; 33 onDateChange: (date: string, isValid: boolean) => any; 34 } 35 36 const DateSelector = forwardRef( 37 ( 38 { 39 id, 40 label, 41 disableOptions = false, 42 tooltip = "", 43 borderBottom = false, 44 onDateChange, 45 value = "", 46 }: IDateSelectorProps, 47 ref: any, 48 ) => { 49 useImperativeHandle(ref, () => ({ resetDate })); 50 51 const [month, setMonth] = useState<string>(""); 52 const [day, setDay] = useState<string>(""); 53 const [year, setYear] = useState<string>(""); 54 55 useEffect(() => { 56 // verify if there is a current value 57 // assume is in the format "2021-12-30" 58 if (value !== "") { 59 const valueSplit = value.split("-"); 60 61 setYear(valueSplit[0]); 62 setMonth(valueSplit[1]); 63 // Turn to single digit to be displayed on dropdown buttons 64 setDay(`${parseInt(valueSplit[2])}`); 65 } 66 }, [value]); 67 68 useEffect(() => { 69 const [isValid, dateString] = validDate(year, month, day); 70 onDateChange(dateString, isValid); 71 }, [month, day, year, onDateChange]); 72 73 const resetDate = () => { 74 setMonth(""); 75 setDay(""); 76 setYear(""); 77 }; 78 79 const isDateDisabled = () => { 80 if (disableOptions) { 81 return disableOptions; 82 } else { 83 return false; 84 } 85 }; 86 87 const monthForDropDown = [{ value: "", label: "<Month>" }, ...months]; 88 const daysForDrop = [{ value: "", label: "<Day>" }, ...days]; 89 const yearsForDrop = [{ value: "", label: "<Year>" }, ...years]; 90 91 return ( 92 <Box className={"inputItem"}> 93 <Box 94 sx={{ 95 display: "flex", 96 alignItems: "center", 97 gap: 5, 98 marginBottom: 5, 99 }} 100 > 101 <InputLabel htmlFor={id}> 102 <span>{label}</span> 103 {tooltip !== "" && ( 104 <Box 105 sx={{ 106 marginLeft: 5, 107 display: "flex", 108 alignItems: "center", 109 "& .min-icon": { 110 width: 13, 111 }, 112 }} 113 > 114 <Tooltip tooltip={tooltip} placement="top"> 115 <Box 116 sx={{ 117 "& .min-icon": { 118 width: 13, 119 }, 120 }} 121 > 122 <HelpIcon /> 123 </Box> 124 </Tooltip> 125 </Box> 126 )} 127 </InputLabel> 128 </Box> 129 <Box sx={{ display: "flex", gap: 12 }}> 130 <Select 131 id={`${id}-month`} 132 name={`${id}-month`} 133 value={month} 134 onChange={(newValue) => { 135 setMonth(newValue); 136 }} 137 options={monthForDropDown} 138 label={""} 139 disabled={isDateDisabled()} 140 /> 141 142 <Select 143 id={`${id}-day`} 144 name={`${id}-day`} 145 value={day} 146 onChange={(newValue) => { 147 setDay(newValue); 148 }} 149 options={daysForDrop} 150 label={""} 151 disabled={isDateDisabled()} 152 /> 153 154 <Select 155 id={`${id}-year`} 156 name={`${id}-year`} 157 value={year} 158 onChange={(newValue) => { 159 setYear(newValue); 160 }} 161 options={yearsForDrop} 162 label={""} 163 disabled={isDateDisabled()} 164 sx={{ 165 marginBottom: 12, 166 }} 167 /> 168 </Box> 169 </Box> 170 ); 171 }, 172 ); 173 174 export default DateSelector;