Skip to content

Commit 87c98a3

Browse files
Fix styleguide - part 2 (#2161)
* fix: fixed a rule for prop casing * fix: added CAPI example to simple expressions * chore: added CAPI examples for computed * fix: made notes about consistency
1 parent 24a53b9 commit 87c98a3

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

src/style-guide/rules-strongly-recommended.md

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,36 +529,72 @@ components/
529529

530530
## Prop name casing {#prop-name-casing}
531531

532-
**Prop names should always use camelCase during declaration, but kebab-case in templates and [JSX](/guide/extras/render-function.html#jsx-tsx).**
533-
534-
We're simply following the conventions of each language. Within JavaScript, camelCase is more natural. Within HTML, kebab-case is.
532+
**Prop names should always use camelCase during declaration. When used inside in-DOM templates, props should be kebab-cased. Single-File Components templates and [JSX](/guide/extras/render-function.html#jsx-tsx) can use either kebab-case or camelCase props. Casing should be consistent - if you choose to use camelCased props, make sure you don't use kebab-cased ones in your application**
535533

536534
<div class="style-example style-example-bad">
537535
<h3>Bad</h3>
538536

537+
<div class="options-api">
538+
539539
```js
540540
props: {
541541
'greeting-text': String
542542
}
543543
```
544544

545+
</div>
546+
547+
<div class="composition-api">
548+
549+
```js
550+
const props = defineProps({
551+
'greeting-text': String
552+
})
553+
```
554+
555+
</div>
556+
545557
```vue-html
546-
<WelcomeMessage greetingText="hi"/>
558+
// for in-DOM templates
559+
<welcome-message greetingText="hi"></welcome-message>
547560
```
548561

549562
</div>
550563

551564
<div class="style-example style-example-good">
552565
<h3>Good</h3>
553566

567+
<div class="options-api">
568+
554569
```js
555570
props: {
556571
greetingText: String
557572
}
558573
```
559574

575+
</div>
576+
577+
<div class="composition-api">
578+
579+
```js
580+
const props = defineProps({
581+
greetingText: String
582+
})
583+
```
584+
585+
</div>
586+
560587
```vue-html
588+
// for SFC - please make sure your casing is consistent throughout the project
589+
// you can use either convention but we don't recommend mixing two different casing styles
561590
<WelcomeMessage greeting-text="hi"/>
591+
// or
592+
<WelcomeMessage greetingText="hi"/>
593+
```
594+
595+
```vue-html
596+
// for in-DOM templates
597+
<welcome-message greeting-text="hi"></welcome-message>
562598
```
563599

564600
</div>
@@ -629,6 +665,8 @@ Complex expressions in your templates make them less declarative. We should stri
629665
{{ normalizedFullName }}
630666
```
631667

668+
<div class="options-api">
669+
632670
```js
633671
// The complex expression has been moved to a computed property
634672
computed: {
@@ -642,6 +680,22 @@ computed: {
642680

643681
</div>
644682

683+
<div class="composition-api">
684+
685+
```js
686+
// The complex expression has been moved to a computed property
687+
const normalizedFullName = computed(() =>
688+
fullName.value
689+
.split(' ')
690+
.map((word) => word[0].toUpperCase() + word.slice(1))
691+
.join(' ')
692+
)
693+
```
694+
695+
</div>
696+
697+
</div>
698+
645699
## Simple computed properties {#simple-computed-properties}
646700

647701
**Complex computed properties should be split into as many simpler properties as possible.**
@@ -667,6 +721,8 @@ Simpler, well-named computed properties are:
667721
<div class="style-example style-example-bad">
668722
<h3>Bad</h3>
669723

724+
<div class="options-api">
725+
670726
```js
671727
computed: {
672728
price() {
@@ -681,9 +737,24 @@ computed: {
681737

682738
</div>
683739

740+
<div class="composition-api">
741+
742+
```js
743+
const price = computed(() => {
744+
const basePrice = manufactureCost.value / (1 - profitMargin.value)
745+
return basePrice - basePrice * (discountPercent.value || 0)
746+
})
747+
```
748+
749+
</div>
750+
751+
</div>
752+
684753
<div class="style-example style-example-good">
685754
<h3>Good</h3>
686755

756+
<div class="options-api">
757+
687758
```js
688759
computed: {
689760
basePrice() {
@@ -702,6 +773,24 @@ computed: {
702773

703774
</div>
704775

776+
<div class="composition-api">
777+
778+
```js
779+
const basePrice = computed(
780+
() => manufactureCost.value / (1 - profitMargin.value)
781+
)
782+
783+
const discount = computed(
784+
() => basePrice.value * (discountPercent.value || 0)
785+
)
786+
787+
const finalPrice = compued(() => basePrice.value - discount.value)
788+
```
789+
790+
</div>
791+
792+
</div>
793+
705794
## Quoted attribute values {#quoted-attribute-values}
706795

707796
**Non-empty HTML attribute values should always be inside quotes (single or double, whichever is not used in JS).**

0 commit comments

Comments
 (0)