Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-js-cron

xrutayisire157.1kMIT5.0.1TypeScript support: included

A React cron editor with antd inspired by jqCron

react, reactjs, js, cron, crontab, editor, widget, generator, antd, ant-design, cronjob, schedule, parser

readme

ReactJS Cron

A React cron editor built with antd

npm package MIT License Badge

Build Lint Unit tests codecov

Live demo and usage at https://xrutayisire.github.io/react-js-cron/

react-js-cron example

Features

  • Zero dependencies except React and antd
  • Supports all standard cron expressions
  • Supports cron names for months and week days
  • Supports cron shortcuts
  • Supports "7" for Sunday
  • Supports two-way sync binding with input
  • Supports locale customization
  • Supports multiple selection by double-clicking on an option
  • And many more (disabled, read-only, 12-hour clock...)

Inspired by

TypeScript

react-js-cron is written in TypeScript with complete definitions

Installation

Be sure that you have these dependencies on your project:

  • react (>=17.0.0)
  • antd (>=5.8.0)
# NPM
npm install react-js-cron

# Yarn
yarn add react-js-cron

Usage

import { Cron } from 'react-js-cron'
import 'react-js-cron/dist/styles.css'

export function App() {
  const [value, setValue] = useState('30 5 * * 1,6')

  return <Cron value={value} setValue={setValue} />
}

Don't forget to import styles manually:

import 'react-js-cron/dist/styles.css'

Converter

If you want to use the converter used internally you can import it in your project:

import { converter } from 'react-js-cron'

const cronString = converter.getCronStringFromValues(
  'day', // period: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'reboot'
  [], // months: number[] | undefined
  [],  // monthDays: number[] | undefined
  [], // weekDays: number[] | undefined
  [2], // hours: number[] | undefined
  [1], // minutes: number[] | undefined
  false // humanizeValue?: boolean
)

console.log('cron string:', converted)
cron string: '1 2 * * *'

Examples

Learn more with dynamic settings.

API

