Skip to content

Commit

Permalink
fix: fixed issue with CreateMultiLingualFormItems not rendering when …
Browse files Browse the repository at this point in the history
…text in input area changes. closes #1535
  • Loading branch information
syam babu committed Jan 16, 2025
1 parent d9087d9 commit 082f4e1
Showing 1 changed file with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React from 'react';
import { contentLanguageKeyMap } from '../../constants/contentLanguage';
import { Form } from 'antd';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -26,22 +26,28 @@ import MultilingualInput from '../../components/MultilingualInput';

const CreateMultiLingualFormItems = ({ children, ...rest }) => {
const { calendarContentLanguage, form, name, data, required, validations, dataCy, placeholder, entityId } = rest;
Form.useWatch(name[0], form);
const { t } = useTranslation();

// State to track dirty fields
const [isFieldDirty, setIsFieldDirty] = useState({});
// State to force re-render on trackFormValue change

let isFieldDirty = {}; // to keep track of dirty fields
let dataCyCollection = [];
let placeholderCollection = [];

const formItemList = calendarContentLanguage.map((language) => {
calendarContentLanguage.forEach((language) => {
const lanKey = contentLanguageKeyMap[language];
const fieldName = name.concat([lanKey]);
isFieldDirty[lanKey] = form.isFieldTouched(fieldName);
});

const formItemList = calendarContentLanguage.map((language) => {
const dependencies = calendarContentLanguage // dependencies for each form item
.filter((lan) => lan !== language)
.map((lan) => [name, contentLanguageKeyMap[lan]]);

dataCyCollection.push(`${dataCy}${language.toLowerCase()}`);
placeholderCollection.push(placeholder[lanKey] ?? '');
placeholderCollection.push(placeholder[contentLanguageKeyMap[language]] ?? '');

const validationRules = required // validation rules for each form item
? [
Expand All @@ -62,21 +68,11 @@ const CreateMultiLingualFormItems = ({ children, ...rest }) => {

return (
<Form.Item
name={[`${name}`, lanKey]}
name={[`${name}`, contentLanguageKeyMap[language]]}
key={language}
dependencies={dependencies}
initialValue={data?.[lanKey]}
rules={validationRules}
shouldUpdate={() => {
const updatedIsFieldDirty = {};
calendarContentLanguage.forEach((language) => {
const lanKey = contentLanguageKeyMap[language];
const fieldName = name.concat([lanKey]);
updatedIsFieldDirty[lanKey] = form.isFieldTouched(fieldName);
});
setIsFieldDirty(updatedIsFieldDirty);
return true;
}}>
initialValue={data?.[contentLanguageKeyMap[language]]}
rules={validationRules}>
{children}
</Form.Item>
);
Expand Down

0 comments on commit 082f4e1

Please sign in to comment.