xrutayisire / react-js-cron

A React cron editor built with antd
https://xrutayisire.github.io/react-js-cron/?path=/docs/reactjs-cron--demo
MIT License
249 stars 106 forks source link

Show a default value in a dropdown if no option is selected from it #68

Open VedantSG123 opened 4 months ago

VedantSG123 commented 4 months ago

allowEmpty not working properly A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] I want to restrict the user from setting the cron value to '*' in a particular dropdown.

Current Behaviour: If the user deselects all the values from the dropdown, it sets the value of that dropdown to '*'

https://github.com/xrutayisire/react-js-cron/assets/103552663/4c2b179b-279a-47c8-9798-6edacf7afff2

I don't want the value to become * when user deselects all the values from the hour dropdown, instead set it to some default value (not *).

Here is what I tried to do:

export const CronForWeekly = () => {
  const [value, setValue] = React.useState('0 12 * * 7');

  const filterExpr = (value: string) => {
    const exprArray = value.split(' ');
    const hourIndex = 1;

    if (exprArray[hourIndex] === '*') {
      exprArray[hourIndex] = '12';
    }

    setValue(exprArray.join(' '));
  };

  return (
    <div className='flex flex-col gap-4'>
      <div></div>
      <h3>Cron Value</h3>
      <div>{value}</div>
      <Cron
        value={value}
        setValue={filterExpr}
        clearButton={false}
        mode='single'
        defaultPeriod='week'
        allowedDropdowns={['hours', 'minutes', 'week-days']}
        clockFormat='12-hour-clock'
        allowClear={false}
        allowEmpty='never'
      />
    </div>
  );
};

But is not working properly, here is the attached screen record:

Screencast from 2024-06-27 10-56-27.webm

When I click on the default value itself, the expression is fine, but the change dosen't reflect on the drop-down, it still shows every hour in the dropdown

xrutayisire commented 4 months ago

Hi,

I saw two different things in your issue:

  1. [bug] allowEmpty='never' should return an error if cron value equals * * * * * Clearly there is a bug here, allowEmpty='never' should never allow * * * * * as a cron value, and should return an error. A fix is needed

  2. [enhancement] Add, allowEmpty support in dropdownsConfig Today, you would not have been able to do exactly what you want, with or without the bug. If you want to allowEmpty='never' for specifically one dropdown, it's not possible. Also, I see that you may not only want an error returned but a fallback to a default value. That is not supported.

I don't have the time to work on this library these days, but I would be happy to review a PR that fix the bug and add the new feature you would like.

xrutayisire commented 4 months ago

Note that dropdownsConfig prop first need a fix to prevent an infinite loop: https://github.com/xrutayisire/react-js-cron/issues/67