Skip to content

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

Open
wants to merge 3 commits into
base: vnext
Choose a base branch
from

Conversation

adrianptrv
Copy link
Contributor

@adrianptrv adrianptrv commented Jul 31, 2025

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.

@adrianptrv adrianptrv added the area: enhancement related to code of SB, new feature or request label Jul 31, 2025
@kacheshmarova kacheshmarova requested review from ddaribo and removed request for kacheshmarova August 1, 2025 04:30
import "igniteui-webcomponents/themes/light/bootstrap.css";

export default function DrpSlots() {
const dateRangeRef = useRef<IgrDateRangePicker>();
Copy link
Contributor

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.

Comment on lines 20 to 25
registerIconFromText("appsIcon", Apps);
registerIconFromText("binIcon", Bin);
registerIconFromText("downArrowIcon", Down);
registerIconFromText("uploadIcon", Upload);

useEffect(() => {
Copy link
Contributor

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.

Comment on lines 9 to 28
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;
}, []);
Copy link
Contributor

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.

Comment on lines 8 to 14
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 };
}, []);
Copy link
Contributor

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() {
Copy link
Contributor

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.

Comment on lines 14 to 24
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();
}
}, []);
Copy link
Contributor

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();
     }
  }

Comment on lines 8 to 13
useEffect(() => {
document.getElementById("DateForm")?.addEventListener("submit", (event: SubmitEvent) => {
event.preventDefault();
showDialog(event.target as HTMLFormElement);
});

Copy link
Contributor

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();
     }
  }

Comment on lines 30 to 35
<IgrDateRangePicker
required
min={new Date("2025-05-01")}
label="Date Range"
id="DateRange"
></IgrDateRangePicker>
Copy link
Contributor

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);
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: enhancement related to code of SB, new feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Date Range Picker: Add samples for newly added component
2 participants