Skip to content

docs: added note on how to avoid the incorrectly floating label in a select with empty value but non-empty text #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/site/src/routes/demo/select/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
<Demo component={Keys} file="select/_Keys.svelte">
Using Keys
<svelte:fragment slot="subtitle">
If your options aren't strings, you must provide a <code>key</code> function
that converts them to unique strings, or the label may misbehave.
If your options aren't strings, you must provide a <code>key</code>
function that converts them to unique strings, or the label may misbehave.
<br />
The only exception to this is if the text is non-empty while the value is empty.
In this case, due to a limitation in <code>@material/select</code>, the
label will float over the selected text.
</svelte:fragment>
</Demo>

Expand Down Expand Up @@ -53,6 +57,18 @@
<Demo component={ConditionalIcon} file="select/_ConditionalIcon.svelte">
Conditional icon
</Demo>

<Demo component={DefaultValue} file="select/_DefaultValue.svelte">
Default Values
<svelte:fragment slot="subtitle">
Due to a limitation of <code>@material/select</code>, the floating label
will display incorrectly if the selected option has <code>value=""</code>
but its label is non-empty.
<br />
In the case where you need a default value, set it to a non-empty string like
in the second example below.
</svelte:fragment>
</Demo>
</section>

<script lang="ts">
Expand All @@ -69,6 +85,7 @@
import Required from './_Required.svelte';
import Disabled from './_Disabled.svelte';
import ConditionalIcon from './_ConditionalIcon.svelte';
import DefaultValue from './_DefaultValue.svelte';
</script>

<style>
Expand Down
39 changes: 39 additions & 0 deletions packages/site/src/routes/demo/select/_DefaultValue.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<div class="columns margins">
<div>
<Select
key={(value) => `${value == null ? '' : value}`}
bind:value={valueA}
label="Wood type"
>
<Option value="">All</Option>
{#each woodTypes as type}
<Option value={type}>{type}</Option>
{/each}
</Select>

<pre class="status">Selected: {valueA}</pre>
</div>
<div>
<Select
key={(value) => `${value == null ? '' : value}`}
bind:value={valueB}
label="Wood type"
>
<Option value="all">All</Option>
{#each woodTypes as type}
<Option value={type}>{type}</Option>
{/each}
</Select>

<pre class="status">Selected: {valueB}</pre>
</div>
</div>

<script lang="ts">
import Select, { Option } from '@smui/select';

let woodTypes = ['Mahogany', 'Oak', 'Walnut', 'Pine'];

let valueA: string;
let valueB: string = 'all';
</script>