Resetting a field (or a form) doesn't emit an event

未关闭
#1,570 8 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
技术栈
react, typescript
领域
frontend

调研方向

该 issue 没有指定文件或测试;先跟踪 example 中的 resetField 入口点以及 onChange 周围的字段监听器处理逻辑。将 reset 行为与普通的 setValue 变更进行比较,然后找出现有的表单或字段事件测试并加以扩展。完成的标准是,重置字段或表单会产生文档中说明的、监听器所需的事件。

由索引模型根据 Issue 内容生成。

描述

v1 v2: needs investigation

Hello,
I have a form with a value I sync with my search param.
To make it sync on mount (search param -> form), I use onMount listener of the field.
To make it sync back later (form -> search param), I intend to use onChange for every field change.

But I figured out that clearing a field doesn't actually emit any event I can listen to.
I ended up moving search param reset function to the place where reset actually happens, but I believe the "reset" event might be a thing to have.

My example (simplified):

const formSchema = z.object({
  userId: z.string(),
    // other values
});

type Form = z.infer<typeof formSchema>;

const Component = () => {
  const [searchParamUserId, setSearchParamUserId] = useSearchParam("userId");
  
  const defaultValues: Partial<Form> = {
    userId: undefined,
  };
  
  const form = useAppForm({
    defaultValues: defaultValues as Form,
    validators: {
      onMount: formSchema,
      onChange: formSchema,
      onSubmit: formSchema,
    },
  });
  
  return (
    <form.AppField
      name="userId"
      listeners={{
        onMount: ({ fieldApi }) => {
          if (userId) {
            fieldApi.setValue(searchParamUserId);
          }
        },
        onChange: ({ value }) => setSearchParamUserId(value),
      }}
    >
      {(field) => (
        <UsersSelect
          selected={field.state.value}
          onSelect={(nextUserId) => {
            // Clicking on the same user should reset value to undefined
            if (nextUserId === field.state.value) {
              form.resetField("userId");
              // Unfortunately, resetting field doesn't emit a listener event
              setSearchParamUserId(undefined);
            } else {
              field.setValue(nextUserId);
            }
          }}
        />
      )}
    </form.AppField>
  );
};
主要语言
TypeScript
星标
6.8k
派生
717
平均合并
5 天 22 小时
30 天内合并 PR
14

环境准备

从这里开始

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

TanStack/form 的其他 Issue

查看 TanStack/form 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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