The MultiLevelSelect kit renders a multi leveled select dropdown based on data from the user. treeData
is a required prop that is expected to contain the data in the form of an array of objects. See code snippet for an example data array.
For the React version of the kit, the onSelect
prop returns an array of objects. This array contains all checked items irrespective of whether it is a parent, child or grandchild. Open the console on this example and check and uncheck checkboxes to see this is action!
For the Rails version of the kit, there is no onselect. The form submits as a array of strings, following the typical rails pattern of utilizing hidden inputs. The strings are the values of the selected items' ids. For example, ["103", "106", "107"].
import React from "react"; import MultiLevelSelect from 'playbook-ui' const treeData = [ { label: "Power Home Remodeling", value: "Power Home Remodeling", id: "powerhome1", expanded: true, children: [ { label: "People", value: "People", id: "people1", expanded: true, children: [ { label: "Talent Acquisition", value: "Talent Acquisition", id: "talent1", }, { label: "Business Affairs", value: "Business Affairs", id: "business1", children: [ { label: "Initiatives", value: "Initiatives", id: "initiative1", }, { label: "Learning & Development", value: "Learning & Development", id: "development1", }, ], }, { label: "People Experience", value: "People Experience", id: "experience1", }, ], }, { label: "Contact Center", value: "Contact Center", id: "contact1", children: [ { label: "Appointment Management", value: "Appointment Management", id: "appointment1", }, { label: "Customer Service", value: "Customer Service", id: "customer1", }, { label: "Energy", value: "Energy", id: "energy1", }, ], }, ], }, ]; const MultiLevelSelectDefault = (props) => { return ( <div> <MultiLevelSelect id='multiselect-default' onSelect={(selectedNodes) => console.log( "Selected Items", selectedNodes ) } treeData={treeData} /> </div> ) }; export default MultiLevelSelectDefault;
Optionally enable the single
variant to replace checkboxes with radios so the input accepts and returns only a single selection.
import React from "react"; import MultiLevelSelect from 'playbook-ui' const treeData = [ { label: "HQ", value: "HQ", id: "hq", }, { label: "Philadelphia", value: "Philadelphia", id: "phl", children: [ { label: "Marketing & Sales PHL", value: "Marketing & Sales PHL", id: "marketingPHL", }, { label: "Installation Office PHL", value: "Installation Office PHL", id: "installationPHL", }, { label: "Warehouse PHL", value: "Warehouse PHL", id: "warehousePHL", }, ] }, { label: "New Jersey", value: "New Jersey", id: "nj", children: [ { label: "New Jersey", value: "New Jersey", id: "nj1", children: [ { label: "Marketing & Sales NJ", value: "Marketing & Sales NJ", id: "marketingNJ", }, { label: "Installation Office NJ", value: "Installation Office NJ", id: "installationNJ", }, { label: "Warehouse NJ", value: "Warehouse NJ", id: "warehouseNJ", }, ], }, { label: "Princeton", value: "Princeton", id: "princeton", children: [ { label: "Marketing & Sales Princeton", value: "Marketing & Sales Princeton", id: "marketingPR", }, { label: "Installation Office Princeton", value: "Installation Office Princeton", id: "installationPR", }, { label: "Warehouse Princeton", value: "Warehouse Princeton", id: "warehousePR", }, ] }, ] }, { label: "Maryland", value: "Maryland", id: "MD", children: [ { label: "Marketing & Sales MD", value: "Marketing & Sales MD", id: "marketingMD", }, { label: "Installation Office MD", value: "Installation Office MD", id: "installationMD", }, { label: "Warehouse MD", value: "Warehouse MD", id: "warehouseMD", }, ] }, { label: "Connecticut", value: "Connecticut", id: "CT", children: [ { label: "Marketing & Sales CT", value: "Marketing & Sales CT", id: "marketingCT", }, { label: "Installation Office CT", value: "Installation Office CT", id: "installationCT", }, { label: "Warehouse CT", value: "Warehouse CT", id: "warehouseCT", }, ] }, ]; const MultiLevelSelectSingle = (props) => { return ( <div> <MultiLevelSelect id="multiselect-single" inputName="Power" onSelect={(selectedNode) => console.log("Selected Node", selectedNode)} treeData={treeData} variant="single" /> </div> ) }; export default MultiLevelSelectSingle;
Dynamically control your selectable options by passing hideRadio: true
to any node within your tree data to omit that node's radio selector. In this example we've hidden all radios except ultimate children (nodes without descendants).
import React from "react"; import MultiLevelSelect from 'playbook-ui' const treeData = [ { label: "HQ", value: "HQ", id: "hq1", }, { label: "Philadelphia", value: "Philadelphia", id: "phl1", hideRadio: true, children: [ { label: "Marketing & Sales PHL", value: "Marketing & Sales PHL", id: "marketingPHL1", }, { label: "Installation Office PHL", value: "Installation Office PHL", id: "installationPHL1", }, { label: "Warehouse PHL", value: "Warehouse PHL", id: "warehousePHL1", }, ] }, { label: "New Jersey", value: "New Jersey", id: "nj2", hideRadio: true, children: [ { label: "New Jersey", value: "New Jersey", id: "nj3", hideRadio: true, children: [ { label: "Marketing & Sales NJ", value: "Marketing & Sales NJ", id: "marketingNJ1", }, { label: "Installation Office NJ", value: "Installation Office NJ", id: "installationNJ1", }, { label: "Warehouse NJ", value: "Warehouse NJ", id: "warehouseNJ1", }, ], }, { label: "Princeton", value: "Princeton", id: "princeton1", hideRadio: true, children: [ { label: "Marketing & Sales Princeton", value: "Marketing & Sales Princeton", id: "marketingPR1", }, { label: "Installation Office Princeton", value: "Installation Office Princeton", id: "installationPR1", }, { label: "Warehouse Princeton", value: "Warehouse Princeton", id: "warehousePR1", }, ] }, ] }, { label: "Maryland", value: "Maryland", id: "MD1", hideRadio: true, children: [ { label: "Marketing & Sales MD", value: "Marketing & Sales MD", id: "marketingMD1", }, { label: "Installation Office MD", value: "Installation Office MD", id: "installationMD1", }, { label: "Warehouse MD", value: "Warehouse MD", id: "warehouseMD1", }, ] }, { label: "Connecticut", value: "Connecticut", id: "CT1", hideRadio: true, children: [ { label: "Marketing & Sales CT", value: "Marketing & Sales CT", id: "marketingCT1", }, { label: "Installation Office CT", value: "Installation Office CT", id: "installationCT1", }, { label: "Warehouse CT", value: "Warehouse CT", id: "warehouseCT1", }, ] }, ]; const MultiLevelSelectSingleChildrenOnly = (props) => { return ( <div> <MultiLevelSelect id="multiselect-single-children-only" inputName="PowerChildren" onSelect={(selectedNode) => console.log("Selected Node", selectedNode)} treeData={treeData} variant="single" /> </div> ) }; export default MultiLevelSelectSingleChildrenOnly;
The returnAllSelected
or return_all_selected
prop can be used when users want data on all checked nodes from the dropdown, irrespective of whether it is a parent or child node.
NOTE: This variant also does not automatically uncheck the parent when any of the child nodes are unchecked. returnAllSelected
is set to false by default.
NOTE: For larger trees that may return many pill selections, you can optionally set input_display: "none"
(for Rails) or inputDisplay = "none"
(for React) to hide all pills within the input.
import React from "react"; import MultiLevelSelect from 'playbook-ui' const treeData = [ { label: "Power Home Remodeling", value: "Power Home Remodeling", id: "powerhome1", expanded: true, children: [ { label: "People", value: "People", id: "people1", children: [ { label: "Talent Acquisition", value: "Talent Acquisition", id: "talent1", }, { label: "Business Affairs", value: "Business Affairs", id: "business1", children: [ { label: "Initiatives", value: "Initiatives", id: "initiative1", }, { label: "Learning & Development", value: "Learning & Development", id: "development1", }, ], }, { label: "People Experience", value: "People Experience", id: "experience1", }, ], }, { label: "Contact Center", value: "Contact Center", id: "contact1", children: [ { label: "Appointment Management", value: "Appointment Management", id: "appointment1", }, { label: "Customer Service", value: "Customer Service", id: "customer1", }, { label: "Energy", value: "Energy", id: "energy1", }, ], }, ], }, ]; const MultiLevelSelectReturnAllSelected = (props) => { return ( <div> <MultiLevelSelect id="multi-level-select-return-all-selected" onSelect={(selectedNodes) => console.log("Selected Items with Return All Selected Data", selectedNodes) } returnAllSelected treeData={treeData} /> </div> ); }; export default MultiLevelSelectReturnAllSelected;
selected_ids
is an optional prop that accepts an array of ids and controls the selected state of the tree nodes that match the values passed. When used within react-hook-form, this prop can be used to manage the selected state of any ids passed to it.
Items that include checked:true
on the treeData itself will also be selected on page load, even without being passed to selectedIds
.
When an item is marked as checked on page load by any means, the dropdown will expand to show the checked items for easier accessibility.
import React from "react"; import MultiLevelSelect from 'playbook-ui' const treeData = [ { label: "Power Home Remodeling", value: "Power Home Remodeling", id: "powerhome1", expanded: true, children: [ { label: "People", value: "People", id: "people1", children: [ { label: "Talent Acquisition", value: "Talent Acquisition", id: "talent1", }, { label: "Business Affairs", value: "Business Affairs", id: "business1", children: [ { label: "Initiatives", value: "Initiatives", id: "initiative1", }, { label: "Learning & Development", value: "Learning & Development", id: "development1", }, ], }, { label: "People Experience", value: "People Experience", id: "experience1", }, ], }, { label: "Contact Center", value: "Contact Center", id: "contact1", children: [ { label: "Appointment Management", value: "Appointment Management", id: "appointment1", }, { label: "Customer Service", value: "Customer Service", id: "customer1", }, { label: "Energy", value: "Energy", id: "energy1", }, ], }, ], }, ]; const MultiLevelSelectSelectedIdsReact = (props) => { return ( <div> <MultiLevelSelect id="multi-level-select-selected_ids" onSelect={(selectedNodes) => console.log("Selected Items with Return All Selected Data", selectedNodes) } returnAllSelected selectedIds={["energy1", "talent1"]} treeData={treeData} /> </div> ); }; export default MultiLevelSelectSelectedIdsReact;
Change the form pill color by passing the optional pillColor
prop. Product, Data, and Status colors are available options. Check them out here in the Form Pill colors example.
import React from "react"; import MultiLevelSelect from 'playbook-ui' const treeData = [ { label: "Power Home Remodeling", value: "Power Home Remodeling", id: "powerhome1", expanded: true, children: [ { label: "People", value: "People", id: "people1", expanded: true, children: [ { label: "Talent Acquisition", value: "Talent Acquisition", id: "talent1", }, { label: "Business Affairs", value: "Business Affairs", id: "business1", children: [ { label: "Initiatives", value: "Initiatives", id: "initiative1", }, { label: "Learning & Development", value: "Learning & Development", id: "development1", }, ], }, { label: "People Experience", value: "People Experience", id: "experience1", }, ], }, { label: "Contact Center", value: "Contact Center", id: "contact1", children: [ { label: "Appointment Management", value: "Appointment Management", id: "appointment1", }, { label: "Customer Service", value: "Customer Service", id: "customer1", }, { label: "Energy", value: "Energy", id: "energy1", }, ], }, ], }, ]; const MultiLevelSelectColor = (props) => { return ( <div> <MultiLevelSelect id='multiselect-color' onSelect={(selectedNodes) => console.log( "Selected Items", selectedNodes ) } pillColor="neutral" treeData={treeData} /> </div> ) }; export default MultiLevelSelectColor;
The MultiLevelSelect also provides a subcomponent structure which can be used to render children to the right of the Checkboxes and their labels. As seen in the code snippet below, these children have access to the current item being iterated over which can be used for conditional rendering.
import React from "react"; import MultiLevelSelect from 'playbook-ui' import Badge from 'playbook-ui' const treeData = [ { label: "Power Home Remodeling", value: "Power Home Remodeling", id: "powerhome1", expanded: true, children: [ { label: "People", value: "People", id: "people1", expanded: true, status: "active", children: [ { label: "Talent Acquisition", value: "Talent Acquisition", id: "talent1", }, { label: "Business Affairs", value: "Business Affairs", id: "business1", status: "active", variant: "primary", children: [ { label: "Initiatives", value: "Initiatives", id: "initiative1", }, { label: "Learning & Development", value: "Learning & Development", id: "development1", status: "Inactive", }, ], }, { label: "People Experience", value: "People Experience", id: "experience1", }, ], }, { label: "Contact Center", value: "Contact Center", id: "contact1", status: "Inactive", variant: "error", children: [ { label: "Appointment Management", value: "Appointment Management", id: "appointment1", }, { label: "Customer Service", value: "Customer Service", id: "customer1", }, { label: "Energy", value: "Energy", id: "energy1", }, ], }, ], }, ]; const MultiLevelSelectWithChildren = (props) => { return ( <div> <MultiLevelSelect id="multiselect-with-children" onSelect={(selectedNodes) => console.log("Selected Items", selectedNodes) } treeData={treeData} > <MultiLevelSelect.Options> {(item) => ( <Badge marginLeft="sm" text={item.status} variant={item.status === "active" ? "success" : "warning"} /> )} </MultiLevelSelect.Options> </MultiLevelSelect> </div> ); }; export default MultiLevelSelectWithChildren;
The MultiLevelSelect subcomponent structure is also available in the 'Single Select' variant. In this variant, the children will be rendered to the right of the Radios and their labels.
import React from "react"; import MultiLevelSelect from 'playbook-ui' import Badge from 'playbook-ui' const treeData = [ { label: "Power Home Remodeling", value: "Power Home Remodeling", id: "powerhome1", expanded: true, children: [ { label: "People", value: "People", id: "people1", expanded: true, status: "active", children: [ { label: "Talent Acquisition", value: "Talent Acquisition", id: "talent1", }, { label: "Business Affairs", value: "Business Affairs", id: "business1", status: "active", variant: "primary", children: [ { label: "Initiatives", value: "Initiatives", id: "initiative1", }, { label: "Learning & Development", value: "Learning & Development", id: "development1", status: "Inactive", }, ], }, { label: "People Experience", value: "People Experience", id: "experience1", }, ], }, { label: "Contact Center", value: "Contact Center", id: "contact1", status: "Inactive", variant: "error", children: [ { label: "Appointment Management", value: "Appointment Management", id: "appointment1", }, { label: "Customer Service", value: "Customer Service", id: "customer1", }, { label: "Energy", value: "Energy", id: "energy1", }, ], }, ], }, ]; const MultiLevelSelectWithChildrenWithRadios = (props) => { return ( <div> <MultiLevelSelect id="multiselect-with-children" onSelect={(selectedNodes) => console.log("Selected Items", selectedNodes) } treeData={treeData} variant="single" > <MultiLevelSelect.Options> {(item) => ( <Badge marginLeft="sm" text={item.status} variant={item.status === "active" ? "success" : "warning"} /> )} </MultiLevelSelect.Options> </MultiLevelSelect> </div> ); }; export default MultiLevelSelectWithChildrenWithRadios;