-
Notifications
You must be signed in to change notification settings - Fork 6
feat(date-range-picker): add samples #889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: vnext
Are you sure you want to change the base?
Conversation
import "igniteui-webcomponents/themes/light/bootstrap.css"; | ||
|
||
export default function DrpSlots() { | ||
const dateRangeRef = useRef<IgrDateRangePicker>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed the recommended "React way" to reference components instead of the querySelector
. Let's stick to this approach in all samples.
registerIconFromText("appsIcon", Apps); | ||
registerIconFromText("binIcon", Bin); | ||
registerIconFromText("downArrowIcon", Down); | ||
registerIconFromText("uploadIcon", Upload); | ||
|
||
useEffect(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most samples in the repo seem to invoke the registerIconFromText
from within useEffect
.
useEffect(() => { | ||
const today = new Date(); | ||
const nextSeven = new Date( | ||
today.getFullYear(), | ||
today.getMonth(), | ||
today.getDate() + 7 | ||
); | ||
const nextWeek: CustomDateRange[] = [ | ||
{ | ||
label: "Next 7 days", | ||
dateRange: { | ||
start: today, | ||
end: nextSeven, | ||
}, | ||
}, | ||
]; | ||
const dateRange = document.querySelector("igc-date-range-picker") as IgrDateRangePicker; | ||
dateRange.customRanges = nextWeek; | ||
dateRange.usePredefinedRanges = true; | ||
}, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useEffect
is rather an overhead for simply binding static properties.
customRanges
can be declared in the function scope and bound. A ref to the DRP would not even be needed.
<IgrDateRangePicker
mode="dialog"
usePredefinedRanges
customRanges={nextWeek}
label="Custom Ranges">
</IgrDateRangePicker>
Also, usePredefinedRanges is already set as a property on the component.
useEffect(() => { | ||
let dateRange = document.querySelector("igc-date-range-picker") as IgrDateRangePicker; | ||
const today: Date = new Date(); | ||
let endDate: Date = new Date(); | ||
endDate.setDate(today.getDate() + 3); | ||
dateRange.value = { start: today, end: endDate }; | ||
}, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned, useEffect
can be avoided in the DRP samples and the static properties can be bound.
If some interactivity is required, the useState
hook can be used:
const [range, setRange] = useState(initialRange);
import { IgrDateRangePicker, IgrButton, IgrDialog } from 'igniteui-react'; | ||
import 'igniteui-webcomponents/themes/light/bootstrap.css'; | ||
|
||
export default function DrpForm() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sample can be reworked by the other suggestions - use useRef
, useState
, etc.
function showDialog(data: HTMLFormElement) { | ||
let dateRange = data.DateRange.value; | ||
const dialog = document.createElement("igc-dialog") as IgrDialog; | ||
dialog.addEventListener("igcClosed", () => dialog.remove()); | ||
const dump = document.createElement("pre"); | ||
dump.innerHTML = `<b>Start</b>: ${dateRange.start} \n<b>End</b>: ${dateRange.end}`; | ||
dialog.appendChild(dump); | ||
document.body.appendChild(dialog); | ||
dialog.show(); | ||
} | ||
}, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably no need to dynamically recreate the Dialog each time. It can be defined in the template, and only its content be updated. For example:
const dialog = useRef<IgrDialog>(null);
<IgrDialog ref={dialog}>
<pre>
<b>Start</b>: {range?.start?.toString() ?? ''} <br />
<b>End</b>: {range?.end?.toString() ?? ''}
</pre>
</IgrDialog>
const showDialog = (event: React.FormEvent<HTMLFormElement>) => {
if (dialog.current) {
event.preventDefault();
dialog.current.show();
}
}
useEffect(() => { | ||
document.getElementById("DateForm")?.addEventListener("submit", (event: SubmitEvent) => { | ||
event.preventDefault(); | ||
showDialog(event.target as HTMLFormElement); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to handle it on the form directly
<form id="DateForm" onSubmit={showDialog}>
const showDialog = (event: React.FormEvent<HTMLFormElement>) => {
if (dialog.current) {
event.preventDefault();
dialog.current.show();
}
}
<IgrDateRangePicker | ||
required | ||
min={new Date("2025-05-01")} | ||
label="Date Range" | ||
id="DateRange" | ||
></IgrDateRangePicker> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reflect the dynamic value, useState
can be applied:
const [range, setRange] = useState({start: null, end: null});
<IgrDateRangePicker
onChange={handleDateRangeChange}
></IgrDateRangePicker>
const handleDateRangeChange = (event: CustomEvent) => {
const newRange = {
start: event.detail?.start || null,
end: event.detail?.end || null
};
setRange(newRange);
};
Closes: #881
igniteui-react - 19.1.0 and igniteui-webcomponents - 6.1.2 are needed to run the samples. PR #890 updates the project packages.