How to clear MUI Autocomplete to initial state - material-ui

I would like to reset/clear my MUI Autocomplete component.
I have two of them with model like { label: string, value: string } and if first will change its value then I would like to clear the second one since second will get options by selected value in first one.
Moreover - I am using react-hook-form with setValue. I use as value in this method { label: '', value: '' } but it causes warning that in my new sort of options there is no such option to select (this is minor issue I think) but it does not reset second Autocomplete input but partially. I still see X to clear value. I used null as value in setValue but it does not cleat input as well.
What I want to achieve is - selecting some option on first input I would like to reset second input like clicking on X does. Is it possible ?
Cheers!

I found what caused the issue I describe above. It was because Autocomplete in my codebase was declared with clearOnBlur={false} props. It prevented to clear value of my Autocomplete when I was doing setValue(autocompleteFieldName, null) via react-hook-form. I hope it helps someone with the same strange issue since I was not interacting with input at all to invoke onBlur event. Cheers!

Related

Pressing Enter on a date input in Cypress

I have an app with some input fields that I'm trying to automate. The gist of these fields is that I should be able to double click a field, type in a new value, then press Enter to submit that value, which sends a PUT request and also closes the input field. This works for any input type except date.
So far I've tried:
Using cy.type('{enter}'). This gives Typing into a date input with cy.type() requires a valid date in the format 'yyyy-MM-dd'. You passed: {enter}
Using cy.trigger() to send out a keydown event for the enter key. This works, as in it closes the input field successfully but it somehow doesn't send the PUT request.
Pressing enter on the parent Element. Same as using cy.trigger()
Strangely enough, manually opening the input field myself, typing a date and pressing enter will send the request just fine. It seems to me like there's some issue with programmatically pressing enter to submit the field without Cypress interpreting this as my attempt to actually type an invalid character into the date field. The docs do specifically say that no special characters are allowed in a date field.
Can't post any code as this is corporate.
I have tried to let it work, but it simply can't be done at the moment. Something like this should work:
it.only('test', function () {
cy.visit('https://www.html5tutorial.info/html5-date.php')
cy.get('input')
.type('2009-12-12')
.type('{enter}')
})
But it doesn't so I started to dig into the pile of issues and found this one:
https://github.com/cypress-io/cypress/issues/3405 . It is about a different input type, but I believe it is related to your problem.
Unfortunately this problem is still present in Cypress 9.5. One possible work-around is to directly trigger the Javascript keyup or keydown event that you are listening for.
cy.get('input')
.type('2020-01-01T12:00')
.trigger('keydown', {
key: 'Enter',
});
This works for me, but as you pointed out might not in all situations. It depends entirely on how your app is listening for Enter in the first place and on which element.
Another possible option is to call .submit() on the form that wraps the input. If you're testing a component, you could create your own wrapper component that contains a form in order to trigger the submit.
You should remove 'date' attribute like below:
cy.get('input').invoke('removeAttr','type').type('2009-12-12{enter}');
I used something like that in my code, and it worked properly
cy.get('get-your-input').invoke('removeAttr', 'type')type('2022-12-01{enter}').trigger('change');
cy.get('get-your-input').invoke('attr', 'value', '2022-12-01').trigger('change');
cy.get('get-your-input').invoke('attr', 'type', 'date');
Please add date plus enter keystroke in the type: cy.get("#date").type("02-02-2022{enter}");

Angular 2 - Dynamic Reactive form with Radio Inputs build with FormBuilder, FormGroup, FormArray

Case:
I have an array of answers which I want to show in the view as radio inputs. When one of the answers is already answered it must be [checked] and if it's [checked] a textarea shows, so far so good.
Next I want to check another radio input and I want the current selected to be deselected and its textarea hidden, then the new selected radio input should be [checked] and it's textarea should show.
I use the FormBuilder and FormArray and have following issues.
I can't use index without intrapolation, I see many examples where the index is used without.
If I select another radio input, first my data disappears and it's not checked and on the second click it get's checked but now both are checked.
- I don't have access to the checked event, I can show it in the view with {{tempVar.checked}} as you can see above, if I use a template variable #tempVar, but I don't have access to it in the textarea below *ngIf="tempVar.checked". If I do use it in the ngIf I get the following error
Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'.
Questions:
Is this the right approach?
An example of a Reactive Dynamic form with FormBuilder and FormArray with Radio inputs
Here is my code
https://gist.github.com/webwizart/121c85a0f316f47977cc0c99455af7bf
I would think using the tags should be reserved for unique identifiers? I wouldn't be surprised if dom renderer will update all elements with the same id.
Not sure if this will help, but you could use an answer view model instead (containing the same data) but that also has a 'checked' property
This way you can be sure that it will behave as you would expect.
e.g.: (in your component.ts)
let answers = this.question.Question.PossibleAnswers
.map(a =>
{ return Object.assign({}, a, { checked: false });
}
);
then in html you use: 'answer.checked' instead of 'radio.checked'
ps: also you could use ng-container instead of span, so you don't have an extra DOM imprint in your html

install4j: Configurable form change visibility from drop-down list

