Skip to content
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

Improve getLabel functions #1311

Merged
merged 2 commits into from
Apr 26, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
export let label: string | undefined = undefined;

export let labelKey = 'label';
let customGetLabel: LabelGetter<Option> | undefined = undefined;
export { customGetLabel as getLabel };
export let getLabel: LabelGetter<Option> = (o: Option) =>
defaultGetLabel(o, labelKey);

export let getDisabled: (value: Option | undefined) => boolean = () => false;

$: getLabel = customGetLabel ?? ((o: Option) => defaultGetLabel(o, labelKey));
</script>

<fieldset class="fieldset-group" class:inline={isInline} on:change>
Expand Down
5 changes: 2 additions & 3 deletions mathesar_ui/src/component-library/list-box/ListBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
export let searchable: DefinedProps['searchable'] = false;
export let disabled: DefinedProps['disabled'] = false;
export let labelKey: DefinedProps['labelKey'] = 'label';
export let getLabel: DefinedProps['getLabel'] = defaultGetLabel;
export let getLabel: DefinedProps['getLabel'] = (option: Option) =>
defaultGetLabel(option, labelKey);
export let checkEquality: DefinedProps['checkEquality'] = (
opt: Option,
opt2: Option,
Expand Down Expand Up @@ -231,7 +232,6 @@

const staticProps: Writable<ListBoxStaticContextProps<Option>> = writable({
selectionType,
labelKey,
getLabel,
searchable,
disabled,
Expand All @@ -240,7 +240,6 @@
});
$: staticProps.set({
selectionType,
labelKey,
getLabel,
searchable,
disabled,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { onMount, getContext, tick } from 'svelte';
import StringOrComponent from '../string-or-component/StringOrComponent.svelte';
import type { ListBoxContext } from './ListBoxTypes';

type Option = $$Generic;
Expand Down Expand Up @@ -62,7 +63,7 @@
class:in-focus={index === $focusedOptionIndex}
on:click={() => api.pick(option)}
>
<span>{$staticProps.getLabel(option, $staticProps.labelKey)}</span>
<StringOrComponent arg={$staticProps.getLabel(option)} />
</li>
{/each}
</ul>
5 changes: 3 additions & 2 deletions mathesar_ui/src/component-library/list-box/ListBoxTypes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Readable } from 'svelte/store';
import type CancellablePromise from '@mathesar-component-library-dir/common/utils/CancellablePromise';
import type { LabelGetter } from '@mathesar-component-library-dir/common/utils/formatUtils';

export interface ListBoxStaticContextProps<Option> {
selectionType: 'single' | 'multiple';
labelKey: string;
getLabel: (value: Option, labelKey?: string) => string;
getLabel: LabelGetter<Option>;
searchable: boolean;
disabled: boolean;
checkEquality: (option: Option, optionToCompare: Option) => boolean;
Expand All @@ -13,6 +13,7 @@ export interface ListBoxStaticContextProps<Option> {

export interface ListBoxProps<Option>
extends Partial<ListBoxStaticContextProps<Option>> {
labelKey?: string;
options: Option[] | (() => CancellablePromise<Option[]>);
value?: Option[];
}
Expand Down
13 changes: 7 additions & 6 deletions mathesar_ui/src/component-library/select/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
ListBoxOptions,
} from '@mathesar-component-library-dir/list-box';
import { Dropdown } from '@mathesar-component-library-dir/dropdown';
import type { LabelGetter } from '@mathesar-component-library-dir/common/utils/formatUtils';
import { getLabel as defaultGetLabel } from '@mathesar-component-library-dir/common/utils/formatUtils';
import type { Appearance } from '@mathesar-component-library-dir/types';
import { getGloballyUniqueId } from '@mathesar-component-library-dir/common/utils/domUtils';
import StringOrComponent from '../string-or-component/StringOrComponent.svelte';

type Option = $$Generic;

Expand All @@ -23,6 +25,10 @@
*/
export let labelKey = 'label';

export let getLabel: LabelGetter<Option | undefined> = (
o: Option | undefined,
) => defaultGetLabel(o, labelKey);

/**
* List of options to select from. Must be an array of SelectOption.
* @required
Expand Down Expand Up @@ -53,11 +59,6 @@
*/
export let ariaLabel: string | undefined = undefined;

export let getLabel: (
value: Option | undefined,
labelKey?: string,
) => string = defaultGetLabel;

/**
* By default, options will be compared by equality. If you're using objects as
* options, you can supply a custom function here to compare them.
Expand Down Expand Up @@ -119,7 +120,7 @@
on:keydown={(e) => api.handleKeyDown(e)}
>
<svelte:fragment slot="trigger">
{getLabel(value, labelKey)}
<StringOrComponent arg={getLabel(value)} />
</svelte:fragment>

<svelte:fragment slot="content">
Expand Down