@@ -10,7 +10,6 @@ import Field from '@/src/components/Common/Field/Field';
1010import { ALL_ATTACHMENTS } from '@/src/constants/dial-base-entity' ;
1111import { AttachmentsI18nKey } from '@/src/constants/i18n' ;
1212import { CONTROL_WITH_BUTTON_WIDTH , STANDARD_CONTROL_WIDTH } from '@/src/constants/main-layout' ;
13- import { useSaveValidationContext } from '@/src/context/SaveValidationContext' ;
1413import { useI18n } from '@/src/locales/client' ;
1514import Suggestions from './Suggestions' ;
1615
@@ -32,44 +31,42 @@ export interface Props {
3231 fieldTitle ?: string ;
3332 elementId ?: string ;
3433 optional ?: boolean ;
35- onChange ?: ( values : string [ ] ) => void ;
34+ onChange ?: ( values ? : string [ ] ) => void ;
3635}
3736
3837const ALL_ATTACHMENTS_VALUE = [ { label : ALL_ATTACHMENTS , value : ALL_ATTACHMENTS } ] ;
3938
4039const AttachmentInput : FC < Props > = ( {
4140 availableItems,
42- initialValues = [ ] ,
41+ initialValues,
4342 fieldTitle,
4443 placeholder,
4544 elementId,
4645 optional,
4746 onChange,
4847} ) => {
4948 const t = useI18n ( ) ;
50- const { resetCounter } = useSaveValidationContext ( ) ;
5149 const containerRef = useRef < HTMLDivElement > ( null ) ;
5250
53- const initialSelected = useMemo ( ( ) => {
51+ const selected = useMemo ( ( ) => {
52+ if ( ! initialValues ) return [ ] ;
5453 return initialValues
5554 . map ( ( val ) => availableItems . find ( ( o ) => o . value === val ) || { label : val , value : val } )
5655 . filter ( Boolean ) as AttachmentOption [ ] ;
5756 } , [ availableItems , initialValues ] ) ;
5857
59- const initialRadio = useMemo ( ( ) => {
60- return initialValues . length === 0
61- ? AttachmentType . NONE
62- : isEqual ( initialSelected , ALL_ATTACHMENTS_VALUE )
63- ? AttachmentType . ALL
64- : AttachmentType . SPECIFIC ;
65- } , [ initialSelected , initialValues . length ] ) ;
58+ const selectedRadio = useMemo ( ( ) => {
59+ if ( ! initialValues ) {
60+ return AttachmentType . NONE ;
61+ } else {
62+ return isEqual ( selected , ALL_ATTACHMENTS_VALUE ) ? AttachmentType . ALL : AttachmentType . SPECIFIC ;
63+ }
64+ } , [ initialValues , selected ] ) ;
6665
67- const [ selected , setSelected ] = useState < AttachmentOption [ ] > ( initialSelected ) ;
6866 const [ inputValue , setInputValue ] = useState ( '' ) ;
6967 const [ wraps , setWraps ] = useState ( false ) ;
7068 const [ showSuggestions , setShowSuggestions ] = useState ( false ) ;
7169 const [ highlight , setHighlight ] = useState ( 0 ) ;
72- const [ selectedRadio , setSelectedRadio ] = useState < AttachmentType > ( initialRadio ) ;
7370
7471 const filteredSuggestions = useMemo ( ( ) => {
7572 return availableItems
@@ -86,14 +83,6 @@ const AttachmentInput: FC<Props> = ({
8683 return selectedRadio === AttachmentType . SPECIFIC && showSuggestions && filteredSuggestions . length > 0 ;
8784 } , [ filteredSuggestions . length , selectedRadio , showSuggestions ] ) ;
8885
89- useEffect ( ( ) => {
90- if ( resetCounter ) {
91- setSelected ( initialSelected ) ;
92- setSelectedRadio ( initialRadio ) ;
93- }
94- // eslint-disable-next-line react-hooks/exhaustive-deps
95- } , [ resetCounter ] ) ;
96-
9786 useEffect ( ( ) => {
9887 const observer = new ResizeObserver ( ( ) => {
9988 if ( containerRef . current ) {
@@ -110,15 +99,18 @@ const AttachmentInput: FC<Props> = ({
11099 } , [ ] ) ;
111100
112101 const fireChange = useCallback (
113- ( items : AttachmentOption [ ] ) => {
114- onChange ?.( items . map ( ( i ) => i . value ) ) ;
102+ ( items ?: AttachmentOption [ ] ) => {
103+ if ( ! items ) {
104+ onChange ?.( void 0 ) ;
105+ return ;
106+ }
107+ onChange ?.( items ?. map ( ( i ) => i . value ) ) ;
115108 } ,
116109 [ onChange ] ,
117110 ) ;
118111
119112 const setValues = useCallback (
120- ( value : AttachmentOption [ ] ) => {
121- setSelected ( value ) ;
113+ ( value ?: AttachmentOption [ ] ) => {
122114 fireChange ( value ) ;
123115 setInputValue ( '' ) ;
124116 setShowSuggestions ( false ) ;
@@ -155,9 +147,9 @@ const AttachmentInput: FC<Props> = ({
155147
156148 const handleRadioChange = useCallback (
157149 ( option : AttachmentType ) => {
158- setSelectedRadio ( option ) ;
159-
160- if ( option === AttachmentType . NONE || option === AttachmentType . SPECIFIC ) {
150+ if ( option === AttachmentType . NONE ) {
151+ setValues ( void 0 ) ;
152+ } else if ( option === AttachmentType . SPECIFIC ) {
161153 setValues ( [ ] ) ;
162154 } else if ( option === AttachmentType . ALL ) {
163155 setValues ( ALL_ATTACHMENTS_VALUE ) ;
0 commit comments