From 86b841a15d3e0cb3b7f6921ee321099f64ee1992 Mon Sep 17 00:00:00 2001 From: Carlos Lima Date: Thu, 17 Oct 2024 09:10:16 -0300 Subject: [PATCH] [PBNTR-568] Adding React Dropdown cleaning feature (#3793) **What does this PR do?** Adding React Dropdown cleaning feature **Screenshots:** image **How to test?** 1. Go to the Dropdown kit page 2. Scroll down to the Clear Selection doc #### Checklist: - [x] **LABELS** Add a label: `enhancement`, `bug`, `improvement`, `new kit`, `deprecated`, or `breaking`. See [Changelog & Labels](https://github.com/powerhome/playbook/wiki/Changelog-&-Labels) for details. - [x] **DEPLOY** I have added the `milano` label to show I'm ready for a review. - [ ] **TESTS** I have added test coverage to my code. --- .../playbook/pb_dropdown/_dropdown.tsx | 24 ++++++++-- .../docs/_dropdown_clear_selection.jsx | 45 +++++++++++++++++++ .../docs/_dropdown_clear_selection.md | 1 + .../playbook/pb_dropdown/docs/example.yml | 1 + .../playbook/pb_dropdown/docs/index.js | 1 + .../subcomponents/DropdownOption.tsx | 2 +- .../subcomponents/DropdownTrigger.tsx | 4 +- 7 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 playbook/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_clear_selection.jsx create mode 100644 playbook/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_clear_selection.md diff --git a/playbook/app/pb_kits/playbook/pb_dropdown/_dropdown.tsx b/playbook/app/pb_kits/playbook/pb_dropdown/_dropdown.tsx index 5940e22e29..27234c3fec 100644 --- a/playbook/app/pb_kits/playbook/pb_dropdown/_dropdown.tsx +++ b/playbook/app/pb_kits/playbook/pb_dropdown/_dropdown.tsx @@ -1,4 +1,4 @@ -import React, { useState, useRef, useEffect } from "react"; +import React, { useState, useRef, useEffect, forwardRef, useImperativeHandle } from "react"; import classnames from "classnames"; import { buildAriaProps, buildCss, buildDataProps, buildHtmlProps } from "../utilities/props"; import { globalProps } from "../utilities/globalProps"; @@ -38,7 +38,14 @@ type DropdownProps = { triggerRef?: any; }; -const Dropdown = (props: DropdownProps) => { +interface DropdownComponent + extends React.ForwardRefExoticComponent> { + Option: typeof DropdownOption; + Trigger: typeof DropdownTrigger; + Container: typeof DropdownContainer; +} + +const Dropdown = forwardRef((props: DropdownProps, ref: any) => { const { aria = {}, autocomplete = false, @@ -125,7 +132,7 @@ const Dropdown = (props: DropdownProps) => { const filteredOptions = optionsWithBlankSelection?.filter((option: GenericObject) => { const label = typeof option.label === 'string' ? option.label.toLowerCase() : option.label; return String(label).toLowerCase().includes(filterItem.toLowerCase()); - }); + }); // For keyboard accessibility: Set focus within dropdown to selected item if it exists useEffect(() => { @@ -175,6 +182,14 @@ const Dropdown = (props: DropdownProps) => { dark }); + useImperativeHandle(ref, () => ({ + clearSelected: () => { + setSelected({}); + setFilterItem(""); + setIsDropDownClosed(true); + onSelect && onSelect(null); + }, + })); return (
{
) -}; +}) as DropdownComponent +Dropdown.displayName = "Dropdown"; Dropdown.Option = DropdownOption; Dropdown.Trigger = DropdownTrigger; Dropdown.Container = DropdownContainer; diff --git a/playbook/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_clear_selection.jsx b/playbook/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_clear_selection.jsx new file mode 100644 index 0000000000..eeece1ec52 --- /dev/null +++ b/playbook/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_clear_selection.jsx @@ -0,0 +1,45 @@ +import React, { useRef } from 'react' +import { Button, Dropdown } from 'playbook-ui' + +const options = [ + { + label: "United States", + value: "United States", + }, + { + label: "Canada", + value: "Canada", + }, + { + label: "Pakistan", + value: "Pakistan", + } +] + +const DropdownClearSelection = (props) => { + const dropdownRef = useRef(null) + + const handleReset = () => { + if (dropdownRef.current) { + dropdownRef.current.clearSelected() + } + } + + return ( + <> + +