Skip to content
This repository was archived by the owner on Sep 26, 2022. It is now read-only.

Commit 0f6b600

Browse files
Merge pull request #190 from TheComputerM/feat/show-select-name-and-empty
Feat/show select name and empty
2 parents c520de3 + f9daf57 commit 0f6b600

File tree

9 files changed

+52
-29
lines changed

9 files changed

+52
-29
lines changed

packages/docs/src/components/doc/Playground.svelte

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
SlideGroup,
1212
SlideItem,
1313
Ripple,
14-
ListItem,
1514
} from 'svelte-materialify/src';
1615
import { mdiInvertColors } from '@mdi/js';
1716
@@ -107,15 +106,6 @@
107106
val.join(', ') :
108107
val)}>
109108
{label}
110-
<div slot="item" let:item>
111-
{#if controls[label].format}
112-
<ListItem value={item.value}>
113-
{item.name}
114-
</ListItem>
115-
{:else}
116-
<ListItem value={item}>{item}</ListItem>
117-
{/if}
118-
</div>
119109
</Select>
120110
{:else if controls[label].type === 'text'}
121111
<TextField bind:value={values[label]} class="mt-4">{label}</TextField>

packages/docs/src/examples/Select/basic.svelte

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<script>
22
import { Row, Col, Select } from 'svelte-materialify/src';
33
4-
const items = [
5-
{ name: 'Foo', value: 'foo' },
6-
{ name: 'Bar', value: 'bar' },
7-
{ name: 'Fizz', value: 'fizz' },
8-
{ name: 'Buzz', value: 'buzz' },
9-
];
4+
const items = ['foo', 'bar', 'fizz', 'buzz'];
105
</script>
116

127
<Row>

packages/docs/src/examples/Select/chips.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{ name: 'Fizz', value: 'fizz' },
88
{ name: 'Buzz', value: 'buzz' },
99
];
10+
const items2 = ['foo', 'bar', 'fizz', 'buzz'];
1011
</script>
1112

1213
<Row>
@@ -15,7 +16,7 @@
1516
<Select chips filled {items}>Filled</Select>
1617
</Col>
1718
<Col>
18-
<Select chips multiple {items}>Multiple</Select>
19-
<Select chips outlined {items}>Outlined</Select>
19+
<Select chips multiple items={items}>Multiple</Select>
20+
<Select chips multiple outlined items={items2}>Multiple Outlined</Select>
2021
</Col>
2122
</Row>

packages/docs/src/examples/Select/multiple.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{ name: 'Fizz', value: 'fizz' },
88
{ name: 'Buzz', value: 'buzz' },
99
];
10+
const items2 = ['foo', 'bar', 'fizz', 'buzz'];
1011
</script>
1112

1213
<Row>
@@ -15,7 +16,7 @@
1516
<Select multiple filled {items}>Filled</Select>
1617
</Col>
1718
<Col>
18-
<Select multiple solo {items} placeholder="Solo" />
19-
<Select multiple outlined {items}>Outlined</Select>
19+
<Select multiple solo items={items2} placeholder="Solo" />
20+
<Select multiple outlined items={items2}>Outlined</Select>
2021
</Col>
2122
</Row>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
import { Row, Select } from 'svelte-materialify/src';
3+
4+
const items = [
5+
{ name: 'Foo!', value: 'foo' },
6+
{ name: 'Bar!', value: 'bar' },
7+
{ name: 'Fizz!', value: 'fizz' },
8+
{ name: 'Buzz!', value: 'buzz' },
9+
];
10+
let value;
11+
</script>
12+
13+
<Row noGutters class="mt-2">
14+
<Select {items} bind:value>Regular</Select>
15+
<p>chosen value: {value}</p>
16+
</Row>

packages/docs/src/playground/Chip.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
icon: {
2020
type: 'select',
2121
items,
22-
format: (val) => items.find((i) => i.value == val).name, // eslint-disable-line eqeqeq
22+
format: (val) => (items.find((i) => i.value == val) ? items.find((i) => i.value == val).name : ''), // eslint-disable-line eqeqeq
2323
},
2424
size: {
2525
type: 'select',
2626
items: ['x-small', 'small', 'default', 'large', 'x-large'],
27+
mandatory: true,
2728
},
2829
};
2930

packages/docs/src/playground/Icon.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
icon: {
1616
type: 'select',
1717
items,
18-
format: (val) => items.find((i) => i.value == val).name, // eslint-disable-line eqeqeq
18+
format: (val) => (items.find((i) => i.value == val) ? items.find((i) => i.value == val).name : ''), // eslint-disable-line eqeqeq
1919
},
2020
color: { type: 'select', items: ['red', 'green', 'blue', 'indigo', 'purple'] },
2121
};

packages/docs/src/routes/components/selects.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ Below is a collection of simple to complex examples.
2424
This is a basic example on how to use `Select`.
2525
<Components.Example file="Select/basic" />
2626

27+
### Name-Value
28+
29+
If you want the `name` that the user sees to differ from the `value` in code, your items should be defined like this:
30+
```js
31+
const items = [
32+
{ name: 'Foo!', value: 'foo' },
33+
{ name: 'Bar!', value: 'bar' }
34+
]
35+
```
36+
as an example:
37+
<Components.Example file="Select/namevalue" />
38+
2739
### Disabled
2840

2941
You cannot use disabled `Select`.

packages/svelte-materialify/src/components/Select/Select.svelte

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
export let chips = false;
2626
export let disabled = null;
2727
export let closeOnClick = !multiple;
28-
export let format = (val) => (Array.isArray(val) ? val.join(', ') : val);
28+
export let emptyString = '';
29+
const getSelectString = (v) => {
30+
// We could also use `return items[0].value ? find.. : v` or provide a `basic` prop
31+
const item = items.find((i) => i.value === v);
32+
return item ? (item.name ? item.name : item) : (v || emptyString);
33+
};
34+
export let format = (val) => (Array.isArray(val) ? val.map((v) => getSelectString(v)).join(', ') : getSelectString(val));
35+
2936
const dispatch = createEventDispatcher();
3037
$: dispatch('change', value);
3138
</script>
@@ -50,10 +57,10 @@
5057

5158
<slot />
5259
<div slot="content">
53-
{#if chips}
60+
{#if chips && value}
5461
<span class="s-select__chips">
55-
{#each Array.isArray(value) ? value : [value] as v}
56-
<Chip>{v}</Chip>
62+
{#each Array.isArray(value) ? value.map((v) => getSelectString(v)) : [getSelectString(value)] as val}
63+
<Chip>{val}</Chip>
5764
{/each}
5865
</span>
5966
{/if}
@@ -67,13 +74,13 @@
6774
<ListItemGroup bind:value {mandatory} {multiple} {max}>
6875
{#each items as item}
6976
<slot name="item" {item}>
70-
<ListItem {dense} value={item.value}>
77+
<ListItem {dense} value={item.value ? item.value : item}>
7178
<span slot="prepend">
7279
{#if multiple}
73-
<Checkbox checked={value.includes(item.value)} />
80+
<Checkbox checked={value.includes(item.value ? item.value : item)} />
7481
{/if}
7582
</span>
76-
{item.name}
83+
{item.name ? item.name : item}
7784
</ListItem>
7885
</slot>
7986
{/each}

0 commit comments

Comments
 (0)