Skip to content

Commit

Permalink
forms: improve diversity of fields
Browse files Browse the repository at this point in the history
* display empty value if field is None
* add possibility to prefill fields with default value
* add possibility to specify the width of table column (search view)
* add render of checkbox, dropdown and textarea fields
* closes inveniosoftware/invenio-banners#10
  • Loading branch information
anikachurilova committed Feb 8, 2023
1 parent e3a8cbf commit 8a9289f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class DateFormatter extends React.Component {
render() {
const { value } = this.props;

if (!value) {
return null;
}

const date = DateTime.fromISO(value);
return (
<p data-testid="date-formatter">{date.toLocaleString(DateTime.DATETIME_MED)}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import mapValues from "lodash/mapValues";
export class AdminForm extends Component {
constructor(props) {
super(props);
const { resource, formFields } = props;
const { resource, resourceSchema: resourceSchema } = props;

this.state = {
error: undefined,
formData: resource
? resource
: mapValues(formFields, function (value) {
: mapValues(resourceSchema, function (value) {
const defaultValue = value.metadata.default;
if (defaultValue) {
return defaultValue;
}
return "";
}),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React from "react";
import { Input, AutocompleteDropdown } from "react-invenio-forms";
import {
Input,
AutocompleteDropdown,
BooleanField,
Dropdown,
TextArea,
} from "react-invenio-forms";
import _capitalize from "lodash/capitalize";
import PropTypes from "prop-types";
import { Form, Segment, Header } from "semantic-ui-react";
Expand All @@ -12,6 +18,7 @@ const fieldsMap = {
integer: Input,
uuid: Input,
datetime: Input,
bool: BooleanField,
};

const generateFieldProps = (
Expand All @@ -34,11 +41,14 @@ const generateFieldProps = (
}

const htmlDescription = (
<div
dangerouslySetInnerHTML={{
__html: formFieldConfig?.description || fieldSchema?.metadata?.description,
}}
/>
<>
<p />
<div
dangerouslySetInnerHTML={{
__html: formFieldConfig?.description || fieldSchema?.metadata?.description,
}}
/>
</>
);

return {
Expand All @@ -49,6 +59,9 @@ const generateFieldProps = (
required: fieldSchema.required,
disabled: fieldSchema.readOnly || (fieldSchema.createOnly && !isCreate),
placeholder,
defaultValue: formFieldConfig?.defaultValue || fieldSchema?.metadata?.defaultValue,
options: formFieldConfig?.options || fieldSchema?.metadata?.options,
rows: formFieldConfig?.rows || fieldSchema?.metadata?.rows,
};
};

Expand Down Expand Up @@ -93,6 +106,20 @@ const mapFormFields = (obj, parentField, isCreate, formFieldsConfig, dropDumpOnl
);
}

if (fieldSchema.type === "bool") {
const description = fieldProps.description;
return (
<>
<BooleanField
key={fieldProps.fieldPath}
required={fieldSchema.required}
{...fieldProps}
/>
{description && <label className="helptext">{description}</label>}
</>
);
}

if (fieldSchema.type === "vocabulary") {
return (
<AutocompleteDropdown
Expand Down Expand Up @@ -125,6 +152,31 @@ const mapFormFields = (obj, parentField, isCreate, formFieldsConfig, dropDumpOnl
);
}

const dropdownOptions =
formFieldsConfig[fieldName]?.options || fieldSchema?.metadata?.options;
if (fieldSchema.type === "string" && dropdownOptions) {
return (
<Dropdown
key={fieldProps.fieldPath}
required={fieldSchema.required}
options={dropdownOptions}
{...fieldProps}
/>
);
}

const rows = formFieldsConfig[fieldName]?.rows || fieldSchema?.metadata?.rows;
if (fieldSchema.type === "string" && rows) {
return (
<TextArea
key={fieldProps.fieldPath}
fieldPath={fieldProps.fieldPath}
rows={rows}
{...fieldProps}
/>
);
}

const Element = fieldsMap[fieldSchema.type];
return <Element {...fieldProps} key={fieldProps.fieldPath} />;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export const SearchResultsContainer = ({
<Table>
<Table.Header>
<Table.Row>
{columns.map(([property, { text, order }], index) => {
const width = index === 0 ? undefined : index === 1 ? 4 : 3;
{columns.map(([property, { text, order, width }], index) => {
if (!width) {
width = index === 0 ? undefined : index === 1 ? 4 : 3;
}

return (
<Table.HeaderCell key={property + order} width={width}>
{text}
Expand Down

0 comments on commit 8a9289f

Please sign in to comment.