rc-component-picker

repository·master·Indexed 43 days ago

https://github.com/react-component/picker

A set of React primitives for date, time, range, and panel picking, serving as the foundation for Ant Design's date/time pickers. Version 1.14.0 provides pluggable adapters for date libraries including dayjs, date-fns, luxon, and moment. It includes the Picker, RangePicker, and PickerPanel components with support for custom locales, time selection, and date disabling.

Tokens
18K
Snippets
42
Records
61
Agent score
54%

What's inside rc-component-picker

  1. Configure time display via showTime prop

    master

    When using a picker in datetime or time mode, you can control the visibility of time components (hours, minutes, seconds, milliseconds) using the showTime prop.

    showTime can be a boolean or a configuration object of type SharedTimeProps. If provided as an object, it allows fine-grained control over time-related settings such as steps, disabled ranges, and visibility. The component automatically infers visibility from the format string if explicit showXXX props are not provided.

    // Example of passing time configuration
    <Picker
      picker="datetime"
      showTime={{
        showHour: true,
        showMinute: true,
        showSecond: false,
        hourStep: 1,
        minuteStep: 5,
      }}
    />
  2. How PickerPanel handles selection and modes

    master

    The PickerPanel manages two distinct types of values:

    1. Real Value (value/onChange): This is the actual selection made by the user. It is triggered when the user selects a value in the current picker mode.
    2. Calendar Value (pickerValue/onPickerValueChange): This is a temporary value used to control which date/month/year is currently being viewed in the panel, without necessarily selecting it as the final value.

    When a user selects a date in a mode that is not the final picker mode (e.g., selecting a year while in year mode to move to month mode), the component automatically transitions the mode to the next logical step in the hierarchy (e.g., year $\rightarrow$ month $\rightarrow$ date).

  3. Handle Picker Value changes with pickerValue

    master

    When you use the pickerValue prop to control the date displayed in the popup panel, you must also provide the onPickerValueChange callback to keep the component state in sync. This is useful for custom navigation or external controls of the calendar view.

    onPickerValueChange provides the new date and an info object containing the source ('reset' or 'panel') and the next mode for both start and end panels.

    <RangePicker
      pickerValue={currentPanelDate}
      onPickerValueChange={(date, info) => {
        // date: [Date, Date]
        // info.source: 'reset' | 'panel'
        // info.mode: [PanelMode, PanelMode]
        setCurrentPanelDate(date);
      }}
    />
  4. Use the RangePicker component

    master

    The RangePicker component allows users to select a range of dates or times. It supports controlled and uncontrolled modes, custom date types, and features like presets, time selection, and range disabling.

    Key features:

    • Controlled/Uncontrolled: Use value and onChange for controlled mode, or defaultValue for uncontrolled.
    • Presets: Provide a list of predefined ranges via the presets prop.
    • Time Selection: Enable time picking by passing showTime.
    • Range Control: Disable specific fields in the range using disabled (can be a boolean or an array [boolean, boolean]).
    • Picker Value: Control the date currently displayed in the popup panel using pickerValue.
    import RangePicker from './RangePicker';
    
    // Example of a controlled RangePicker
    const MyComponent = () => {
      const [value, setValue] = React.useState<[Date | null, Date | null]>(null);
    
      return (
        <RangePicker
          value={value}
          onChange={(dates, dateStrings) => {
            console.log('Selected dates:', dates);
            console.log('Date strings:', dateStrings);
            setValue(dates);
          }}
          presets={[
            { label: 'Last 7 Days', value: [/* start date, end date */] }
          ]}
        />
      );
    };
  5. Use the Picker component

    master

    The Picker component is a versatile date/time selection component. It supports single date selection, multiple date selection (via the multiple prop), and time/datetime modes. It can be used as a controlled or uncontrolled component using value/onChange or defaultValue.

    Key features include:

    • Multiple selection: Enable by setting multiple={true}. This enables maxTagCount and tagRender props for managing selected date tags.
    • Time selection: Enable by providing showTime configuration.
    • Presets: Provide quick-select options via the presets prop.
    • Custom Rendering: Customize the appearance of cells, months, and dates using cellRender, monthCellRender, and dateRender.
    import Picker from './Picker'; // Assuming the exported component is imported as Picker
    
    // Basic usage
    <Picker
      value={date}
      onChange={(date, dateString) => console.log(date, dateString)}
    />
    
    // Multiple selection
    <Picker
      multiple
      value={[date1, date2]}
      onChange={(dates, dateStrings) => console.log(dates, dateStrings)}
      maxTagCount="responsive"
      tagRender={(props) => <span>{props.label}</span>}
    />
  6. How to use the Picker component

    master

    The Picker component is used for selecting a single date or time value. It requires a generateConfig adapter (to bridge the component with your chosen date library) and a locale object.

    Note: You must import the component's CSS for correct styling.

    import type { Dayjs } from 'dayjs';
    import dayjs from 'dayjs';
    import Picker from '@rc-component/picker';
    import dayjsGenerateConfig from '@rc-component/picker/generate/dayjs';
    import enUS from '@rc-component/picker/locale/en_US';
    import '@rc-component/picker/assets/index.css';
    
    export default () => (
      <Picker<Dayjs> generateConfig={dayjsGenerateConfig} locale={enUS} defaultValue={dayjs()} />
    );
  7. View the RangePicker demo example

    master

    The range demo showcases the usage of the RangePicker component. You can find the implementation details and usage patterns in the source file located at ../examples/range.tsx relative to this documentation file.

    // See implementation in ../examples/range.tsx
  8. How to use RangePicker with presets and time

    master

    The RangePicker allows selecting a start and end date/time. You can enable time selection via showTime and define quick-selection presets.

    Example using dayjs:

    import type { Dayjs } from 'dayjs';
    import { RangePicker } from '@rc-component/picker';
    import dayjsGenerateConfig from '@rc-component/picker/generate/dayjs';
    import enUS from '@rc-component/picker/locale/en_US';
    
    export default () => (
      <RangePicker<Dayjs>
        generateConfig={dayjsGenerateConfig}
        locale={enUS}
        showTime
        presets={[
          {
            label: 'Today',
            value: () => [dayjsGenerateConfig.getNow(), dayjsGenerateConfig.getNow()],
          },
        ]}
      />
    );
    import type { Dayjs } from 'dayjs';
    import { RangePicker } from '@rc-component/picker';
    import dayjsGenerateConfig from '@rc-component/picker/generate/dayjs';
    import enUS from '@rc-component/picker/locale/en_US';
    export default () => (
      <RangePicker<Dayjs>
        generateConfig={dayjsGenerateConfig}
        locale={enUS}
        showTime
        presets={[
          {
            label: 'Today',
            value: () => [dayjsGenerateConfig.getNow(), dayjsGenerateConfig.getNow()],
          },
        ]}
      />
    );
  9. How to use the RangePicker component

    master

    The RangePicker component is used for selecting a start and end date/time range. It supports features like showTime for time selection and presets for quick selection of common ranges.

    import type { Dayjs } from 'dayjs';
    import { RangePicker } from '@rc-component/picker';
    import dayjsGenerateConfig from '@rc-component/picker/generate/dayjs';
    import enUS from '@rc-component/picker/locale/en_US';
    
    export default () => (
      <RangePicker<Dayjs>
        generateConfig={dayjsGenerateConfig}
        locale={enUS}
        showTime
        presets={[
          {
            label: 'Today',
            value: () => [dayjsGenerateConfig.getNow(), dayjsGenerateConfig.getNow()],
          },
        ]}
      />
    );
  10. Disable specific dates in the Picker using disabledDate

    master

    You can restrict the selection of certain dates in the Picker component by providing a disabledDate prop. The disabledDate prop accepts a function that receives a date object and returns a boolean: true if the date should be disabled, and false otherwise.

    This is commonly used to prevent users from selecting past dates, future dates, or specific ranges of dates.

    // Example logic for disabling past dates
    const disabledDate = (current: Dayjs | null) => {
      return current && current < dayjs().startOf('day');
    };
    
    <Picker
      disabledDate={disabledDate}
    />
  11. How to use Picker with a date library

    master

    To use the Picker component, you must provide a generateConfig adapter corresponding to your chosen date library (e.g., dayjs, moment, luxon, or date-fns) and a locale object.

    Example using dayjs:

    import type { Dayjs } from 'dayjs';
    import dayjs from 'dayjs';
    import Picker from '@rc-component/picker';
    import dayjsGenerateConfig from '@rc-component/picker/generate/dayjs';
    import enUS from '@rc-component/picker/locale/en_US';
    import '@rc-component/picker/assets/index.css';
    
    export default () => (
      <Picker<Dayjs> generateConfig={dayjsGenerateConfig} locale={enUS} defaultValue={dayjs()} />
    );
    import type { Dayjs } from 'dayjs';
    import dayjs from 'dayjs';
    import Picker from '@rc-component/picker';
    import dayjsGenerateConfig from '@rc-component/picker/generate/dayjs';
    import enUS from '@rc-component/picker/locale/en_US';
    import '@rc-component/picker/assets/index.css';
    export default () => (
      <Picker<Dayjs> generateConfig={dayjsGenerateConfig} locale={enUS} defaultValue={dayjs()} />
    );