palantir / blueprint

A React-based UI toolkit for the web
https://blueprintjs.com/
Apache License 2.0
20.7k stars 2.17k forks source link

Add active menu styles to shortcuts for Date Range input #4052

Open ajmagic opened 4 years ago

ajmagic commented 4 years ago

Environment

Feature request

Expose selectedShortcutIndex and onShortcutChange props to DateRange Input, similar to DateRangePicker

Examples

I'm looking to use DateRangeInput to allow selection of month(s) at a time. Usually, the users are interested in current month, and forward and backward a couple of months. Hence I'm providing them dynamic shortcuts, depending on their current month selection. Since the selected shortcut index isn't exposed, the component erroneously shows the previous month being selected. The shortcuts to months are dynamically generated, so when April is selected it looks so:

image

And when March is selected, and then re-opening it, the page loads with February selected since it is now at index 1: image

dmackerman commented 4 years ago

Just ran into this also, as I expected onShortcutChange and selectedShortcutIndex to be available DateRangeInput, because well...it's the same component? 😄

iusfof commented 4 years ago

+1. This props are present in the documentation, but they don't work https://blueprintjs.com/docs/#datetime/daterangepicker.shortcuts

dmackerman commented 4 years ago

@iusfof We ended up extending the Component like so:

import { DateRangeInput } from '@blueprintjs/datetime';

class DateRangeInputShortcuts extends DateRangeInput {
  static getDerivedStateFromProps(props, state) {
    const { selectedShortcutIndex } = props;
    if (typeof selectedShortcutIndex === 'number') {
      return { ...state, selectedShortcutIndex };
    }

    return state;
  }

  handleShortcutChange = (shortcut, selectedShortcutIndex) => {
    const { onShortcutChange } = this.props;
    if (onShortcutChange) {
      onShortcutChange(shortcut, selectedShortcutIndex);
    }
    this.setState({ selectedShortcutIndex });
  };
}

export default DateRangeInputShortcuts;