mobx-react-form textfield default value not recognized on submit - reactjs

The code is as follows
<TextField {...form.$("phone.countryCode").bind()} value={222} />
The form setup is as follows
{
name: "phone",
fields: [
{
name: "countryCode",
label: "Country Code:",
bindings: "TextField",
placeholder: "+",
rules: "required",
}
]
}
The value displays correctly in the textfield. But when i submit the form, it shows "required" error.
I assume field.input.onChange is not triggered when we set the value like this and hence the mobx field is not updated with the value.
Any idea on how to fix this?
Edit:
The value is a dynamic one based on the country selection. So you cannot set the value during initialise. That is why i had to go for this approach.

Don't need to override value on the TextField, mobx-react-form (I am assuming you are using it) can't possibly know that you are doing it. Just pass default value while initialising the form, like so:
const fields = [
{
name: "countryCode",
label: "Country Code:",
bindings: "TextField",
placeholder: "+",
rules: "required",
value: 'YOUR_DEFAULT_VALUE_HERE'
}
]
new MobxReactForm({ fields });
If the value depends on the other value then you can use useEffect, for example:
useEffect(() => {
// set countryCode value manually with needed value
FormModel.$("countryCode").set(country.code);
// Add needed deps
}, [country])

Related

autocomplete editor showOnFocus doesn't work when text is cleared using backspace

I am using ag grid auto complete editor ag-grid-autocomplete-editor on my react application. I have a requirement where I want to show the entire list of options when the ag grid cell is clicked and when the text is cleared on the field using backspace.
I have achieved the first requirement by setting the showOnFocus as true but seems like it doesn't work when I cleared the text from the cell editor.
My cell edit looks like as below -
fieLd: MY NAME_FIELD; headerName: MY_NAME, width: 400.
colId: MY_FIELD,
cellEditor: AutocompleteSelectCellEditor,
cellEditorParams:{
cellRenderer: "rowDropDownCellRenderer"
required: true,
selectData: MyOptions
placeholder: "Select"
autocomplete: {
showOnFocus: true
fetch: function(cellEditor: any, text: any, callBack: any, trigger: any){
if(myOptions.find(c => c.label === text || text.length === 0)){
var suggestions = myOptions;
callBack(suggestions)
} else {
varSuggestionNew = myOptions.filter(c => c.label.includes(text))
callback(varSuggestionNew)
}
}
}
}
MyOptions looks like as below -
[
{ value: 0, label: "this" }
{ value: 1, label: "is" },
{ value: 2, label: "sparta" }
{ value: 3, label: "yolo" },
]
So here one thing I noticed, fetch method is not called when I clear the text from ag grid field. That's the reason the options are not showing. Is there a way I can achieve this?
working example - https://stackblitz.com/edit/ag-grid-autocomplete-editor?file=index.js

react-select wont display default value

I am trying to display a default value in react-select based on an array of values. I have confirmed that the value is equal to the value I am comparing against. I have looked through this post, this post, this post, this post, this post and this post. Despite the similarities, the defaultValue will still not set. I have confirmed by loggin the result of my comparators that they are the same, both in uppercase, both strings.
My Select element is
<Select
name="disposition"
classNamePrefix="select"
options={statusOptions}
ref={selectInputRefPlan}
styles={singleStylesRowComp}
defaultValue={statusOptions.filter(
({ value }) => value.value === lead.disposition
)}
/>
and my array is
const statusOptions = [
{ value: "HOT", label: i18n._(t`HOT`) },
{ value: "WIP", label: i18n._(t`WIP`) },
{ value: "LOST", label: i18n._(t`LOST`) },
{ value: "DNC", label: i18n._(t`DNC`) },
{ value: "WON", label: i18n._(t`WON`) },
{ value: "NEW", label: i18n._(t`NEW`) },
];
I am testing against the value and not the label, so translation issues won't arise. when i log the response, value.value === 'NEW' and lead.disposition === 'NEW', however, the defaultValue will not set. I have also tried the prop value, as well as having read through the 6 or 7 similar posts. Is there a simple syntax error? or something else I am missing?
Here is a sandbox link that demonstrates the issue
The error is in your filter function. You decompose value, and then access value.value. This is undefined.
Instead, take the option argument without decomposition, and access option.value.
<Select
name="disposition"
classNamePrefix="select"
options={statusOptions}
ref={selectInputRefPlan}
styles={singleStylesRowComp}
defaultValue={statusOptions.filter((option) => option.value === lead.disposition)}
/>;

react-semantic-redux-form selectField multiple

