1
1
import { SyntheticEvent , useCallback , useMemo } from 'react'
2
+ import classNames from 'classnames'
2
3
import { DropdownProps , Field , InputOnChangeData , SelectField , TextAreaField , TextAreaProps } from 'decentraland-ui'
3
4
import { MappingType , MultipleMapping } from '@dcl/schemas'
4
5
import { t } from 'decentraland-dapps/dist/modules/translation'
@@ -17,7 +18,7 @@ const mappingTypeIcons = {
17
18
}
18
19
19
20
export const MappingEditor = ( props : Props ) => {
20
- const { mapping, error, disabled, onChange } = props
21
+ const { mapping, error, loading , disabled, isCompact , onChange } = props
21
22
22
23
const [ mappingType , mappingValue ] = useMemo ( ( ) => {
23
24
switch ( mapping . type ) {
@@ -64,11 +65,11 @@ export const MappingEditor = (props: Props) => {
64
65
[ onChange ]
65
66
)
66
67
67
- const handleSingleMappingValueChange = useCallback ( ( _ : React . ChangeEvent < HTMLInputElement > , data : InputOnChangeData ) => {
68
- onChange ( { type : MappingType . SINGLE , id : data . value } )
68
+ const handleSingleMappingValueChange = useCallback ( ( _ , data : InputOnChangeData ) => {
69
+ onChange ( { type : MappingType . SINGLE , id : data . value . replaceAll ( ',' , '' ) } )
69
70
} , [ ] )
70
71
71
- const handleMultipleMappingValueChange = useCallback ( ( _ : React . ChangeEvent < HTMLTextAreaElement > , data : TextAreaProps ) => {
72
+ const handleMultipleMappingValueChange = useCallback ( ( _ , data : TextAreaProps | InputOnChangeData ) => {
72
73
const ids =
73
74
data . value
74
75
?. toString ( )
@@ -97,64 +98,94 @@ export const MappingEditor = (props: Props) => {
97
98
)
98
99
99
100
return (
100
- < div className = { styles . main } >
101
+ < div className = { classNames ( styles . main , isCompact ? styles . compact : styles . full ) } >
101
102
< SelectField
102
- label = { t ( 'mapping_editor.mapping_type_label' ) }
103
+ label = { isCompact ? undefined : t ( 'mapping_editor.mapping_type_label' ) }
103
104
onChange = { handleMappingTypeChange }
104
105
value = { mappingType }
105
- className = { styles . mappingType }
106
+ disabled = { disabled }
107
+ className = { classNames ( styles . mappingType , isCompact ? styles . compact : styles . full ) }
106
108
options = { mappingTypeOptions }
107
- /> { ' ' }
108
- < div className = { styles . mappings } >
109
+ />
110
+ < div className = { classNames ( styles . mappings , isCompact ? styles . compact : styles . full ) } >
109
111
{ mappingType === MappingType . ANY ? (
110
- < Field label = { t ( 'mapping_editor.mapping_value_label' ) } disabled value = { t ( 'mapping_editor.mapping_value_any' ) } />
112
+ < Field
113
+ label = { isCompact ? undefined : t ( 'mapping_editor.mapping_value_label' ) }
114
+ loading = { loading }
115
+ disabled
116
+ className = { styles . any }
117
+ value = { t ( 'mapping_editor.mapping_value_any' ) }
118
+ />
111
119
) : mappingType === MappingType . SINGLE ? (
112
120
< Field
113
- label = { t ( 'mapping_editor.mapping_value_label' ) }
121
+ label = { isCompact ? undefined : t ( 'mapping_editor.mapping_value_label' ) }
114
122
disabled = { disabled }
115
- type = { mappingType === MappingType . SINGLE ? ' number' : 'text' }
123
+ type = " number"
116
124
value = { mappingValue }
125
+ loading = { loading }
117
126
error = { ! ! error }
118
127
message = { error }
119
128
placeholder = { '1234567890' }
120
129
maxLength = { 78 }
121
130
onChange = { handleSingleMappingValueChange }
122
131
/>
123
132
) : mappingType === MappingType . MULTIPLE ? (
124
- < TextAreaField
125
- label = { t ( 'mapping_editor.mapping_value_label' ) }
126
- disabled = { disabled }
127
- info = {
128
- mappingValue . length === 0 && ! error
129
- ? t ( 'mapping_editor.mapping_value_multiple_info' )
130
- : t ( 'mapping_editor.mapping_value_multiple_amount_info' , { count : ( mapping as MultipleMapping ) . ids . length } )
131
- }
132
- error = { error }
133
- placeholder = { '1, 2, 3, 4' }
134
- value = { mappingValue }
135
- onChange = { handleMultipleMappingValueChange }
136
- />
133
+ isCompact ? (
134
+ < Field
135
+ disabled = { disabled }
136
+ loading = { loading }
137
+ error = { ! ! error }
138
+ message = {
139
+ error ? error : t ( 'mapping_editor.mapping_value_multiple_amount_info' , { count : ( mapping as MultipleMapping ) . ids . length } )
140
+ }
141
+ placeholder = "1, 2, 3, 4"
142
+ value = { mappingValue }
143
+ className = { styles . multiple }
144
+ onChange = { handleMultipleMappingValueChange }
145
+ />
146
+ ) : (
147
+ < TextAreaField
148
+ label = { t ( 'mapping_editor.mapping_value_label' ) }
149
+ disabled = { disabled }
150
+ loading = { loading }
151
+ info = {
152
+ mappingValue . length === 0 && ! error
153
+ ? t ( 'mapping_editor.mapping_value_multiple_info' )
154
+ : t ( 'mapping_editor.mapping_value_multiple_amount_info' , { count : ( mapping as MultipleMapping ) . ids . length } )
155
+ }
156
+ className = { styles . multiple }
157
+ error = { error }
158
+ placeholder = "1, 2, 3, 4"
159
+ value = { mappingValue }
160
+ onChange = { handleMultipleMappingValueChange }
161
+ />
162
+ )
137
163
) : mappingType === MappingType . RANGE ? (
138
- < >
164
+ < div className = { classNames ( styles . range , isCompact ? styles . compact : styles . full ) } >
139
165
< Field
140
- label = { t ( 'mapping_editor.mapping_value_from_label' ) }
166
+ label = { isCompact ? undefined : t ( 'mapping_editor.mapping_value_from_label' ) }
167
+ error = { ! ! error }
168
+ message = { error }
141
169
disabled = { disabled }
142
170
type = "number"
143
171
placeholder = { '1' }
144
172
maxLength = { 78 }
145
173
value = { mappingValue . split ( ',' ) [ 0 ] }
146
174
onChange = { handleFromMappingValueChange }
147
175
/>
176
+ { isCompact ? < div className = { styles . to } > { t ( 'mapping_editor.to' ) } </ div > : null }
148
177
< Field
149
- label = { t ( 'mapping_editor.mapping_value_to_label' ) }
178
+ label = { isCompact ? undefined : t ( 'mapping_editor.mapping_value_to_label' ) }
150
179
disabled = { disabled }
180
+ error = { ! ! error }
181
+ message = { error }
151
182
type = "number"
152
183
placeholder = { '4000' }
153
184
maxLength = { 78 }
154
185
value = { mappingValue . split ( ',' ) [ 1 ] }
155
186
onChange = { handleToMappingValueChange }
156
187
/>
157
- </ >
188
+ </ div >
158
189
) : null }
159
190
</ div >
160
191
</ div >
0 commit comments