Skip to content

Commit

Permalink
feat: drop get value for simpler API
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Aug 30, 2024
1 parent f4f8307 commit b6c8b98
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/useSelect/useOption.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { axe } from 'vitest-axe';
test('warns if no Selection or ListBox Context is provided', async () => {
const warn = vi.spyOn(console, 'warn');
await renderSetup(() => {
return useOption({ label: 'Ayooo', option: '' });
return useOption({ label: 'Ayooo', value: '' });
});

expect(warn).toHaveBeenCalledTimes(2);
Expand All @@ -19,7 +19,7 @@ test('should not have a11y errors', async () => {
await render({
setup() {
const label = 'Field';
const { optionProps } = useOption({ label, option: '' });
const { optionProps } = useOption({ label, value: '' });

return {
optionProps,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/useSelect/useOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface OptionDomProps {

export interface OptionProps<TValue> {
label?: string;
option: TValue;
value: TValue;

disabled?: boolean;
}
Expand All @@ -44,7 +44,7 @@ export function useOption<TOption>(_props: Reactivify<OptionProps<TOption>>, ele
}

function getValue() {
return selectionCtx?.evaluateOption(toValue(props.option));
return toValue(props.value);
}

const optionId = useUniqId(FieldTypePrefixes.Option);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/useSelect/useSelect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function createSelect() {
<OptionItem
v-for="(option, idx) in group.items"
:key="(getValue?.(option)) ?? idx"
:option="option"
:value="option"
:label="option.label"
:disabled="!!option.disabled"
>
Expand All @@ -99,7 +99,7 @@ function createSelect() {
<OptionItem
v-for="(option, idx) in options"
:key="(getValue?.(option)) ?? idx"
:option="option"
:value="option"
:label="option.label"
:disabled="!!option.disabled"
>
Expand Down
10 changes: 2 additions & 8 deletions packages/core/src/useSelect/useSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export interface SelectProps<TOption, TValue = TOption> {
name?: string;
description?: string;

getValue?(option: TOption): TValue;
modelValue?: Arrayable<TValue>;

disabled?: boolean;
Expand All @@ -42,19 +41,15 @@ export interface SelectionContext<TOption, TValue = TOption> {
isValueSelected(value: TValue): boolean;
isMultiple(): boolean;
toggleValue(value: TValue, force?: boolean): void;
evaluateOption(option: TOption): TValue;
}

export const SelectionContextKey: InjectionKey<SelectionContext<unknown>> = Symbol('SelectionContextKey');

const MENU_OPEN_KEYS = ['Enter', 'Space', 'ArrowDown', 'ArrowUp'];

export function useSelect<TOption, TValue = TOption>(
_props: Reactivify<SelectProps<TOption, TValue>, 'schema' | 'getValue'>,
) {
export function useSelect<TOption, TValue = TOption>(_props: Reactivify<SelectProps<TOption, TValue>, 'schema'>) {
const inputId = useUniqId(FieldTypePrefixes.Select);
const props = normalizeProps(_props, ['schema', 'getValue']);
const evaluate = props.getValue || ((opt: TOption) => opt as unknown as TValue);
const props = normalizeProps(_props, ['schema']);
const field = useFormField<Arrayable<TValue>>({
path: props.name,
initialValue: toValue(props.modelValue) as Arrayable<TValue>,
Expand Down Expand Up @@ -99,7 +94,6 @@ export function useSelect<TOption, TValue = TOption>(

const selectionCtx: SelectionContext<TOption, TValue> = {
isMultiple: () => toValue(props.multiple) ?? false,
evaluateOption: evaluate,
isValueSelected(value): boolean {
const values = normalizeArrayable(fieldValue.value ?? []);

Expand Down

0 comments on commit b6c8b98

Please sign in to comment.