Skip to content

Commit e4ce528

Browse files
committed
Document prop directive and item list selector
1 parent a79d608 commit e4ce528

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

Diff for: docs_src/content/docs/20-utilities.md

+62-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ For a concrete example of this pattern see svelte native's [listView element sou
3636

3737
### Property Element
3838

39-
Some NativeScript controls have properties that expect NativeScript views as values. To make this possible using markup, Svelte Native introduces the property element. This element works like the ones in the NativeScript core documentation and set some property of their parent view with the value of the first child of the property element. The tag name is the name of the parent element followed by the property name. For example `<page.actionbar>` would set the `actionbar` property of the parent `page` element.
39+
Some NativeScript controls have properties that expect NativeScript views as values. To make this possible using markup, Svelte Native introduce two helpers, the property element and the `prop` directive.
40+
41+
This property element works like the ones in the NativeScript core documentation and set some property of their parent view with the value of the first child of the property element. The tag name is the name of the parent element followed by the property name. For example `<page.actionbar>` would set the `actionbar` property of the parent `page` element.
4042

4143
#### An Example
4244

@@ -75,4 +77,63 @@ onMount(() => {
7577
drawer.nativeView.mainContent = mainContent.nativeView
7678
drawer.nativeView.drawerContent = drawerContent.nativeView
7779
})
80+
```
81+
82+
### Property Directive
83+
84+
The Property Element is useful but can be a little verbose. Svelte native also introduces a custom svelte directive called `prop`. The prop directive will take the native view of the component it is on an assign it to a property of the parent element.
85+
86+
For example our side drawer example from Property Element
87+
```html
88+
<radSideDrawer>
89+
<radSideDrawer.drawerContent>
90+
<stackLayout />
91+
</radSideDrawer.drawerContent>
92+
<radSideDrawer.mainContent>
93+
<stackLayout />
94+
</radSideDrawer.mainContent>
95+
</radSideDrawer>
96+
```
97+
98+
can be written using the prop element as
99+
100+
```html
101+
<radSideDrawer>
102+
<stackLayout prop:drawerContent />
103+
<stackLayout prop:mainContent/>
104+
</radSideDrawer>
105+
```
106+
107+
### Implicit Property Directives
108+
109+
Many advanced controls (including those in the nativescript-ui packages) use elements to provide configuration. These configuration properties need to be assigned to a parent property but often only have one valid parent property to which they can be assigned, so the `prop:` or Property Element becomes boilerplate
110+
111+
Take this example from `nativescript-ui-dataform`:
112+
113+
```html
114+
<radDataForm source={ticket} metadata={ticketMetadata}>
115+
<entityProperty prop:properties name="price" index="4" readOnly="true">
116+
<propertyEditor prop:editor type="Decimal" />
117+
</entityProperty>
118+
<entityProperty prop:properties name="numberOfTickets" displayName="Number of Tickets" index="5">
119+
<propertyEditor prop:editor type="Stepper">
120+
<propertyEditorParams prop:params minimum="0" maximum="100" step="2" />
121+
</propertyEditor>
122+
</entityProperty>
123+
</radDataForm>
124+
```
125+
126+
on a large form, the `prop:properties` `prop:editor` and `prop:params` can get repetitive. Svelte Native lets you register a configuration element with a default property name for the `prop:` directive. When this is set, the `prop:` directive is not needed at all:
127+
128+
```html
129+
<radDataForm source={ticket} metadata={ticketMetadata}>
130+
<entityProperty name="price" index="4" readOnly="true">
131+
<propertyEditor type="Decimal" />
132+
</entityProperty>
133+
<entityProperty name="numberOfTickets" displayName="Number of Tickets" index="5">
134+
<propertyEditor type="Stepper">
135+
<propertyEditorParams minimum="0" maximum="100" step="2" />
136+
</propertyEditor>
137+
</entityProperty>
138+
</radDataForm>
78139
```

Diff for: docs_src/content/docs/40-components.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -334,27 +334,25 @@ function onItemTap(event) {
334334
335335
Multiple templates can be used for different layouts of list items, which is similar to [NativeScript Multiple Templates Selector Function](https://docs.nativescript.org/ui/components/list-view#multiple-templates-selector-function). `itemTemplateSelector` is used to select template using `key` which is returned based on your logic.
336336
337-
> **Note:** Use _multiple templates_ approach instead of `{#if}` block, because the _view recycling_ concept does not work as intended since `<listView>` cannot reuse the same template view if off-screen items require different type of template.
337+
> **Note:** Use _multiple templates_ approach instead of `{#if}` block, because the _view recycling_ concept does not work as intended since `<listView>` cannot reuse the same template view if off-screen items require different type of template. [See this article](https://medium.com/@alexander.vakrilov/faster-nativescript-listview-with-multiple-item-templates-8f903a32e48f)
338338
339339
```html
340-
<listView bind:this="{test_subject}" items={items} itemTemplateSelector={selectTemplate}>
340+
<listView items={items} itemTemplateSelector={selectTemplate}>
341341
<Template let:item key="odd">
342342
<label text="Odd {item}" />
343343
</Template>
344344
<Template let:item key="even">
345345
<label text="Even {item}" />
346346
</Template>
347347
</listView>
348-
```
349-
```js
348+
350349
<script>
351-
import { Template } from 'svelte-native/components'
352-
export let test_subject;
353-
let items = ["item 0", "item 1"]
354-
function selectTemplate(item, index, items) {
355-
// Your logic here
356-
return (index % 2 == 0) ? "even" : "odd";
357-
}
350+
import { Template } from 'svelte-native/components'
351+
let items = ["item 0", "item 1"]
352+
function selectTemplate(item, index, items) {
353+
// Your logic here
354+
return (index % 2 == 0) ? "even" : "odd";
355+
}
358356
</script>
359357
```
360358
@@ -366,6 +364,7 @@ function selectTemplate(item, index, items) {
366364
|------|------|-------------|
367365
| `items` | `Array<any>` | An array of items to be shown in the `<listView>`.
368366
| `separatorColor` | `Color` | Sets the separator line color. Set to `transparent` to remove it.
367+
| `itemTemplateSelector` | `(item,index,items) => string` | (optional) returns the key to the template to use for the provided item.
369368
370369
#### Events
371370

0 commit comments

Comments
 (0)