Tweaking Formatting in WebStorm for JSX / React - sublimetext2

I'm looking for an editor that will aid in Formatting React/JSX Code with no headaches. I'm quite impressed with WebStorm while working with react / jsx, little-to-no setup. The formatter is almost perfect. Can I somehow tweak the formatter?
Current Code:
<button onClick={this.props.showHistoryButton} style={{textTransform:'capitalize',borderRadius:0,boxShadow:'none',border:'1px solid transparent',width:100,zIndex:9}} className="btn btn-default">
History
</button>
<UserRow loadRecord={this.props.loadRecord} showHistory={this.props.showHistory} checkedIds={this.props.checkedIds} expanded={user.expanded}
expandRow={expandRow}
compressRow={compressRow}
resetExpanded={resetExpanded}
allExpanded={allExpanded} checked={user.checked}
unCheckRow={unCheckRow}
checkRow={checkRow}
resetParentHeight={this.resetParentHeight.bind(this)} tableContainerHeight={this.state.tableContainerHeight}
key={index}
user={user}/>
Code after Format / Indent
<button onClick={this.props.showHistoryButton} style={{
textTransform: 'capitalize',
borderRadius: 0,
boxShadow: 'none',
border: '1px solid transparent',
width: 100,
zIndex: 9
}} className="btn btn-default">
History
</button>
<UserRow loadRecord={this.props.loadRecord} showHistory={this.props.showHistory}
checkedIds={this.props.checkedIds} expanded={user.expanded}
expandRow={expandRow}
compressRow={compressRow}
resetExpanded={resetExpanded}
allExpanded={allExpanded} checked={user.checked}
unCheckRow={unCheckRow}
checkRow={checkRow}
resetParentHeight={this.resetParentHeight.bind(this)}
tableContainerHeight={this.state.tableContainerHeight}
key={index}
user={user}/>
Expected Result
<UserRow
loadRecord={this.props.loadRecord}
showHistory={this.props.showHistory}
As you can see not every 'prop' is on a new line, can that be achieved through a tweak / setting?
Edit - I also tried Sublime, it worked half the time (producing an error randomly)

JSX formatter uses HTML code style preferences. Please try setting Wrap attributes: to Wrap always in Settings | Editor | Code Style | HTML - does it help?

Related

Variabilizing the # from <mat-menu>

I'm currently trying to generate a dynamic menu from a JSON blob in my typescript files, Angular project.
I'm using the / component from Angular Material.
My json menu has this structure :
{
id: 0,
titreTranslate: 'menu-accueil',
isDescriptionRequired: true,
routerLink: '/accueil',
icon: faHome,
isAllowed: true,
hasSubOptions: false,
trigger: 'accueil'
}
My code look something like this :
<mat-toolbar-row class="navigation-row">
<span *ngFor="let option of menuOptions">
<button mat-button
[matMenuTriggerFor]="admin"
routerLink="{{option.routerLink}}"
(keyup.enter)="router.navigate([option.routerLink], { queryParams: option.queryParams })"
[routerLinkActive]="['active-menu']"
[queryParams]="option.queryParams"
class="link bouton-menu-gauche flex-row"
*ngIf="option.isAllowed"
>
<fa-icon
*ngIf="option.icon"
class="primary-color"
[icon]="option.icon"></fa-icon>
{{ option.titreTranslate | translate }}
</button>
<mat-menu #{{option.trigger}}="matMenu">
<span *ngFor="let subOption of option.menuOptions">
<button mat-menu-item
*ngIf="option.menuOptions"
routerLink="{{subOption.routerLink}}"
(keyup.enter)="router.navigate([subOption.routerLink], { queryParams: subOption.queryParams })"
[routerLinkActive]="['active-menu']"
[queryParams]="subOption.queryParams"
class="link bouton-menu-gauche flex-row">
<fa-icon
*ngIf="subOption.icon"
class="primary-color"
[icon]="subOption.icon"></fa-icon>
{{ subOption.titreTranslate | translate }}
</button>
</span>
</mat-menu>
</span>
</mat-toolbar-row>
The line with " <mat-menu #{{option.trigger}}="matMenu"> " is what concerns me here ; I've tried many ways to variabilize this #, such as putting it directly in the Json menu or trying different syntax ; It always fail, and won't give me the code structure i want.
If i had to guess, i'd say the generated code should look like : <mat-menu #"accueil"="matMenu">, with "" that fail to compile ; but i don't get any compilation errors, so i'm lost here.
Does anybody had to work with that kind of structure before ?
(apologies for my english if it's bad, i'm french)
Check out this answer:
https://stackoverflow.com/a/44441164/2025458
It's easier to search for the answer if you call things by the name they are given in the documentation, the '#' is a template reference or template variable
And since it's inside a structural directive (*ngfor or any other directive strating with *)
It binds to a template created by the structural directive, so every loop of the ngfor generates its own nested template with its own instance of the variable, so you can just use one name

