1
- import React , { useMemo } from 'react' ;
1
+ import React , { useMemo , useState } from 'react' ;
2
2
import _ from 'underscore' ;
3
3
import lodashGet from 'lodash/get' ;
4
4
import { withOnyx } from 'react-native-onyx' ;
5
+ import CONST from '../../CONST' ;
5
6
import ONYXKEYS from '../../ONYXKEYS' ;
6
7
import styles from '../../styles/styles' ;
7
- import Navigation from '../../libs/Navigation/Navigation' ;
8
- import ROUTES from '../../ROUTES' ;
9
8
import useLocalize from '../../hooks/useLocalize' ;
10
9
import * as OptionsListUtils from '../../libs/OptionsListUtils' ;
11
10
import OptionsSelector from '../OptionsSelector' ;
12
11
import { propTypes , defaultProps } from './tagPickerPropTypes' ;
13
12
14
- function TagPicker ( { policyTags , reportID , tag , iouType , iou } ) {
13
+ function TagPicker ( { selectedTag , tag , policyTags , policyRecentlyUsedTags , onSubmit } ) {
15
14
const { translate} = useLocalize ( ) ;
15
+ const [ searchValue , setSearchValue ] = useState ( '' ) ;
16
+
17
+ const policyRecentlyUsedTagsList = lodashGet ( policyRecentlyUsedTags , tag , [ ] ) ;
18
+ const policyTagList = lodashGet ( policyTags , [ tag , 'tags' ] , { } ) ;
19
+ const policyTagsCount = _ . size ( _ . filter ( policyTagList , ( policyTag ) => policyTag . enabled ) ) ;
20
+ const isTagsCountBelowThreshold = policyTagsCount < CONST . TAG_LIST_THRESHOLD ;
21
+
22
+ const shouldShowTextInput = ! isTagsCountBelowThreshold ;
16
23
17
24
const selectedOptions = useMemo ( ( ) => {
18
- if ( ! iou . tag ) {
25
+ if ( ! selectedTag ) {
19
26
return [ ] ;
20
27
}
21
28
22
29
return [
23
30
{
24
- name : iou . tag ,
31
+ name : selectedTag ,
25
32
enabled : true ,
33
+ accountID : null ,
26
34
} ,
27
35
] ;
28
- } , [ iou . tag ] ) ;
29
-
30
- // Only shows one section, which will be the default behavior if there are
31
- // less than 8 policy tags
32
- // TODO: support sections with search
33
- const sections = useMemo ( ( ) => {
34
- const tagList = _ . chain ( lodashGet ( policyTags , [ tag , 'tags' ] , { } ) )
35
- . values ( )
36
- . map ( ( t ) => ( {
37
- text : t . name ,
38
- keyForList : t . name ,
39
- tooltipText : t . name ,
40
- } ) )
41
- . value ( ) ;
36
+ } , [ selectedTag ] ) ;
42
37
43
- return [
44
- {
45
- data : tagList ,
46
- } ,
47
- ] ;
48
- } , [ policyTags , tag ] ) ;
38
+ const initialFocusedIndex = useMemo ( ( ) => {
39
+ if ( isTagsCountBelowThreshold && selectedOptions . length > 0 ) {
40
+ return _ . chain ( policyTagList )
41
+ . values ( )
42
+ . findIndex ( ( policyTag ) => policyTag . name === selectedOptions [ 0 ] . name , true )
43
+ . value ( ) ;
44
+ }
49
45
50
- const headerMessage = OptionsListUtils . getHeaderMessage ( lodashGet ( sections , '[0].data.length' , 0 ) > 0 , false , '' ) ;
46
+ return 0 ;
47
+ } , [ policyTagList , selectedOptions , isTagsCountBelowThreshold ] ) ;
51
48
52
- const navigateBack = ( ) => {
53
- Navigation . goBack ( ROUTES . getMoneyRequestConfirmationRoute ( iouType , reportID ) ) ;
54
- } ;
49
+ const sections = useMemo (
50
+ ( ) =>
51
+ OptionsListUtils . getFilteredOptions ( { } , { } , [ ] , searchValue , selectedOptions , [ ] , false , false , false , { } , [ ] , true , policyTagList , policyRecentlyUsedTagsList , false ) . tagOptions ,
52
+ [ searchValue , selectedOptions , policyTagList , policyRecentlyUsedTagsList ] ,
53
+ ) ;
55
54
56
- const updateTag = ( ) => {
57
- // TODO: add logic to save the selected tag
58
- navigateBack ( ) ;
59
- } ;
55
+ const headerMessage = OptionsListUtils . getHeaderMessage ( lodashGet ( sections , '[0].data.length' , 0 ) > 0 , false , '' ) ;
60
56
61
57
return (
62
58
< OptionsSelector
@@ -66,9 +62,13 @@ function TagPicker({policyTags, reportID, tag, iouType, iou}) {
66
62
headerMessage = { headerMessage }
67
63
textInputLabel = { translate ( 'common.search' ) }
68
64
boldStyle
69
- value = ""
70
- onSelectRow = { updateTag }
71
- shouldShowTextInput = { false }
65
+ highlightSelectedOptions
66
+ isRowMultilineSupported
67
+ shouldShowTextInput = { shouldShowTextInput }
68
+ value = { searchValue }
69
+ initialFocusedIndex = { initialFocusedIndex }
70
+ onChangeText = { setSearchValue }
71
+ onSelectRow = { onSubmit }
72
72
/>
73
73
) ;
74
74
}
@@ -84,7 +84,4 @@ export default withOnyx({
84
84
policyRecentlyUsedTags : {
85
85
key : ( { policyID} ) => `${ ONYXKEYS . COLLECTION . POLICY_RECENTLY_USED_TAGS } ${ policyID } ` ,
86
86
} ,
87
- iou : {
88
- key : ONYXKEYS . IOU ,
89
- } ,
90
87
} ) ( TagPicker ) ;
0 commit comments