I am attempting to use react-semantic-redux-form SelectField with the multiple options so a user can select multiple options and if there is one already set then this should show as checked.
I am also using redux-form with semantic0ui-react.
I am getting an error attempting to include multiple selections.
My include statement is:
import { SelectField } from "react-semantic-redux-form";
My state is:
state = {
relationships: ["some entry"],
relationshipOptions: [],
};
The element code is:
<Grid.Column>
<Field
component={SelectField}
name="relationships"
label="Your Relationships"
options={relationshipOptions}
multiple
placeholder="Select to add a relationship"
/>
I get the error as below
Dropdown `value` must be an array when `multiple` is set. Received type: `[object String]`.
in Dropdown
The way you have relationshipOptions is wrong, it is supposed array of objects,
const relationshipOptions = [
{ key: "single", value: "single", text: "single" },
{ key: "married", value: "married", text: "married" }
];
Here is the working example, Code Sandbox
Also if you have single, married in array. You can do something like this,
let relationshipOptions = ["single", "married"].map((x) => {
return ({
key: x,
value: x,
text: x
});
});

Material-ui multiselect with optgroup

I am having problems with setting up grouped multiselect using material-ui in react. As material ui does not have such build-in component, I had to do it myself.
I am attaching codesandbox example of my code.
https://codesandbox.io/s/goofy-sun-2ffyv
The problem is that on onchange event the value I get I get is undefined.
Yet, I am excpected to get the value instead.
Any help and suggestion is appreciated.
According to Material-ui select docs:
The MenuItem elements must be direct descendants when native is false.
You are using div as direct element and MenuItems as children of div.
To fix this issue, i would recommend changing the structure of your data (I would use isDisabled without changing the structure, but I don't know if you want to use it for something else)
const posts = [
{
options: [
{
value: 'Hello',
label: 'Hello',
isOptGroup: true // -> the new flag
},
{
value: "3",
label: "Ойбек",
isDisabled: true
},
{
value: "2",
label: "Чинор",
isDisabled: true
},
{
value: "1",
label: "Озодбош",
isDisabled: true
}
]
}
];
And your MenuItem will be disabled based on that flag:
<MenuItem
key={option.value}
value={option.value}
className={classes.item}
disabled={option.isOptGroup}
>
<ListItemText primary={option.label} />
</MenuItem>

Blur/Click on Formpanel ExtJS 4 does not access form fields correctly

I have an ExtJS Formpanel and I have written a listener on a click for the form ( not on any fields of a form ) which is working fine.
Even after setting the Ext.FocusManager.Enable() to true, I am not able to get the even 'blur' working. What am I missing?
I am not able to access form fields from the event handlers for formpanel click event. When I do - this.up.('form').get.(fielname).value [which works fine in the event handlers on the form fields.] It says the element is undefined. How can I access the form elements here?
Adding the code snippet -
// Call it Object A
Ext.create.('Ext.form.Panel', {
id : xyz,
items: [
{
xtype : 'textfield',
name : 'test',
fieldLabel : 'Name'
}
listeners : { // listener on the formPanel; not on any of its element
click : {
console.log("this works" );
},
focus : {
console.log('this does not work');
}
}
]
}
I am doing this so that I can access a value of another object, say B.field.
Onload I am able to fetch the value of B.field. But when the user changes the value of B.field which is on a different tab, I am not able to fetch the changed value of B.field in A. I am just finding ways to avoid an Ajax call to the database, if possible.
Thanks in advance for your time.
Without any sample from your code to reference, it's hard to determine what you are trying to do.
It could be that you just need to fix how you are querying for the form elements. For example, elements in a toolbar are not children of the form, so up/down doesn't work.
I don't think you can listen for the events focus, blur, or click on a form. Even if you could, I am not sure you would want to do. Instead, it's more common to listen for focus on a field or click on a button.
Example 1 form with field using focus and button using click
http://codepen.io/anon/pen/qEPRge?editors=001
;(function(Ext) {
Ext.onReady(function() {
console.log("Ext.onReady")
var form = new Ext.create("Ext.form.Panel", {
title: "person"
,items: [
{itemId: "fld-id", fieldLabel: "id", name: "id", value: "1", xtype: "textfield", labelAlign: "top", labelSeparator: ""}
,{itemId: "fld-name", fieldLabel: "name", name: "name", value: "Emily", xtype: "textfield", labelAlign: "top", labelSeparator: ""}
,{itemId: "btn-submit", text: "submit", xtype: "button"}
]
})
form.on("afterrender", function(component) {
console.log("form.afterrender")
})
form.render(Ext.getBody())
form.queryById("fld-id").on("focus", function(component) {
console.log("fld-id.focus")
})
form.queryById("fld-name").on("focus", function(component) {
console.log("fld-name.focus")
})
form.queryById("btn-submit").on("click", function(component) {
console.log("btn-submit.click")
console.log("fld-id.value:")
console.log(component.up("form").queryById("fld-id").getValue())
console.log("fld-name.value:")
console.log(component.up("form").queryById("fld-name").getValue())
})
})
})(Ext)

Resources