CronProps {
  /**
   * Cron value, the component is by design a controlled component.
   * The first value will be the default value.
   *
   * required
   */
  value: string

  /**
   * Set the cron value, similar to onChange.
   * The naming tells you that you have to set the value by yourself.
   *
   * required
   */
  setValue:
    | (value: string, extra: { selectedPeriod }) => void
    | Dispatch<SetStateAction<string>>

  /**
   * Set the container className and used as a prefix for other selectors.
   * Available selectors: https://xrutayisire.github.io/react-js-cron/?path=/story/reactjs-cron--custom-style
   */
  className?: string

  /**
   * Humanize the labels in the cron component, SUN-SAT and JAN-DEC.
   *
   * Default: true
   */
  humanizeLabels?: boolean

  /**
   * Humanize the value, SUN-SAT and JAN-DEC.
   *
   * Default: false
   */
  humanizeValue?: boolean

  /**
   * Add a "0" before numbers lower than 10.
   *
   * Default: false
   */
  leadingZero?: boolean | ['month-days', 'hours', 'minutes']

  /**
   * Define the default period when the default value is empty.
   *
   * Default: 'day'
   */
  defaultPeriod?: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'reboot'

  /**
   * Disable the cron component.
   *
   * Default: false
   */
  disabled?: boolean

  /**
   * Make the cron component read-only.
   *
   * Default: false
   */
  readOnly?: boolean

  /**
   * Show clear button for each dropdown.
   *
   * Default: true
   */
  allowClear?: boolean

  /**
   * Define if empty should trigger an error.
   *
   * Default: 'for-default-value'
   */
  allowEmpty?: 'always' | 'never' | 'for-default-value'

  /**
   * Support cron shortcuts.
   *
   * Default: ['@yearly', '@annually', '@monthly', '@weekly', '@daily', '@midnight', '@hourly']
   */
  shortcuts?: boolean | ['@yearly', '@annually', '@monthly', '@weekly', '@daily', '@midnight', '@hourly', '@reboot']

  /**
   * Define the clock format.
   *
   * Default: undefined
   */
  clockFormat?: '12-hour-clock' | '24-hour-clock'

  /**
   * Display the clear button.
   *
   * Default: true
   */
  clearButton?: boolean

  /**
   * antd button props to customize the clear button.
   */
  clearButtonProps?: ButtonProps

  /**
   * Define the clear button action.
   *
   * Default: 'fill-with-every'
   */
  clearButtonAction?: 'empty' | 'fill-with-every'

  /**
   * Display error style (red border and background).
   *
   * Display: true
   */
  displayError?: boolean

  /**
   * Triggered when the cron component detects an error with the value.
   */
  onError?:
    | (error: {
      type: 'invalid_cron'
      description: string
    }) => void
    | Dispatch<SetStateAction<{
      type: 'invalid_cron'
      description: string
    }>>
    | undefined

  /**
   * Define if a double click on a dropdown option should automatically
   * select / unselect a periodicity.
   *
   * Default: true
   */
  periodicityOnDoubleClick?: boolean

  /**
   * Define if it's possible to select only one or multiple values for each dropdown.
   *
   * Even in single mode, if you want to disable the double click on a dropdown option that
   * automatically select / unselect a periodicity, set 'periodicityOnDoubleClick'
   * prop at false.
   *
   * When single mode is active and 'periodicityOnDoubleClick' is false,
   * each dropdown will automatically close after selecting a value
   *
   * Default: 'multiple'
   */
  mode?: 'multiple' | 'single'

  /**
   * Define which dropdowns need to be displayed.
   *
   * Default: ['period', 'months', 'month-days', 'week-days', 'hours', 'minutes']
   */
  allowedDropdowns?: [
    'period',
    'months',
    'month-days',
    'week-days',
    'hours',
    'minutes'
  ]

  /**
   * Define the list of periods available.
   *
   * Default: ['year', 'month', 'week', 'day', 'hour', 'minute', 'reboot']
   */
  allowedPeriods?: ['year', 'month', 'week', 'day', 'hour', 'minute', 'reboot']

  /**
   * Define a configuration that is used for each dropdown specifically.
   * Configuring a dropdown will override any global configuration for the same property.
   *
   * Configurations available:
   *
   * // See global configuration
   * // For 'months' and 'week-days'
   * humanizeLabels?: boolean
   *
   * // See global configuration
   * // For 'months' and 'week-days'
   * humanizeValue?: boolean
   *
   * // See global configuration
   * // For 'month-days', 'hours' and 'minutes'
   * leadingZero?: boolean
   *
   * // See global configuration
   * For 'period', 'months', 'month-days', 'week-days', 'hours' and 'minutes'
   * disabled?: boolean
   *
   * // See global configuration
   * For 'period', 'months', 'month-days', 'week-days', 'hours' and 'minutes'
   * readOnly?: boolean
   *
   * // See global configuration
   * // For 'period', 'months', 'month-days', 'week-days', 'hours' and 'minutes'
   * allowClear?: boolean
   *
   * // See global configuration
   * // For 'months', 'month-days', 'week-days', 'hours' and 'minutes'
   * periodicityOnDoubleClick?: boolean
   *
   * // See global configuration
   * // For 'months', 'month-days', 'week-days', 'hours' and 'minutes'
   * mode?: Mode
   *
   * // The function will receive one argument, an object with value and label.
   * // If the function returns true, the option will be included in the filtered set.
   * // Otherwise, it will be excluded.
   * // For 'months', 'month-days', 'week-days', 'hours' and 'minutes'
   * filterOption?: FilterOption
   *
   * Default: undefined
   */
  dropdownsConfig?: {
    'period'?: {
      disabled?: boolean
      readOnly?: boolean
      allowClear?: boolean
    }
    'months'?: {
      humanizeLabels?: boolean
      humanizeValue?: boolean
      disabled?: boolean
      readOnly?: boolean
      allowClear?: boolean
      periodicityOnDoubleClick?: boolean
      mode?: 'multiple' | 'single'
      filterOption?: ({
          value,
          label,
        }: {
          value: string
          label: string
        }) => boolean
    }
    'month-days'?: {
      leadingZero?: boolean
      disabled?: boolean
      readOnly?: boolean
      allowClear?: boolean
      periodicityOnDoubleClick?: boolean
      mode?: 'multiple' | 'single'
      filterOption?: ({
          value,
          label,
        }: {
          value: string
          label: string
        }) => boolean
    }
    'week-days'?: {
      humanizeLabels?: boolean
      humanizeValue?: boolean
      disabled?: boolean
      readOnly?: boolean
      allowClear?: boolean
      periodicityOnDoubleClick?: boolean
      mode?: 'multiple' | 'single'
      filterOption?: ({
          value,
          label,
        }: {
          value: string
          label: string
        }) => boolean
    }
    'hours'?: {
      leadingZero?: boolean
      disabled?: boolean
      readOnly?: boolean
      allowClear?: boolean
      periodicityOnDoubleClick?: boolean
      mode?: 'multiple' | 'single'
      filterOption?: ({
          value,
          label,
        }: {
          value: string
          label: string
        }) => boolean
    }
    'minutes'?: {
      leadingZero?: boolean
      disabled?: boolean
      readOnly?: boolean
      allowClear?: boolean
      periodicityOnDoubleClick?: boolean
      mode?: 'multiple' | 'single'
      filterOption?: ({
          value,
          label,
        }: {
          value: string
          label: string
        }) => boolean
    }
  }

  /**
   * Change the component language.
   * Can also be used to remove prefix and suffix.
   *
   * When setting 'humanizeLabels' you can change the language of the
   * alternative labels with 'altWeekDays' and 'altMonths'.
   *
   * The order of the 'locale' properties 'weekDays', 'months', 'altMonths'
   * and 'altWeekDays' is important! The index will be used as value.
   *
   * Default './src/locale.ts'
   */
  locale?: {
    everyText?: string
    emptyMonths?: string
    emptyMonthDays?: string
    emptyMonthDaysShort?: string
    emptyWeekDays?: string
    emptyWeekDaysShort?: string
    emptyHours?: string
    emptyMinutes?: string
    emptyMinutesForHourPeriod?: string
    yearOption?: string
    monthOption?: string
    weekOption?: string
    dayOption?: string
    hourOption?: string
    minuteOption?: string
    rebootOption?: string
    prefixPeriod?: string
    prefixMonths?: string
    prefixMonthDays?: string
    prefixWeekDays?: string
    prefixWeekDaysForMonthAndYearPeriod?: string
    prefixHours?: string
    prefixMinutes?: string
    prefixMinutesForHourPeriod?: string
    suffixMinutesForHourPeriod?: string
    errorInvalidCron?: string
    weekDays?: string[]
    months?: string[]
    altWeekDays?: string[]
    altMonths?: string[]
  }
}