I have a Configurable Form and i want to change the visibility of another field based on the value of a Drop-down list.
For example I have a Drop-Down list with entries A,B and the variable name for it is testDD.
I have a Text field smtpMailServer that I want to display only if testDD's value is A.
I have tried the following approaches in smtpMailServer's visibility without success:
return ((String) context.getVariable("testDD")).equals("A");
return (context.getVariable("testDD")).equals("A");
and I've also tried to add a script to testDD Change Selection Script with The following code
context.setVariable("ThisFormConfiguration", selectedItem);
And use the code above with ThisFormConfiguration instead of testDD. But it's not working.
Could you please help me?
Thanks!
I have tried the following approaches in smtpMailServer's visibility without success
The visibility script of a form component is only evaluated when the form is shown. You should keep it, but it only handles the initial condition.
and I've also tried to add a script to testDD Change Selection Script with
The following code context.setVariable("ThisFormConfiguration", selectedItem); A
Using the "Selection change script" property is the right idea, but your script has no effect. There is no live binding from the variables to the form components, the variable is read when the form is shown and updated when the user clicks "Next".
You have to use the following selection script:
formEnvironment.getFormComponentById("123").setVisible(selectedItem.equals("A"));
where "123" has to be replaced by the ID of the text field.

sap.m.Select: start with a blank selection input element

When using a data aggregation on sap.m.Select, the first entry is always selected. Here's a link to the SDK's preview.
Example code from my app
new sap.m.Select("id-names", {
width: '100%',
}).bindAggregation("items", "data>/trip/names", new sap.ui.core.Item({
text: "{data>Name}"
}));
There is a parameter called selectedKey on the constructor to change this to another index. What I want is the select to be blank, because I want to force my users to make a choice, not blandly accept the first entry in the list.
I could force an blank entry in my aggregation data>/trip/names but that would pollute my list.
Is there a better way to achieve this?
Since the OpenUI5 version 1.34, you can set the forceSelection property to false.
The forceSelection property indicates whether the selection is restricted to one of the items in the list. The default value is true (which means, if the selection is not set, the first item in the dropdown list is selected).
When to set it to false?
If you do not want a default item to be pre selected.
Additional information
https://github.com/SAP/openui5/commit/b2191fd50e2115f8f9d2db7604a75fb50c57591f
Currently, no. There seems to be no better way.
There is a ticket for that on GitHub.
It's also my opinion to avoid messing with the dataset and much liked the idea of adding an additional item aggregate. However my improvement on this is to use a formatter on the control itself so it is clearly visible and executed at the right time.
I make use of a formatter with fully qualified controller to get the control as 'this' parameter. In the formatter function I add a ListItem, as proposed by #Victor S
In XML view
<Select forceSelection="false" selectedKey="{model>/key}" items="{path: 'model>/Items'}" icon="{path: '', formatter: 'mynamespace.Utils.addDeselectOption'}">
In the Utils controller:
addDeselectOption: function() {
var that = this;
this.getBinding("items").attachDataReceived(function(){
that.insertItem(new sap.ui.core.ListItem({text: '', key: undefined}), 0);
});
}
Works form me in UI5 1.52
Even though this solution is not great, I managed to get the empty field stick by adding both, the forceSelection=false property, and also in the controller's onInit function (I used the Select element):
var codeField = this.getView().byId("codeField");
setTimeout(function() {
codeField.insertItem(new sap.ui.core.ListItem({text: '', key: undefined}), 0);
}, 1000);
If the forceSelection=false is left out, the field will load either too early or too late to the drop down, which will cause the wrong selection to be visible. Hope it helps someone.
You can also extend the control and build you own select with e.g. an additional parameter add empty choice...I am actually also thinking about that...

Live search combo in Ext JS 4.2.2

I'm trying to implement a live search combo. It suppose to work like this:
When I enter a character into the combo field I read the current value and send it as a parameter to the store's url. In the backend the parameter is used to return any value from the database that contains it, so the store behind the combo gets filled only with those filtered values.
As I continue to enter characters into the combo, the parameter should be updated and sent again to the backend and so on, getting like this a smaller and smaller store.
I tryied to achieve this behaviour using the combo's event keypress, even keyup, but the problem is it's impossible for me to get access to the current value from the combo field.
For example, when I entered the "for" string into the combo, how can I obtain this value using the combo object? comboName.getValue() doesn't work, it returns nothing "".
I already saw the live combo example here: http://docs.sencha.com/extjs/4.2.2/#!/example/form/forum-search.html but doesnt help me at all.
So my big question is: how do i get the current value while still editing the combo's field?
Any help would be appreciated, thanks.
You should be able to use
comboName.getValue();
or
comboName.getRawValue();
Where comboName is your combo box. Are neither working- I note in your post you state getValues() which is an improper method. You may want to also check whether when you're referring to your combo box object, that the reference is actually correct. The first argument from the key events is actually the object itself, so you should be able to do, e.g.
listeners:{
keyup:function(comboBox){
var value = comboBox.getValue() || comboBox.getRawValue();
console.log(value);
}
}
Swapping you the value getting method as appropriate.
I found that the combo already has a quick search behaviour, I just have to set queryMode on 'remote' and some other small configurations. More details here:
Ext Js 4.2.2 combobox queryMode