Skip to content

Commit

Permalink
Merge pull request #1311 from centerofci/refactor_getLabel
Browse files Browse the repository at this point in the history
Improve getLabel functions
  • Loading branch information
seancolsen authored Apr 26, 2022
2 parents 5a21488 + 108e5b4 commit e2fedad
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
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

0 comments on commit e2fedad

Please sign in to comment.