License

MIT © xrutayisire

changelog

Changelog

react-js-cron follows Semantic Versioning 2.0.0.

5.0.1

  • Fix dropdownsConfig filterOption prop not working for months and week-days.

5.0.0

  • (Breaking change!) Fix issue #60: react-js-cron now only support antd >= v5.8.0, use "suffixIcon" instead of deprecated "showArrow" for Select component

4.1.0

  • (New feature) Issue #36: Dropdowns specific configuration (mode, allowClear, filterOption, ...)
  • Fix dropdowns popover style

4.0.0

  • (Breaking change!) Fix issue #55: react-js-cron now only support antd >= v5.5.0, change antd Select property "dropdownMatchSelectWidth" to "popupMatchSelectWidth"

3.2.0

  • Add themes support for antd v5 with ConfigProvider (@nefedov-dm)

3.1.0

  • Export converter (@useafterfree)

3.0.1

  • (Important) Issue #40: Antd v5 compatibility
  • (Breaking change!) Fix issue #35: react-js-cron now only support antd >= v4.23.0, change antd props dropdownClassName
  • Fix issue #41: Fix broken build with react-js-cron v3.0.0

3.0.0

❌ Do not use this release, broken build ❌

2.1.2

  • Fix issue #31: Fix cron parsing accepting incorrect values

2.1.1

  • Fix issue #30: Fix invalid expression parsing with multiple ranges

2.1.0

  • (New feature) Issue #28: Add an extra param in setValue function to always know the selected period

