-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORE-1967 Refactor metadata template form fields
Try to simplify the logic for building and validating form fields.
- Loading branch information
Showing
13 changed files
with
423 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/components/metadata/templates/fields/CheckboxStringValueField.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @author psarando | ||
*/ | ||
import React from "react"; | ||
|
||
import { FastField } from "formik"; | ||
|
||
import FormCheckboxStringValue from "components/forms/FormCheckboxStringValue"; | ||
|
||
const CheckboxStringValueField = ({ | ||
attribute, | ||
avu, | ||
avuFieldName, | ||
writable, | ||
...props | ||
}) => { | ||
return ( | ||
<FastField | ||
name={`${avuFieldName}.value`} | ||
component={FormCheckboxStringValue} | ||
label={attribute.name} | ||
disabled={!writable} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default CheckboxStringValueField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @author psarando | ||
*/ | ||
import React from "react"; | ||
|
||
import { FastField } from "formik"; | ||
|
||
import { useTranslation } from "i18n"; | ||
import FormTextField from "components/forms/FormTextField"; | ||
import { MenuItem } from "@mui/material"; | ||
|
||
const EnumField = ({ attribute, avu, avuFieldName, writable, ...props }) => { | ||
const { t } = useTranslation("metadata"); | ||
|
||
return ( | ||
<FastField | ||
name={`${avuFieldName}.value`} | ||
component={FormTextField} | ||
select | ||
label={attribute.name} | ||
required={attribute.required && writable} | ||
inputProps={{ readOnly: !writable }} | ||
validate={(value) => { | ||
if (attribute.required && !value) { | ||
return t("required"); | ||
} | ||
}} | ||
{...props} | ||
> | ||
{attribute.values && | ||
attribute.values.map((enumVal, index) => ( | ||
<MenuItem key={index} value={enumVal.value}> | ||
{enumVal.value} | ||
</MenuItem> | ||
))} | ||
</FastField> | ||
); | ||
}; | ||
|
||
export default EnumField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @author psarando | ||
*/ | ||
import React from "react"; | ||
|
||
import { FastField } from "formik"; | ||
|
||
const GroupingField = ({ | ||
attribute, | ||
avu, | ||
avuFieldName, | ||
writable, | ||
...props | ||
}) => { | ||
return ( | ||
<FastField name={`${avuFieldName}.value`} component="span" {...props} /> | ||
); | ||
}; | ||
|
||
export default GroupingField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @author psarando | ||
*/ | ||
import React from "react"; | ||
|
||
import { FastField } from "formik"; | ||
|
||
import { useTranslation } from "i18n"; | ||
import FormIntegerField from "components/forms/FormIntegerField"; | ||
|
||
const IntegerField = ({ attribute, avu, avuFieldName, writable, ...props }) => { | ||
const { t } = useTranslation("metadata"); | ||
|
||
return ( | ||
<FastField | ||
name={`${avuFieldName}.value`} | ||
component={FormIntegerField} | ||
label={attribute.name} | ||
required={attribute.required && writable} | ||
inputProps={{ readOnly: !writable }} | ||
validate={(value) => { | ||
if (attribute.required && !value && value !== 0) { | ||
return t("required"); | ||
} | ||
|
||
if (isNaN(Number(value))) { | ||
return t("templateValidationErrMsgNumber"); | ||
} | ||
}} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default IntegerField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/components/metadata/templates/fields/MultilineTextField.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @author psarando | ||
*/ | ||
import React from "react"; | ||
|
||
import { FastField } from "formik"; | ||
|
||
import { useTranslation } from "i18n"; | ||
import FormMultilineTextField from "components/forms/FormMultilineTextField"; | ||
|
||
const MultilineTextField = ({ | ||
attribute, | ||
avu, | ||
avuFieldName, | ||
writable, | ||
...props | ||
}) => { | ||
const { t } = useTranslation("metadata"); | ||
|
||
return ( | ||
<FastField | ||
name={`${avuFieldName}.value`} | ||
component={FormMultilineTextField} | ||
label={attribute.name} | ||
required={attribute.required && writable} | ||
inputProps={{ readOnly: !writable }} | ||
validate={(value) => { | ||
if (attribute.required && !value) { | ||
return t("required"); | ||
} | ||
}} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default MultilineTextField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @author psarando | ||
*/ | ||
import React from "react"; | ||
|
||
import { FastField } from "formik"; | ||
|
||
import { useTranslation } from "i18n"; | ||
import FormNumberField from "components/forms/FormNumberField"; | ||
|
||
const NumberField = ({ attribute, avu, avuFieldName, writable, ...props }) => { | ||
const { t } = useTranslation("metadata"); | ||
|
||
return ( | ||
<FastField | ||
name={`${avuFieldName}.value`} | ||
component={FormNumberField} | ||
label={attribute.name} | ||
required={attribute.required && writable} | ||
inputProps={{ readOnly: !writable }} | ||
validate={(value) => { | ||
if (attribute.required && !value && value !== 0.0) { | ||
return t("required"); | ||
} | ||
|
||
if (isNaN(Number(value))) { | ||
return t("templateValidationErrMsgNumber"); | ||
} | ||
}} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default NumberField; |
Oops, something went wrong.