Skip to content

Commit

Permalink
fix: fix field name if no label is set
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliaghisini committed Aug 25, 2021
1 parent d2d7897 commit a306050
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/components/EditBlock.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ class EditBlock extends SubblockEdit {
if (__SERVER__) {
return <div />;
}
const id = new Date().getTime();
const id = this.props.data?.id || new Date().getTime();

return (
<Subblock subblock={this} className="subblock-edit">
<div key={this.props.data.index}>
<Field
{...this.props.data}
name={getFieldName(this.props.data.label)}
name={getFieldName(this.props.data.label, id)}
key={this.props.data.index}
isOnEdit={true}
id={id}
Expand Down
3 changes: 2 additions & 1 deletion src/components/FormView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ const FormView = ({
</Grid.Row>
))}
{data.subblocks?.map((subblock, index) => {
let name = getFieldName(subblock.label);
let name = getFieldName(subblock.label, subblock.id);

return (
<Grid.Row key={'row' + index}>
<Grid.Column>
Expand Down
6 changes: 3 additions & 3 deletions src/components/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const formStateReducer = (state, action) => {

const getInitialData = (data) => ({
...data.reduce(
(acc, field) => ({ ...acc, [getFieldName(field.label)]: field }),
(acc, field) => ({ ...acc, [getFieldName(field.label, field.id)]: field }),
{},
),
});
Expand Down Expand Up @@ -92,7 +92,7 @@ const View = ({ data, id, path }) => {
const isValidForm = () => {
let v = [];
data.subblocks.forEach((subblock, index) => {
let name = getFieldName(subblock.label);
let name = getFieldName(subblock.label, subblock.id);
let fieldType = subblock.field_type;
let additionalField =
config.blocks.blocksConfig.form.additionalFields?.filter(
Expand Down Expand Up @@ -131,7 +131,7 @@ const View = ({ data, id, path }) => {
let attachments = {};

data.subblocks.forEach((subblock, index) => {
let name = getFieldName(subblock.label);
let name = getFieldName(subblock.label, subblock.id);
if (formData[name]?.value) {
formData[name].field_id = subblock.field_id;
const isAttachment = subblock.field_type === 'attachment';
Expand Down
6 changes: 4 additions & 2 deletions src/components/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { saveAs } from 'file-saver';

export const getFieldName = (label) => {
return label?.toLowerCase().replace(/[^a-zA-Z0-9]/g, '_');
export const getFieldName = (label, id) => {
return label?.length > 0
? label?.toLowerCase().replace(/[^a-zA-Z0-9]/g, '_')
: id;
};

/**
Expand Down

0 comments on commit a306050

Please sign in to comment.