-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
icon picker modal #2908
Open
youssefezzahi96
wants to merge
9
commits into
master
Choose a base branch
from
CLXP-200-icon-picker-modal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+443
−4
Open
icon picker modal #2908
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3666276
create select tile component
youssefezzahi96 f706812
feedbacks
youssefezzahi96 a567ead
feedbacks 2
youssefezzahi96 0446cfb
add isSelected option pro
youssefezzahi96 94375c3
icon picker modal
youssefezzahi96 2538c3a
rebase on select icon
youssefezzahi96 0552835
add some UT for better coverage
youssefezzahi96 db0a073
fix build
youssefezzahi96 32099bd
fix coverage
youssefezzahi96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
packages/@coorpacademy-components/src/molecule/icon-picker-modal/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import React, {useMemo, useState, useCallback, useEffect} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {fas} from '@fortawesome/pro-solid-svg-icons'; | ||
import {debounce, map, pipe, get, values, slice, filter} from 'lodash/fp'; | ||
import BaseModal from '../base-modal'; | ||
import SelectIcon from '../../atom/select-icon'; | ||
import Provider from '../../atom/provider'; | ||
import SearchForm from '../search-form'; | ||
import {COLORS} from '../../variables/colors'; | ||
import style from './style.css'; | ||
|
||
export const filterIcons = (query, allIcons) => { | ||
return query | ||
? filter(iconName => iconName.toLowerCase().includes(query.toLowerCase()), allIcons) | ||
: allIcons; | ||
}; | ||
|
||
const ICONS_PER_LOAD = 48; | ||
|
||
const IconPickerModal = (props, context) => { | ||
const {isOpen, onCancel, onConfirm, onClose} = props; | ||
const {translate} = context; | ||
|
||
const [selectedIcon, setSelectedIcon] = useState(null); | ||
const [searchValue, setSearchValue] = useState(''); | ||
const [displayedIcons, setDisplayedIcons] = useState([]); | ||
const [currentIndex, setCurrentIndex] = useState(0); | ||
|
||
const allIcons = useMemo(() => pipe(values, map(get('iconName')))(fas), []); | ||
const [searchResults, setSearchResults] = useState(allIcons); | ||
|
||
const handleCancel = useCallback(() => { | ||
onCancel(); | ||
}, [onCancel]); | ||
|
||
const handleClose = useCallback(() => { | ||
onClose(); | ||
}, [onClose]); | ||
|
||
const handleIconClick = useCallback( | ||
index => () => { | ||
setSelectedIcon(prevSelectedIcon => (prevSelectedIcon === index ? null : index)); | ||
}, | ||
[] | ||
); | ||
|
||
const loadMoreIcons = useCallback(() => { | ||
const nextIndex = currentIndex + ICONS_PER_LOAD; | ||
const newIcons = slice(currentIndex, nextIndex, searchResults); | ||
setDisplayedIcons(prevIcons => [...prevIcons, ...newIcons]); | ||
setCurrentIndex(nextIndex); | ||
}, [currentIndex, searchResults]); | ||
|
||
useEffect(() => { | ||
setDisplayedIcons(() => slice(0, ICONS_PER_LOAD, searchResults)); | ||
setCurrentIndex(ICONS_PER_LOAD); | ||
}, [searchResults]); | ||
|
||
const handleScroll = useCallback( | ||
event => { | ||
const {scrollTop, clientHeight, scrollHeight} = event.currentTarget; | ||
if (scrollHeight - scrollTop <= clientHeight + 1) { | ||
loadMoreIcons(); | ||
} | ||
}, | ||
[loadMoreIcons] | ||
); | ||
|
||
const updateSearchResults = useCallback( | ||
query => { | ||
const results = filterIcons(query, allIcons); | ||
setSearchResults(results); | ||
}, | ||
[allIcons] | ||
); | ||
|
||
const debouncedSearch = useMemo(() => debounce(300, updateSearchResults), [updateSearchResults]); | ||
|
||
const handleSearch = useCallback( | ||
value => { | ||
setSearchValue(value); | ||
debouncedSearch(value); | ||
}, | ||
[debouncedSearch] | ||
); | ||
|
||
const handleReset = useCallback(() => { | ||
setSearchValue(''); | ||
updateSearchResults(''); | ||
}, [updateSearchResults]); | ||
|
||
const icons = useMemo( | ||
() => | ||
displayedIcons.map((iconName, index) => ( | ||
<SelectIcon | ||
key={`icon-${index}`} | ||
size="responsive" | ||
data-name={`icon-${index}`} | ||
aria-label={`aria icon ${index}`} | ||
faIcon={iconName} | ||
onClick={handleIconClick(index)} | ||
options={{isSelected: selectedIcon === index}} | ||
/> | ||
)), | ||
[displayedIcons, selectedIcon, handleIconClick] | ||
); | ||
|
||
const footer = useMemo(() => { | ||
return { | ||
cancelButton: { | ||
onCancel: handleCancel, | ||
label: translate('cancel') | ||
}, | ||
confirmButton: { | ||
onConfirm: () => { | ||
onConfirm(selectedIcon); | ||
setSelectedIcon(null); | ||
onClose(); | ||
}, | ||
label: translate('confirm'), | ||
iconName: 'plus', | ||
disabled: selectedIcon === null, | ||
color: COLORS.cm_primary_blue | ||
} | ||
}; | ||
}, [handleCancel, onConfirm, onClose, translate, selectedIcon]); | ||
|
||
if (!isOpen) return null; | ||
|
||
return ( | ||
<BaseModal | ||
title={translate('icon_picker_title')} | ||
description={translate('icon_picker_description')} | ||
isOpen={isOpen} | ||
onClose={handleClose} | ||
onScroll={handleScroll} | ||
footer={footer} | ||
headerIcon={{ | ||
name: 'arrows-rotate', | ||
backgroundColor: '#D6E6FF' | ||
}} | ||
> | ||
<div className={style.iconPicker}> | ||
{ | ||
<> | ||
<div className={style.searchWrapper}> | ||
<SearchForm | ||
search={{ | ||
placeholder: translate('search_place_holder'), | ||
value: searchValue, | ||
onChange: handleSearch | ||
}} | ||
onReset={handleReset} | ||
dataTestId="search-input" | ||
/> | ||
</div> | ||
{searchValue && searchResults.length === 0 ? ( | ||
<div className={style.emptySearchResultContainer}> | ||
<div className={style.emptySearchResultTitle}> | ||
{translate('empty_search_result_title', {searchValue})} | ||
</div> | ||
<div className={style.emptySearchResultDescription}> | ||
{translate('empty_search_result_description')} | ||
</div> | ||
<div className={style.emptySearchResultClearSearch} onClick={handleReset}> | ||
{translate('empty_search_result_clear_search')} | ||
</div> | ||
</div> | ||
) : ( | ||
<div className={style.iconsListWrapper}>{icons}</div> | ||
)} | ||
</> | ||
} | ||
</div> | ||
</BaseModal> | ||
); | ||
}; | ||
|
||
IconPickerModal.contextTypes = { | ||
translate: Provider.childContextTypes.translate | ||
}; | ||
|
||
IconPickerModal.propTypes = { | ||
isOpen: PropTypes.bool, | ||
onCancel: PropTypes.func, | ||
onConfirm: PropTypes.func, | ||
onClose: PropTypes.func | ||
}; | ||
|
||
export default IconPickerModal; |
61 changes: 61 additions & 0 deletions
61
packages/@coorpacademy-components/src/molecule/icon-picker-modal/style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
@value colors: "../../variables/colors.css"; | ||
@value cm_grey_100 from colors; | ||
@value cm_primary_blue from colors; | ||
@value cm_grey_500 from colors; | ||
|
||
.iconPicker { | ||
height: 485px; | ||
width: calc(71vw - 68px); | ||
max-width: 612px; | ||
} | ||
|
||
.searchWrapper { | ||
border-radius: 12px; | ||
width: 300px; | ||
background-color: cm_grey_100; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.iconsListWrapper { | ||
display: flex; | ||
flex-wrap: wrap; | ||
justify-content: space-around; | ||
margin: auto; | ||
gap: 12px; | ||
overflow: visible; | ||
} | ||
|
||
.emptySearchResultContainer { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
|
||
.emptySearchResultTitle { | ||
font-size: 20px; | ||
font-weight: 700; | ||
line-height: 28px; | ||
margin-bottom: 8px; | ||
} | ||
|
||
.emptySearchResultDescription { | ||
font-size: 16px; | ||
font-weight: 500; | ||
line-height: 22px; | ||
margin-bottom: 16px; | ||
} | ||
|
||
.emptySearchResultClearSearch { | ||
color: #0061FF; | ||
font-size: 14px; | ||
font-style: normal; | ||
font-weight: 600; | ||
line-height: 20px; | ||
cursor: pointer; | ||
} | ||
|
||
@media mobile { | ||
.iconPicker { | ||
width: 100%; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/@coorpacademy-components/src/molecule/icon-picker-modal/test/fixtures/default.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
props: { | ||
isOpen: true, | ||
onCancel: () => {}, | ||
onConfirm: () => {}, | ||
onClose: () => {} | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tu pourrai sortir tout ce qui concerne la search ds un custom hooks. pour separer la logic de search de ton composant UI. car cest un peu mélangé la.
search + deboucing + search Result genre
useIconSearch