How to reinit placeholder in datePicker? #2224
Unanswered
azica
asked this question in
Support from community
Replies: 1 comment
-
Hello! What do you mean by placeholder? The date inside the datepicker modal still looks like it has previous date? Try changing the inputs value (not defaultValue) and if the modal still has the old date, you can try to dispatch "input" event because the datepicker is listening to the input event and updates the datepicker based on that input.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is my datepicker code, after submitting form i am cleared the values inputs ofc datepicker and when i am open again the same form i see in the placeholder prev value which is not cleared? what to do? tries many times to useEffect but it does not help me
import type { FC } from "react";
import cn from "classnames";
import { useEffect, useState } from "react";
import { Datepicker, Input, initTE } from "tw-elements";
import { formatDate } from "@/helpers/utils";
import styles from "./Datepicker.module.scss";
const DatePicker: FC = ({
value,
hasLabelMark,
label,
id,
disabled,
field,
onChange,
required,
placeholder,
className,
inputSize,
dateProps,
invalid,
helperText,
}) => {
const [currentValue, setCurrentValue] = useState(value);
const [isInvalid, setIsInvalid] = useState(invalid ? invalid : false);
const [errorMessage, setErrorMessage] = useState(helperText ? helperText : "");
useEffect(() => {
const myInput = document.querySelector(
#id_${id}
);if (myInput) {
const init = async () => {
initTE({ Datepicker, Input }, { allowReinits: true });
// eslint-disable-next-line no-new
await new Datepicker(myInput, {
monthsFull: [
"Январь",
"Февраль",
"Март",
"Апрель",
"Май",
"Июнь",
"Июль",
"Август",
"Сентябрь",
"Октябрь",
"Ноябрь",
"Декабрь",
],
monthsShort: ["Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек"],
weekdaysNarrow: ["Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"],
weekdaysShort: ["Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"],
okBtnText: "Ok",
clearBtnText: "Очистить",
cancelBtnText: "Отменить",
format: "dd.mm.yyyy",
title: "Выберите дату",
// inline: true,
disableFuture: Boolean(dateProps?.disableFuture),
});
myInput?.addEventListener("dateChange.te.datepicker", (event: any) => {
setCurrentValue(formatDate(event.date));
});
};
init();
}
}, []);
useEffect(() => {
if (invalid !== isInvalid && invalid !== undefined) {
setIsInvalid(invalid);
}
}, [invalid]);
useEffect(() => {
if (helperText && helperText !== errorMessage) {
setErrorMessage(helperText);
}
if (helperText && helperText.length > 0) {
setIsInvalid(true);
}
}, [helperText]);
useEffect(() => {
setCurrentValue(value || "");
}, [value]);
useEffect(() => {
if (currentValue) {
onChange({ field, value: currentValue, id });
}
}, [currentValue]);
const datePickerClasses = cn(
styles.datePicker,
className && styles[className],
className && className,
inputSize && styles[inputSize],
);
return (
{label ? <label className={cn(styles.label, hasLabelMark && styles.labelMarked)}>{label} : null}
<div
className={cn(styles.inputWrap, "fadeIn", isInvalid && styles.error)}
data-te-input-wrapper-init
id={
id_${id}
}><input
type="text"
name={field}
defaultValue={currentValue === "" ? "" : currentValue}
placeholder={String(placeholder)}
required={required}
disabled={disabled}
className={styles.input}
data-te-datepicker-toggle-ref
/>
{isInvalid && {errorMessage}}
);
};
export default DatePicker;
Beta Was this translation helpful? Give feedback.
All reactions