Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to configure default new item value for the array input #6595

Closed
archfz opened this issue Sep 19, 2021 · 3 comments
Closed

Possibility to configure default new item value for the array input #6595

archfz opened this issue Sep 19, 2021 · 3 comments

Comments

@archfz
Copy link

archfz commented Sep 19, 2021

Is your feature request related to a problem? Please describe.
I am trying to add a default value for new items that are added on the ArrayInput using the SimpleFormIterator.

Describe the solution you'd like

diff --git a/node_modules/ra-ui-materialui/esm/form/SimpleFormIterator.d.ts b/node_modules/ra-ui-materialui/esm/form/SimpleFormIterator.d.ts
index 5adc70a..33a6083 100644
--- a/node_modules/ra-ui-materialui/esm/form/SimpleFormIterator.d.ts
+++ b/node_modules/ra-ui-materialui/esm/form/SimpleFormIterator.d.ts
@@ -5,6 +5,7 @@ declare const SimpleFormIterator: FC<SimpleFormIteratorProps>;
 declare type DisableRemoveFunction = (record: Record) => boolean;
 export interface SimpleFormIteratorProps extends Partial<Omit<FieldArrayRenderProps<any, HTMLElement>, 'meta'>> {
     addButton?: ReactElement;
+    itemDefaultValue?: any;
     basePath?: string;
     className?: string;
     defaultValue?: any;
diff --git a/node_modules/ra-ui-materialui/esm/form/SimpleFormIterator.js b/node_modules/ra-ui-materialui/esm/form/SimpleFormIterator.js
index d0212f6..95ddd36 100644
--- a/node_modules/ra-ui-materialui/esm/form/SimpleFormIterator.js
+++ b/node_modules/ra-ui-materialui/esm/form/SimpleFormIterator.js
@@ -119,7 +119,7 @@ var SimpleFormIterator = function (props) {
     };
     var addField = function () {
         ids.current.push(nextId.current++);
-        fields.push(undefined);
+        fields.push(props.itemDefaultValue);
     };
     // add field and call the onClick event of the button passed as addButton prop
     var handleAddButtonClick = function (originalOnClickHandler) { return function (event) {
@@ -180,6 +180,7 @@ SimpleFormIterator.defaultProps = {
 };
 SimpleFormIterator.propTypes = {
     defaultValue: PropTypes.any,
+    itemDefaultValue: PropTypes.any,
     addButton: PropTypes.element,
     removeButton: PropTypes.element,
     basePath: PropTypes.string,
diff --git a/node_modules/ra-ui-materialui/lib/form/SimpleFormIterator.d.ts b/node_modules/ra-ui-materialui/lib/form/SimpleFormIterator.d.ts
index 5adc70a..33a6083 100644
--- a/node_modules/ra-ui-materialui/lib/form/SimpleFormIterator.d.ts
+++ b/node_modules/ra-ui-materialui/lib/form/SimpleFormIterator.d.ts
@@ -5,6 +5,7 @@ declare const SimpleFormIterator: FC<SimpleFormIteratorProps>;
 declare type DisableRemoveFunction = (record: Record) => boolean;
 export interface SimpleFormIteratorProps extends Partial<Omit<FieldArrayRenderProps<any, HTMLElement>, 'meta'>> {
     addButton?: ReactElement;
+    itemDefaultValue?: any;
     basePath?: string;
     className?: string;
     defaultValue?: any;

Describe alternatives you've considered
No alternatives.

Additional context
For my case it's needed for proper json schema validation, issue being that if we have undefined the error is on the array index not on the properties of the object on that index, which causes the error to not be displayed.

@djhi
Copy link
Collaborator

djhi commented Sep 21, 2021

Can't you use the defaultValue on the inputs you pass inside the SimpleFormIterator?

@archfz
Copy link
Author

archfz commented Sep 22, 2021

This is not for the default value of the whole input. This is the default value of an item when it is being added with the add button. Please see in patch this part fields.push(props.itemDefaultValue);

@WiXSL
Copy link
Contributor

WiXSL commented Jan 28, 2022

Duplicate of #4805

@WiXSL WiXSL marked this as a duplicate of #4805 Jan 28, 2022
@WiXSL WiXSL closed this as completed Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants