Skip to content

Commit 68827a4

Browse files
committed
Fix update/add modal inputs margin
1 parent e8f7594 commit 68827a4

1 file changed

Lines changed: 78 additions & 75 deletions

File tree

src/components/Table/TableModal.tsx

Lines changed: 78 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,95 +8,98 @@ import Input from '../Input';
88
import Modal from '../Modal';
99

1010
type TableModalProps = {
11-
checkedRowValue: TableSignatureValue | null;
12-
tableData: TableSignature;
13-
modalType: ModalType;
14-
onClose: () => void;
15-
onSubmit: (model: TableSignatureValue, initialModel: TableSignatureValue,) => void;
11+
checkedRowValue: TableSignatureValue | null;
12+
tableData: TableSignature;
13+
modalType: ModalType;
14+
onClose: () => void;
15+
onSubmit: (
16+
model: TableSignatureValue,
17+
initialModel: TableSignatureValue
18+
) => void;
1619
};
1720

1821
const TableModal = ({
19-
checkedRowValue,
20-
tableData,
21-
modalType,
22-
onClose,
23-
onSubmit,
22+
checkedRowValue,
23+
tableData,
24+
modalType,
25+
onClose,
26+
onSubmit,
2427
}: TableModalProps) => {
25-
const [model, setModel] = useState<TableSignatureValue>({});
26-
const [errors, setErrors] = useState<TableSignatureValue>({});
27-
const initialModel = getInitialModel(
28-
tableData.fields,
29-
modalType === 'update' ? checkedRowValue : null
30-
);
28+
const [model, setModel] = useState<TableSignatureValue>({});
29+
const [errors, setErrors] = useState<TableSignatureValue>({});
30+
const initialModel = getInitialModel(
31+
tableData.fields,
32+
modalType === 'update' ? checkedRowValue : null
33+
);
3134

32-
useEffect(() => {
33-
setModel(initialModel);
34-
setErrors({});
35-
}, [modalType]);
35+
useEffect(() => {
36+
setModel(initialModel);
37+
setErrors({});
38+
}, [modalType, initialModel]);
3639

37-
const getValidate = (model: TableSignatureValue): TableSignatureValue => {
38-
return tableData.fields.reduce((acc, field) => {
39-
if (isFieldRequired(field.type)) {
40-
Object.assign(acc, {
41-
[field.name]: !!model[field.name] ? '' : REQIRED_TEXT,
42-
});
43-
}
40+
const getValidate = (model: TableSignatureValue): TableSignatureValue => {
41+
return tableData.fields.reduce((acc, field) => {
42+
if (isFieldRequired(field.type)) {
43+
Object.assign(acc, {
44+
[field.name]: !!model[field.name] ? '' : REQIRED_TEXT,
45+
});
46+
}
4447

45-
return acc;
46-
}, {} as TableSignatureValue);
47-
};
48+
return acc;
49+
}, {} as TableSignatureValue);
50+
};
4851

49-
const onChange = (field: string, value: string) => {
50-
const newModel = { ...model, [field]: value };
51-
setModel(newModel);
52+
const onChange = (field: string, value: string) => {
53+
const newModel = { ...model, [field]: value };
54+
setModel(newModel);
5255

53-
const _errors = getValidate(newModel);
54-
setErrors({
55-
...errors,
56-
[field]: !!_errors[field] ? _errors[field] : '',
57-
});
58-
};
56+
const _errors = getValidate(newModel);
57+
setErrors({
58+
...errors,
59+
[field]: !!_errors[field] ? _errors[field] : '',
60+
});
61+
};
5962

60-
const onSubmitHandler = () => {
61-
const _errors = getValidate(model);
62-
const errorLength = Object.values(_errors).filter((a) => !!a.length).length;
63-
setErrors(_errors);
63+
const onSubmitHandler = () => {
64+
const _errors = getValidate(model);
65+
const errorLength = Object.values(_errors).filter((a) => !!a.length).length;
66+
setErrors(_errors);
6467

65-
if (!!errorLength) return;
68+
if (!!errorLength) return;
6669

67-
onSubmit(model, initialModel);
68-
};
70+
onSubmit(model, initialModel);
71+
};
6972

70-
return (
71-
<Modal
72-
visible={!!modalType}
73-
title={`${modalType === 'add' ? `Add new ${tableData.name} record` : `Update ${tableData.name} record`}`}
74-
onClose={onClose}
75-
onSubmit={onSubmitHandler}
76-
>
77-
<View>
78-
{tableData.fields.map((field) => {
79-
const readOnly = typeof model[field.name] === 'object' && model[field.name] !== null;
73+
return (
74+
<Modal
75+
visible={!!modalType}
76+
title={`${modalType === 'add' ? `Add new ${tableData.name} record` : `Update ${tableData.name} record`}`}
77+
onClose={onClose}
78+
onSubmit={onSubmitHandler}
79+
>
80+
<View>
81+
{tableData.fields.map((field) => {
82+
const readOnly =
83+
typeof model[field.name] === 'object' && model[field.name] !== null;
8084

81-
return (
82-
<Input
83-
title={`${isFieldRequired(field.type) ? '*' : ''}${field.name} (${field.type})`}
84-
key={`field-input-${field.name}`}
85-
text={model[field.name]}
86-
readOnly={readOnly ? () => { } : false}
87-
onChangeText={(value) => onChange(field.name, value)}
88-
style={[{ marginBottom: 6 }]}
89-
showMicrofone={false}
90-
showClearButton={!readOnly}
91-
placeholder={`${field.name}`}
92-
error={!!errors?.[field.name] ? errors[field.name] : false}
93-
94-
/>
95-
)
96-
})}
97-
</View>
98-
</Modal>
99-
);
85+
return (
86+
<Input
87+
title={`${isFieldRequired(field.type) ? '*' : ''}${field.name} (${field.type})`}
88+
key={`field-input-${field.name}`}
89+
text={model[field.name]}
90+
readOnly={readOnly ? () => {} : false}
91+
onChangeText={(value) => onChange(field.name, value)}
92+
style={{ marginBottom: 6 }}
93+
showMicrofone={false}
94+
showClearButton={!readOnly}
95+
placeholder={`${field.name}`}
96+
error={!!errors?.[field.name] ? errors[field.name] : false}
97+
/>
98+
);
99+
})}
100+
</View>
101+
</Modal>
102+
);
100103
};
101104

102105
export default TableModal;

0 commit comments

Comments
 (0)