0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AntDのMultiple-Cascaderのdefalutvalues

Last updated at Posted at 2024-02-18

背景

Cascaderにmultipleオプションをつけた際にdefalutvaluesの記述がdocを見ても分かリませんでした。
ここに備忘録としてまとめます。

image.png

sample ソース

import React from "react";
import "./index.css";
import { Cascader } from "antd";

interface Option {
  value: string | number;
  label: string;
  children?: Option[];
  disableCheckbox?: boolean;
}

const options: Option[] = [
  {
    label: "Light",
    value: "light",
    children: new Array(20)
      .fill(null)
      .map((_, index) => ({ label: `Number ${index}`, value: index })),
  },
  {
    label: "Bamboo",
    value: "bamboo",
    children: [
      {
        label: "Little",
        value: "little",
        children: [
          {
            label: "Toy Fish",
            value: "fish",
            disableCheckbox: true,
          },
          {
            label: "Toy Cards",
            value: "cards",
          },
          {
            label: "Toy Bird",
            value: "bird",
          },
        ],
      },
    ],
  },
];

const onChange = (value: string[][]) => {
  console.log(value);
};

const App: React.FC = () => (
  <Cascader
    style={{ width: "100%" }}
    options={options}
    onChange={onChange}
    multiple
    maxTagCount="responsive"
    defaultValues={[["bamboo", "little"], [["light", 0]]]}
  />
);

export default App;

defaultValuesに二次元配列を指定し、親から子までのvalueを左から要素にすることで設定することができます。

defaultValues={[["bamboo", "little"], [["light", 0]]]}

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?