Material UI tabs , Not able to add the text-overflow elipses to the span tag

I am new to the material UI. here, I have two tabs and that tabs has some content.
Now, I am trying to add the text overflow elipses to this . But the text is in span over there,
<Tab
key={`${tab}_${index}`}
classes={{
root: css.tabRoot,
selected: css.tabSelected,
wrapper: css.tabIconWrapper,
labelIcon: css.tabLabelIcon
}}
disableRipple
label={tab.label}
value={tab.value}
icon={
tab.icon ? <Icons className={css.tabIcons} iconname={tab.icon} /> : null
}
/>
Now, Here the label is having a bit more length . So, I am just trying to limit it as max-width : 100px;
But in the material UI it is getting a bit complicated to override .
SO, what I tried is ,
label={<Typography className={selectedTab ? '' : ''}>{tab.label}</Typography>}
But here
I am not able to get which tab has been selected , I mean I want to add that class if the tab is selected.
can any one help me with this ? any solution like with or without typography works for me.
thanks.
div class="MuiTabs-scroller MuiTabs-fixed" role="tablist" style="overflow: hidden;">
<div class="MuiTabs-flexContainer">
<button class="MuiButtonBase-root MuiTab-root VIP_tabRoot MuiTab-textColorInherit Mui-selected VIP_tabSelected" tabindex="0" type="button" role="tab" aria-selected="true">
<span class="MuiTab-wrapper VIP_tabIconWrapper">Test004</span>
</button>
</div>
</div>

Trying to find unique XPath of button

