Framework:Javascript GridAngular GridReact GridVue Grid

React Grid: Date Component

You can create your own date components, and AG Grid will use them every time it needs to ask the user for a date value. The date components are currently used in date filters.

Simple Date Component

Below is a simple example of filter component as a Hook:

export default forwardRef((props, ref) => {
   const [date, setDate] = useState(null);
   const [picker, setPicker] = useState(null);
   const refFlatPickr = useRef();
   const refInput = useRef();

   //*********************************************************************************
   //          LINKING THE UI, THE STATE AND AG-GRID
   //*********************************************************************************

   const onDateChanged = (selectedDates) => {
       setDate(selectedDates[0]);
       props.onDateChanged();
   };

   useEffect(() => {
       setPicker(flatpickr(refFlatPickr.current, {
           onChange: onDateChanged,
           dateFormat: 'd/m/Y',
           wrap: true
       }));
   }, []);

   useEffect(() => {
       if (picker) {
           picker.calendarContainer.classList.add('ag-custom-component-popup');
       }
   }, [picker]);

   useEffect(() => {
       //Callback after the state is set. This is where we tell ag-grid that the date has changed so
       //it will proceed with the filtering and we can then expect AG Grid to call us back to getDate
       if (picker) {
           picker.setDate(date);
       }
   }, [date, picker]);

   useImperativeHandle(ref, () => ({
       //*********************************************************************************
       //          METHODS REQUIRED BY AG-GRID
       //*********************************************************************************
       getDate() {
           //ag-grid will call us here when in need to check what the current date value is hold by this
           //component.
           return date;
       },

       setDate(date) {
           //ag-grid will call us here when it needs this component to update the date that it holds.
           setDate(date);
       },

       //*********************************************************************************
       //          AG-GRID OPTIONAL METHODS
       //*********************************************************************************

       setInputPlaceholder(placeholder) {
           if (refInput.current) {
               refInput.current.setAttribute('placeholder', placeholder);
           }
       },

       setInputAriaLabel(label) {
           if (refInput.current) {
               refInput.current.setAttribute('aria-label', label);
           }
       }
   }));

   // inlining styles to make simpler the component
   return (
       <div className="ag-input-wrapper custom-date-filter" role="presentation" ref={refFlatPickr}>
           <input type="text" ref={refInput} data-input style={{ width: "100%" }} />
           <a class='input-button' title='clear' data-clear>
               <i class='fa fa-times'></i>
           </a>
       </div>
   );
});

And here is the same example as a Class-based Component:

export default class CustomDateComponent extends Component {
   constructor(props) {
       super(props);

       this.state = {
           date: null
       };
   }

   render() {
       //Inlining styles to make simpler the component
       return (
           <div className="ag-input-wrapper custom-date-filter" role="presentation" ref="flatpickr">
               <input type="text" ref="eInput" data-input style={{width: "100%"}}/>
               <a class='input-button' title='clear' data-clear>
                   <i class='fa fa-times'></i>
               </a>
           </div>
       );
   }

   componentDidMount() {
       this.picker = flatpickr(this.refs.flatpickr, {
           onChange: this.onDateChanged.bind(this),
           dateFormat: 'd/m/Y',
           wrap: true
       });

       this.eInput = this.refs.eInput;

       this.picker.calendarContainer.classList.add('ag-custom-component-popup');
   }

   //*********************************************************************************
   //          METHODS REQUIRED BY AG-GRID
   //*********************************************************************************

   getDate() {
       //ag-grid will call us here when in need to check what the current date value is hold by this
       //component.
       return this.state.date;
   }

   setDate(date) {
       //ag-grid will call us here when it needs this component to update the date that it holds.
       this.setState({date});
       this.picker.setDate(date);
   }

   //*********************************************************************************
   //          AG-GRID OPTIONAL METHODS
   //*********************************************************************************

   setInputPlaceholder(placeholder) {
       this.eInput.setAttribute('placeholder', placeholder);
   }

   setInputAriaLabel(label) {
       this.eInput.setAttribute('aria-label', label);
   }

   //*********************************************************************************
   //          LINKS THE INTERNAL STATE AND AG-GRID
   //*********************************************************************************

   updateAndNotifyAgGrid(date) {
       //Callback after the state is set. This is where we tell ag-grid that the date has changed so
       //it will proceed with the filtering and we can then expect AG Grid to call us back to getDate
       this.setState({date}, this.props.onDateChanged);
   }

   //*********************************************************************************
   //          LINKING THE UI, THE STATE AND AG-GRID
   //*********************************************************************************

   onDateChanged = (selectedDates) => {
       this.setState({date: selectedDates[0]});
       this.updateAndNotifyAgGrid(selectedDates[0]);
   };
}

Example: Custom Date Component

The example below shows how to register a custom date component that contains an extra floating calendar picker rendered from the filter field. The problem with this approach is that we have no control over third party components and therefore no way to implement a preventDefault when the user clicks on the Calendar Picker (for more info see Custom Floating Filter Example). Our way of fixing this problem is to add the ag-custom-component-popup class to the floating calendar.

Registering Date Components

By default the grid will use the browser provided date picker for Chrome and Firefox (as we think it's nice), but for all other browsers it will just provide a simple text field. You can use your own date picker to AG Grid by providing a custom Date Component as follows:

<AgGridReact
   frameworkComponents={{ agDateInput: CustomDateComponent }}
   ...other properties...
/>

Please see Provided Components for more information about overrided AG Grid provided components (as we're doing here by overriding agDateInput).

Custom Date Interface

The interface for a custom date component is as follows:

interface {
   // Returns the current date represented by this editor
   getDate(): Date;

   // Sets the date represented by this component
   setDate(date: Date): void;

   // Optional methods

   // Sets the input text placeholder
   setInputPlaceholder(placeholder: string): void;

   // Sets the input text aria label
   setInputAriaLabel(label: string): void;
}

Note that if you're using Hooks for Grid Components that have lifecycle/callbacks that the grid will call (for example, the getDate callback from an Date Component), then you'll need to expose them with forwardRef & useImperativeHandle.

Please refer to the Hook documentation (or the examples on this page) for more information.

Custom Filter Parameters

When a React component is instantiated the grid will make the grid APIs, a number of utility methods as well as the cell & row values available to you via props - the interface for what is provided is documented below.

interface IDateParams {
    // Callback method to call when the date has changed
    onDateChanged: () => void;
}