2.0.0

  • (Breaking change!) Issue #19: Remove CSS import in Cron component to support Next.js. It's now required to manually import "react-js-cron/dist/styles.css" file!
  • (Breaking change!) Issue #22: periodicityOnDoubleClick is not ignored anymore when single mode is active
  • (New feature) Issue #22: Automatically close a dropdown when single mode is active and periodicityOnDoubleClick is false
  • (New feature) Issue #28: Add the possibility to restrict visible dropdowns and periods

1.4.0

  • (New feature) Issue #22: Add the possibility to choose a single selection mode

1.3.1

  • Fix disabled mode broken since antd 13.0
  • Fix issue #15: Fix double setValue function call that prevents changing value

1.3.0

  • (New feature) Issue #12: Add the possibility to deactivate the double click feature
  • Fix antd automatic tree-shaking not working in many cases (Missing Select import)
  • Fix to allow equal min and max range values

1.2.1

  • Fix issue #6: Display problem for Selects with antd 4.10.0
  • Fix placeholder color that should not be gray but black as the value

1.2.0

  • (New feature) Issue #3: Add the possibility to translate alternative labels
  • Fix typo in README

1.1.1

  • Fix issue #2: antd Select cannot work when using Cron component
  • Add dependencies version in README
  • Add link in README to story for clear button action management

1.1.0

  • (Breaking change!) Drop support of antd version anterior to 4.6.0 due to change on rc-virtual-list
  • (Breaking change!) Fix period change not handling new value, changing period now change the value
  • Add the possibility to choose clear button action, empty or fill-with-every
  • Fix issue #1 Styling of popovers breaks with latest antd version
  • Fix a problem with the onBlur function not triggered by Select component
  • Fix antd automatic tree-shaking not working in many cases

1.0.8

  • Fix double-click wrong output
  • Rewrite the entire cron converter to support some missing cron expressions
  • Fix typo in peer dependencies
  • Improve read-only mode by hiding week days or month days if not needed

1.0.7

  • Improve rendering problems caused by the new responsive management
  • Fix locale update not changing some labels
  • Rename setError to onError to improve naming
  • Update README API section
  • Add @reboot to the supported shortcuts
  • Change leading zero prop type to be easy to use
  • Fix missing locale property for clear button text
  • Fix no-prefix class on period field
  • The default period should only be read once
  • Fix double margin-bottom on fields
  • Prevent select dropdown to overlap window by changing dropdown popup direction
  • Update demo links in README

1.0.6

  • Fix regression on multiple detection
  • Add a prop to choose a clock format, 12-hour clock or 24-hour clock
  • Fix bug with multiple on months and week days
  • Fix display of dropdown options when leadingZero is active
  • Update README to add features info and usage
  • Improve responsive design management
  • Add support for cron shortcuts by default
  • Update hooks dependencies to prevent multiple re-render
  • Support import with an alias

1.0.5

  • Update README image with new features
  • Add a prop leadingZero to add a '0' before number lower than 10
  • Add a prop to make the component read only
  • Add a prop to disable the component
  • Set day as default period
  • Add missing support for mixing week days with month and month days
  • Rename some locale properties to fix typos
  • Fix typo in jqCron name

1.0.4

  • Add a prop to humanize the value
  • The value should not be changed using humanizeLabels

1.0.3

  • Fix bug when the input string contains duplicates
  • Clear button should have a margin-bottom in case of a break line
  • Add prop to humanize labels for week days and months
  • Add a prop 'allowEmpty' to choose how the component handles the default value
  • Add a prop 'defaultPeriod' used when the default value is an empty string
  • Empty string and invalid string should not change the value
  • Fix a bug accepting string like '5-0'
  • Fix infinite loop for bad string like '*/0' or '1-2/0'
  • Always use 0 for Sunday
  • Fix a bug when using 0 for Sunday

1.0.2

  • Fix bug that caused the impossibility to unselect last value
  • Style modification to display errors correctly over antd default style
  • Update peerDependencies to add minimal versions
  • Improve custom select onClickOption function
  • Fix typo in README

1.0.1

  • Update README image example for npm

1.0.0

  • Initial release of the library