@jackiemacklein/nettz-utils
Version:
Serviços de imagem, e-mail, códigos de barras, utilitários numéricos e componentes React para apps Node.js com TypeScript
137 lines (136 loc) • 6.7 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const react_multi_date_picker_1 = __importStar(require("react-multi-date-picker"));
require("react-multi-date-picker/styles/colors/red.css");
require("react-multi-date-picker/styles/layouts/mobile.css");
const gregorian_pt_br_1 = __importDefault(require("react-date-object/locales/gregorian_pt_br"));
const date_panel_1 = __importDefault(require("react-multi-date-picker/plugins/date_panel"));
const DateRangePicker = ({ value, onChange, range = true, }) => {
// Função para converter Date para string no formato YYYY-MM-DD respeitando o fuso horário local
const formatDateToString = (date) => {
if (!date)
return "";
// Usa o fuso horário local do navegador
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
};
// Converte para o formato aceito pelo componente
const pickerValue = react_1.default.useMemo(() => {
if (range) {
if ((value === null || value === void 0 ? void 0 : value.startDate) && (value === null || value === void 0 ? void 0 : value.endDate)) {
return [new react_multi_date_picker_1.DateObject(value.startDate), new react_multi_date_picker_1.DateObject(value.endDate)];
}
else if (value === null || value === void 0 ? void 0 : value.startDate) {
return [new react_multi_date_picker_1.DateObject(value.startDate)];
}
}
else if (value === null || value === void 0 ? void 0 : value.startDate) {
return [new react_multi_date_picker_1.DateObject(value.startDate)];
}
return [];
}, [value === null || value === void 0 ? void 0 : value.startDate, value === null || value === void 0 ? void 0 : value.endDate, range]);
const ref = (0, react_1.useRef)(null);
const handleClickOutside = (event) => {
var _a, _b;
const target = event.target;
const isClickOutside = !((_a = ref.current) === null || _a === void 0 ? void 0 : _a.contains(target));
if (isClickOutside) {
(_b = ref.current) === null || _b === void 0 ? void 0 : _b.closeCalendar();
}
};
(0, react_1.useEffect)(() => {
document.addEventListener("click", handleClickOutside, true);
return () => document.removeEventListener("click", handleClickOutside, true);
}, []);
return ((0, jsx_runtime_1.jsx)(react_multi_date_picker_1.default, { style: { width: "100%" }, value: pickerValue, inputClass: "form-control", required: true, numberOfMonths: 2, containerStyle: { width: "100%" }, rangeHover: true, range: range, onlyShowInRangeDates: true, locale: gregorian_pt_br_1.default,
// calendar={gregorian}
multiple: false, format: "DD/MM/YYYY", editable: false, onChange: (dates) => {
if (range) {
if (dates && Array.isArray(dates)) {
if (dates.length === 1) {
const dateObj = dates[0];
const date = dateObj.toDate();
onChange({
startDate: formatDateToString(date),
endDate: null,
});
}
else if (dates.length === 2) {
const startDateObj = dates[0];
const endDateObj = dates[1];
const startDate = startDateObj.toDate();
const endDate = endDateObj.toDate();
onChange({
startDate: formatDateToString(startDate),
endDate: formatDateToString(endDate),
});
}
else {
onChange({ startDate: "", endDate: null });
}
}
else {
onChange({ startDate: "", endDate: null });
}
}
else {
if (dates && Array.isArray(dates) && dates.length > 0) {
const dateObj = dates[0];
const date = dateObj.toDate();
onChange({
startDate: formatDateToString(date),
endDate: null,
});
}
else {
onChange({
startDate: "",
endDate: null,
});
}
}
}, plugins: !range ? [(0, jsx_runtime_1.jsx)(date_panel_1.default, {})] : [], render: (0, jsx_runtime_1.jsx)("input", { className: "nettz-input", placeholder: "Selecione o per\u00EDodo", style: { width: "100%" } }), calendarPosition: "bottom", ref: ref, onClose: () => {
// Callback quando o calendário é fechado
} }));
};
exports.default = DateRangePicker;