Skip to content

Commit

Permalink
feat(AdvancedSearch): add isOpen control possibility
Browse files Browse the repository at this point in the history
  • Loading branch information
GaroGabrielyan committed Feb 9, 2024
1 parent b54a6e1 commit abcebde
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/lib/molecules/AdvancedSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './index.scss';

function AdvancedSearch({
data,
isOpen,
position,
onSearch,
totalCount,
Expand All @@ -43,10 +44,14 @@ function AdvancedSearch({
const searchRef = useRef(null);
const [popoverOpen, setPopoverOpen] = useState(false);
const [initialDataState, setInitialDataState] = useState([]);

const [searchValue, setSearchValue] = useState(null);
const [isOpenControlled, setIsOpenControlled] = useState(false);
const debouncedSearchValue = useDebounce(searchValue, 300);

useEffect(() => {
setIsOpenControlled(isOpen !== undefined);
}, [isOpen]);

useEffect(() => {
if (debouncedSearchValue === null) return;
onSearch(debouncedSearchValue);
Expand Down Expand Up @@ -82,9 +87,11 @@ function AdvancedSearch({

const inputAndPopoverWidthVariable = useMemo(
() => ({
'--advanced-search-width': popoverOpen ? `${openedInputWidth}vw` : closedInputWidth
'--advanced-search-width': (isOpenControlled ? isOpen : popoverOpen)
? `${openedInputWidth}vw`
: closedInputWidth
}),
[popoverOpen]
[popoverOpen, isOpenControlled, isOpen]
);

const handleOutsideClick = useClickOutside(() => onOutsideClick());
Expand All @@ -106,7 +113,7 @@ function AdvancedSearch({
<PopoverV2
position="bottom"
screenType="desktop"
isOpen={popoverOpen}
isOpen={isOpenControlled ? isOpen : popoverOpen}
scrollbarNeeded={false}
extendTargetWidth={false}
className="advancedSearch__popover"
Expand Down Expand Up @@ -325,7 +332,11 @@ AdvancedSearch.propTypes = {
/**
* text for no data to display
*/
noDataText: PropTypes.string
noDataText: PropTypes.string,
/**
* A boolean prop to control the popover's open and close states from outside the component.
*/
isOpen: PropTypes.bool
};

AdvancedSearch.defaultProps = {
Expand Down
5 changes: 5 additions & 0 deletions stories/molecules/AdvancedSearch/AdvancedSearch.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default {
onShowMoreClick: args({ control: false, category: category.action }),
totalCountText: args({ control: 'text', category: category.content }),
showMoreIsLoading: args({ control: false, category: category.states }),
isOpen: args({ control: 'boolean', category: category.functionality }),
primaryFilterData: args({ control: 'object', category: category.content }),
closedInputWidth: args({ control: 'number', category: category.appearance }),
openedInputWidth: args({ control: 'number', category: category.appearance }),
Expand Down Expand Up @@ -84,6 +85,7 @@ export const AdvancedSearch = ({ ...args }) => {
const [byUserFiltered, setByUserFiltered] = useState([]);
const [initialData, setInitialData] = useState([]);
const [searchCharacter, setSearchCharacter] = useState('');
const [isOpen, setIsOpen] = useState(false);

setTimeout(() => setLoading(false), 3000);

Expand Down Expand Up @@ -289,6 +291,9 @@ export const AdvancedSearch = ({ ...args }) => {
{...args}
onShowMoreClick={showMoreHandler}
onSearch={onSearchHandler}
isOpen={isOpen}
extendedInputConfigs={{ onFocus: () => setIsOpen(true) }}
onOutsideClick={() => setIsOpen(false)}
/>
<Divider size={32} />
<Time style={{ marginLeft: '10px' }} />
Expand Down

0 comments on commit abcebde

Please sign in to comment.