@@ -8,89 +8,96 @@ import Input from '../Input';
88import Modal from '../Modal' ;
99
1010type TableModalProps = {
11- checkedRowValue : TableSignatureValue | null ;
12- tableData : TableSignature ;
13- modalType : ModalType ;
14- onClose : ( ) => void ;
15- onSubmit : ( model : TableSignatureValue ) => void ;
11+ checkedRowValue : TableSignatureValue | null ;
12+ tableData : TableSignature ;
13+ modalType : ModalType ;
14+ onClose : ( ) => void ;
15+ onSubmit : ( model : TableSignatureValue ) => void ;
1616} ;
1717
1818const TableModal = ( {
19- checkedRowValue,
20- tableData,
21- modalType,
22- onClose,
23- onSubmit,
19+ checkedRowValue,
20+ tableData,
21+ modalType,
22+ onClose,
23+ onSubmit,
2424} : TableModalProps ) => {
25- const [ model , setModel ] = useState < TableSignatureValue > ( { } ) ;
26- const [ errors , setErrors ] = useState < TableSignatureValue > ( { } ) ;
25+ const [ model , setModel ] = useState < TableSignatureValue > ( { } ) ;
26+ const [ errors , setErrors ] = useState < TableSignatureValue > ( { } ) ;
2727
28- useEffect ( ( ) => {
29- setModel (
30- getInitialModel (
31- tableData . fields ,
32- modalType === 'update' ? checkedRowValue : null
33- )
34- ) ;
35- setErrors ( { } ) ;
36- } , [ modalType ] ) ;
28+ useEffect ( ( ) => {
29+ setModel (
30+ getInitialModel (
31+ tableData . fields ,
32+ modalType === 'update' ? checkedRowValue : null
33+ )
34+ ) ;
35+ setErrors ( { } ) ;
36+ } , [ modalType ] ) ;
3737
38- const getValidate = ( model : TableSignatureValue ) : TableSignatureValue => {
39- return tableData . fields . reduce ( ( acc , field ) => {
40- if ( isFieldRequired ( field . type ) ) {
41- Object . assign ( acc , {
42- [ field . name ] : ! ! model [ field . name ] ? '' : REQIRED_TEXT ,
43- } ) ;
44- }
38+ const getValidate = ( model : TableSignatureValue ) : TableSignatureValue => {
39+ return tableData . fields . reduce ( ( acc , field ) => {
40+ if ( isFieldRequired ( field . type ) ) {
41+ Object . assign ( acc , {
42+ [ field . name ] : ! ! model [ field . name ] ? '' : REQIRED_TEXT ,
43+ } ) ;
44+ }
4545
46- return acc ;
47- } , { } as TableSignatureValue ) ;
48- } ;
46+ return acc ;
47+ } , { } as TableSignatureValue ) ;
48+ } ;
4949
50- const onChange = ( field : string , value : string ) => {
51- const newModel = { ...model , [ field ] : value } ;
52- setModel ( newModel ) ;
50+ const onChange = ( field : string , value : string ) => {
51+ const newModel = { ...model , [ field ] : value } ;
52+ setModel ( newModel ) ;
5353
54- const _errors = getValidate ( newModel ) ;
55- setErrors ( {
56- ...errors ,
57- [ field ] : ! ! _errors [ field ] ? _errors [ field ] : '' ,
58- } ) ;
59- } ;
54+ const _errors = getValidate ( newModel ) ;
55+ setErrors ( {
56+ ...errors ,
57+ [ field ] : ! ! _errors [ field ] ? _errors [ field ] : '' ,
58+ } ) ;
59+ } ;
6060
61- const onSubmitHandler = ( ) => {
62- const _errors = getValidate ( model ) ;
63- const errorLength = Object . values ( _errors ) . filter ( ( a ) => ! ! a . length ) . length ;
64- setErrors ( _errors ) ;
61+ const onSubmitHandler = ( ) => {
62+ const _errors = getValidate ( model ) ;
63+ const errorLength = Object . values ( _errors ) . filter ( ( a ) => ! ! a . length ) . length ;
64+ setErrors ( _errors ) ;
6565
66- if ( ! ! errorLength ) return ;
66+ if ( ! ! errorLength ) return ;
6767
68- onSubmit ( model ) ;
69- } ;
68+ onSubmit ( model ) ;
69+ } ;
7070
71- return (
72- < Modal
73- visible = { ! ! modalType }
74- title = { `${ modalType === 'add' ? `Add new ${ tableData . name } record` : `Update ${ tableData . name } record` } ` }
75- onClose = { onClose }
76- onSubmit = { onSubmitHandler }
77- >
78- < View >
79- { tableData . fields . map ( ( field ) => (
80- < Input
81- title = { `${ isFieldRequired ( field . type ) ? '*' : '' } ${ field . name } (${ field . type } )` }
82- key = { `field-input-${ field . name } ` }
83- text = { model [ field . name ] }
84- onChangeText = { ( value ) => onChange ( field . name , value ) }
85- style = { { marginBottom : 6 } }
86- showMicrofone = { false }
87- placeholder = { `${ field . name } ` }
88- error = { ! ! errors ?. [ field . name ] ? errors [ field . name ] : false }
89- />
90- ) ) }
91- </ View >
92- </ Modal >
93- ) ;
71+ return (
72+ < Modal
73+ visible = { ! ! modalType }
74+ title = { `${ modalType === 'add' ? `Add new ${ tableData . name } record` : `Update ${ tableData . name } record` } ` }
75+ onClose = { onClose }
76+ onSubmit = { onSubmitHandler }
77+ >
78+ < View >
79+ { tableData . fields . map ( ( field ) => {
80+ const readOnly = typeof model [ field . name ] === 'object' && model [ field . name ] !== null ;
81+
82+ return (
83+ < Input
84+ title = { `${ isFieldRequired ( field . type ) ? '*' : '' } ${ field . name } (${ field . type } )` }
85+ key = { `field-input-${ field . name } ` }
86+ text = { model [ field . name ] }
87+ readOnly = { readOnly ? ( ) => { } : false }
88+ onChangeText = { ( value ) => onChange ( field . name , value ) }
89+ style = { [ { marginBottom : 6 } ] }
90+ showMicrofone = { false }
91+ showClearButton = { ! readOnly }
92+ placeholder = { `${ field . name } ` }
93+ error = { ! ! errors ?. [ field . name ] ? errors [ field . name ] : false }
94+
95+ />
96+ )
97+ } ) }
98+ </ View >
99+ </ Modal >
100+ ) ;
94101} ;
95102
96103export default TableModal ;
0 commit comments