Allow Datepicker to be empty

オープン
#1,393 コメント 4 件 リアクション 7 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
react, typescript
領域
frontend

調査の方向性

Datepicker コンポーネントから始めて、Clear ボタン、defaultDate、onSelectedDateChanged が現在どのように値を処理しているかを確認します。既存の動作を要求された null または空の状態と比較し、そのうえで、任意の日付をクリアすると期待される値になり、defaultDate や minDate の動作を損なわないことを確認します。

索引モデルが issue の本文から書いたものです。

説明

Summary

Datepicker only allows to receive a valid Date but it would be really great if we could set null, "" or any kind of value that represents empty.

Context

So, i was working in specific use case where users can set a Date but this field is optional (null under the hood), so the user can also keep it empty.
When i start this i saw Clear button and i tought this was exactly what i was looking for but then i realize that this button only resets the field to defaultDate or minDate but never to "" (as native input does) or null. Since Clear button resets to defaultDate, would be great if defaultDate accepts null or even if onSeletedDateChanged could receive the current value and a second boolean prop like isClearAction so we can customize the logic.

For now, since im using React Hook Form to deal with validations, i've reached this by using the following approach:

type MyDatepickerProps = Omit<
  DatepickerProps,
  'showClearButton' | 'labelClearButton'
> & {
  onClear?: () => void;
};

const MyDatepicker = forwardRef<
  DatepickerRef,
  MyDatepickerDatepickerProps
>(({ onClear, ...rest }, ref) => {
  return (
    <div>
      <Datepicker
        ref={ref}
        {...rest}
        showClearButton={!onClear}
        labelClearButton="Reset"
      />
      {onClear && (
        <button
          onClick={onClear}
          type="button"
        >
          {/* This is just a X icon for the button */}
          <HiXCircle />
        </button>
      )}
    </div>
  );
});

And in the form (remember: im using React Hook Form):

<Controller
  control={control}
  name="my-field"
  render={({ field }) => (
    <MyDatepicker
      minDate={new Date()}
      value={field.value === null ? '' : field.value?.toDateString()}
      placeholder="Date"
      onSelectedDateChanged={field.onChange}
      onClear={() => field.onChange(null)}
    />
  )}
/>;

As you can see, im not using default onChange since it not triggers with current value. Thats why im using Controller to customize field actions.

Would be very very good to have this behavior on this component 🥺

EDIT: I've already read some issues about that but this is a feature request since it is not implemented.

主要言語
TypeScript
スター
2.1k
フォーク
502
平均マージ
19時間 16分
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

themesberg/flowbite-react のほかの issue

themesberg/flowbite-react の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。