I have a case where XPath is not unique and matches 3 elements on the page whose elementary position changes with refresh:
<div class="col-xs-12 Hover">
<button data-testid="continueCheckoutButton" ng-
class="continueDellMetricsClass" ng-click="continueButtonClick()" ng-
disabled="disableContinueButton" class="btn btn-success btn-block
continueButton" data-metrics="" type="button">Checkout</button>
Please help me finding the unique XPath or CSS path of this button element.
The other two HTML is as follows:
<div class="col-xs-12">
<button data-testid="continueCheckoutButton" ng-
class="continueDellMetricsClass" ng-click="continueButtonClick()" ng-
disabled="disableContinueButton" class="btn btn-success btn-block
continueButton" data-metrics="" type="button" style="background:
rgb(204, 136, 136); border: 2px solid red;">Checkout</button>
<div>
<button ng-class="continueDellMetricsClass" ng-
click="continueButtonClick()" ng-disabled="disableContinueButton"
class="btn btn-success btn-block continueButton" data-
testid="continueCheckoutButton" data-metrics="" type="button"
style="background: rgb(204, 136, 136); border: 2px solid
red;">Checkout</button>
</div>
This is what results in 3 elements match:
//button[#data-testid = 'continueCheckoutButton']
Please help!
To distinguish target button from two other you can try:
//button[not(#style) and .="Checkout"]
P.S. If HTML for all nodes seem to be identical, you can use index of required one:
(//button[not(#style) and .="Checkout"])[1]
Note that in XPath indexation starts from 1, so index for the first node will be [1].
You can also use find_elements...() instead of find_element...() to get a list of elements and select required with [index].
To click on the button with text as Checkout you have to induce wait through WebDriverWait and you can use either of the following code block (Python) :
CSS_SELECTOR :
button.btn.btn-success.btn-block.continueButton[ng-class='continueDellMetricsClass'][data-testid='continueCheckoutButton']
XPATH :
//button[#class='btn btn-success btn-block continueButton' and contains(.,'Checkout')]
Note : As the element is a Angular element you have to induce proper WebDriverWait through your respective Selenium Language Binding Art.
Update
As per your updated HTML you can use the following Locator Strategy :
XPATH :
//button[#class='btn btn-success btn-block continueButton' and contains(.,'Checkout') and not (#style='background')]
//div[#class='col-xs-12']/button[#class='btn btn-success btn-block continueButton' and contains(.,'Checkout')]
Try this.

How to write a custom form helper template for dynamically generated content?

I have some sort of quiz system, where to user gets shown a question and several answer-options with radio-buttons.
But as I am using a helper for a inputRadioGroup that gets filled via a list, it does not look pretty anymore (like Twitter Bootstrap). The radiobuttons are inline, while they should be underneath each other. And actually I would like to change the icon to a prettier button.
This is how it looks at the moment:
Therefore I tried to write my own custom form helper, but keep getting stuck. I find it frustratingly hard to understand the documentation for this:
https://www.playframework.com/documentation/2.3.x/JavaFormHelpers
First I created a new template named myFieldConstructorTemplate.scala.html
#(elements: helper.FieldElements)
<div class="#if(elements.hasErrors) {error}">
<label for="#elements.id">#elements.label</label>
<div class="input">
#elements.input
<span class="errors">#elements.errors.mkString(", ")</span>
<span class="help">#elements.infos.mkString(", ")</span>
</div>
</div>
Saved it to the /views-folder. Then try to use it in my view class quiz.scala.html:
#import helper._
#import helper.twitterBootstrap._
#(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])
#helper.form(action = routes.Application.nextQuizPage(), 'id -> "answerRadioForm"){
#helper.inputRadioGroup(
answerRadioForm("Answer"),
options = answerList.map(answer => answer.answerID.toString -> answer.answerText),
'_label -> "Answer",
'_error -> answerRadioForm("answerID").error.map(_.withMessage("select answer")))
<button type="submit" class="btn btn-default" value="Send">
Next Question
</button>
}
#implicitField = #{ FieldConstructor(myFieldConstructorTemplate.f) }
#inputText(answerRadioForm("questionID"))
If I put this into my template, I get a not found: value implicitField-error.
So how can I manage to change the appearance of my radiobuttons to underneath and looking like Twitter Bootstrap?
[EDIT1]: I have changed the order of the imports to the suggested version:
#(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])
#import helper._
#implicitField = #{ FieldConstructor(myFieldConstructorTemplate.f) }
#import helper.twitterBootstrap._
I get this error then:
ambiguous implicit values:
both method implicitField of type => views.html.helper.FieldConstructor
and value twitterBootstrapField in package twitterBootstrap of type
=> views.html.helper.FieldConstructor match expected type
views.html.helper.FieldConstructor
I think this has to do with the way I import the answers into the radiobuttons?
[EDIT2]:
The order of the imports is now:
#(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])
#import models.Question
#import models.Answer
#import helper._
#implicitField = #{ FieldConstructor(myFieldConstructorTemplate.f) }
With this, the program compiles. BUT the radiobuttons still look the same. So I tried to change the design, which does not quite work. It looks now like all the radiobuttons are melted into a single one:
Here is my template class myFieldConstructorTemplate.scala.html:
#(elements: helper.FieldElements)
<div class="btn-group", data-toggle="buttons">
<label for="#elements.id">#elements.label</label>
<div class="input">
<label class="btn btn-primary">
#elements.input
<span class="errors">#elements.errors.mkString(", ")</span>
<span class="help">#elements.infos.mkString(", ")</span>
</label>
</div>
</div>
[EDIT3]: I have changed my class according to the last answer, but still the radiobuttons are melted into each other. So I want to point out that I am not fixated on using the inputRadioGroup from the playframework helper, if there is another solution that works the same and looks almost like bootstrap, I would gladly use that. It seems that changing the helper isnt that easy / intuitive. I appreciate any form of help!
The Twitter Bootstrap structure needs to be accurate.
Wrap the btn-group class around the inputRadioGroup helper like so:
<div class="btn-group" data-toggle="buttons">
#helper.inputRadioGroup(
answerRadioForm("Answer"),
options = answerList.map(answer => answer.answerID.toString -> answer.answerText),
'_label -> "Answer",
'_error -> answerRadioForm("answerID").error.map(_.withMessage("select answer")))
</div>
Then replace the template with:
#(elements: helper.FieldElements)
<label class="btn btn-primary">
#elements.input #elements.label
</label>
<span class="errors">#elements.errors.mkString(", ")</span>
<span class="help">#elements.infos.mkString(", ")</span>
In general, perhaps it'd be of interest another way of doing it. When you use fooForm("myField"...), you can use fooForm("myField[#i]"...) in a for loop where #i is a counter going from 0 to however many inputs there are. Then you can yourself sketch out the full HTML instead of using implicit values.
By the way, the documentation with the Scala version about all this has lots more information than the Java version. See here. It has more information on inputRadioGroup than the Java version of the documentation but still very useful reading for better understanding of all this.
Some Play Bootstrap plugin has code available on GitHub that is also useful reading especially as it uses implicit values also.
UPDATE
Here's a couple of GitHub projects showing how to achieve this:
with Scala version of Play: https://github.com/bjfletcher/play-bootstrap-radio-group
with Java version of Play: https://github.com/bjfletcher/play-java-bootstrap-radio-group
Screenshot of the result:
Move the implicitField-definition to the top of the file:
#(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])
#import helper._
#implicitField = #{ FieldConstructor(myFieldConstructorTemplate.f) }
#import helper.twitterBootstrap._
#helper.form(action = routes.Application.nextQuizPage(), 'id -> "answerRadioForm"){
#helper.inputRadioGroup(
answerRadioForm("Answer"),
options = answerList.map(answer => answer.answerID.toString -> answer.answerText),
'_label -> "Answer",
'_error -> answerRadioForm("answerID").error.map(_.withMessage("select answer")))
<button type="submit" class="btn btn-default" value="Send">
Next Question
</button>
}
#inputText(answerRadioForm("questionID"))
This makes sure the implicit value is available where it's needed.
Always keep params on top of template and remove the following import statement #import helper.twitterBootstrap._ this will conflict with your own field constructor.
#(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])
#import helper._
#implicitField = #{ FieldConstructor(myFieldConstructorTemplate.f) }
Hope it resolves your issue.

Zend Framework Form Element

I have this code:
<button type="submit" name="submit" class="btn green pull-right">
Potvrdi <i class="m-icon-swapright m-icon-white"></i>
</button>
How can I make Zend_Form_Element_Button with these attributes? (including tag, it is an icon that goes with the text "Potvrdi" as label on button)
I have done this so far:
$submit = new Zend_Form_Element_Button('submit');
$submit ->setLabel('Potvrdi')
->setAttrib('type', 'submit')
->setAttrib('class', 'btn green pull-right');
Thank you.
The quickest solution is to disable escaping for the label and include the HTML code directly in the label:
$submit = new Zend_Form_Element_Button('submit');
$submit ->setLabel('Potvrdi <i class="m-icon-swapright m-icon-white"></i>')
->setAttrib('type', 'submit')
->setAttrib('class', 'btn green pull-right')
->setAttrib('escape', false);
However, If you plan to use this type of button often in your source code, you should consider writing your own Zend_Form_Element (e.g. My_Form_Element_IconButton) that takes care of adding these tags.