Allow Datepicker to be empty

未关闭
#1,393 4 条评论 7 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
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.2k
派生
504
平均合并
19 小时 16 分钟
30 天内合并 PR
2

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

themesberg/flowbite-react 的其他 Issue

查看 themesberg/flowbite-react 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。