mantinedev / mantine

A fully featured React components library
https://mantine.dev
MIT License
26.26k stars 1.86k forks source link

Checkbox.Group Checkbox didn't check correctly after form.setFieldValue or form.setValues #6869

Closed wizztjh closed 4 days ago

wizztjh commented 5 days ago

Dependencies check up

What version of @mantine/* packages do you have in package.json?

6.0.21

What package has an issue?

@mantine/core

What framework do you use?

Next.js

In which browsers you can reproduce the issue?

Chrome

Describe the bug

Checkbox.Group Checkbox didn't update based on the form value via form.setFieldValue or form.setValues. The multiselect updated accordingly, but the Checkbox.Group Checkbox didn't reset when we clicked the reset filter by setting form.setValues({ filterBy: [], searchTags: [] });.

image

https://codesandbox.io/p/sandbox/mantine-react-template-forked-wcq5q5

import "@mantine/core/styles.css";
import { Anchor, Checkbox, MantineProvider, MultiSelect } from "@mantine/core";
import { useForm } from "@mantine/form";

export interface Filters {
  filterBy: string[];
  searchTags: string[];
}

export default function App() {
  const form = useForm<Filters>({
    initialValues: { filterBy: [], searchTags: [] },
  });
  const filterOptionsList = [{ label: "My Favorites", value: "myFavorites" }];
  const onResetFilter = () => {
    form.setValues({ filterBy: [], searchTags: [] });
  };
  return (
    <MantineProvider>
      <form>
        <Checkbox.Group
          label="Filter By"
          defaultValue={form.values.filterBy}
          {...form.getInputProps("filterBy", { type: "checkbox" })}
        >
          {filterOptionsList.map((option) => (
            <Checkbox
              key={`filter-checkbox-${option.value}`}
              label={option.label}
              value={option.value}
            />
          ))}
        </Checkbox.Group>
        <MultiSelect
          label="Tagged with"
          searchable
          multiple
          placeholder="Search by tag"
          {...form.getInputProps("searchTags", { type: "input" })}
          data={["What", "AI"]}
          clearable
        />
        <Anchor onClick={onResetFilter}> Reset filter</Anchor>
      </form>
    </MantineProvider>
  );
}

If possible, include a link to a codesandbox with a minimal reproduction

https://codesandbox.io/p/sandbox/mantine-react-template-forked-wcq5q5

Possible fix

No response

Self-service