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

feat: Implement useComboBox #102

Merged
merged 33 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
296962f
refactor: move listbox, option and option group into upper level
logaretm Jan 10, 2025
b043f59
fix: expose listbox id and remove activedecended from listbox aria attrs
logaretm Jan 10, 2025
d48030c
feat: implement combobox initial draft
logaretm Jan 10, 2025
d600c13
refactor: kill selection context and only rely on listbox manager
logaretm Jan 10, 2025
c1b0e31
refactor: move out the finder logic
logaretm Jan 11, 2025
6d2a413
feat: close popover on selection
logaretm Jan 11, 2025
3ef9004
feat: implement focus strategy
logaretm Jan 11, 2025
b21654f
feat: allow any type of options to be used
logaretm Jan 11, 2025
9eb023a
feat: rename popup props to listbox props
logaretm Jan 11, 2025
ccef7ab
feat: add filtering logic
logaretm Jan 12, 2025
5abb3df
refactor: simplify collection API
logaretm Jan 13, 2025
2ac69cd
fix: force selection of the currently selected option
logaretm Jan 13, 2025
a9b2d6e
refactor: several API improvements
logaretm Jan 13, 2025
f9675bc
fix: rename exposed options and items and improve the option API
logaretm Jan 15, 2025
2e81c59
feat: get rid of the index prop
logaretm Jan 15, 2025
1e51db9
fix: optimize the option enumeration
logaretm Jan 15, 2025
3b19be3
test: adjust tests to wait for next tick
logaretm Jan 15, 2025
fdcbe62
refactor: rename focus strategy values
logaretm Jan 15, 2025
2ce5c22
fix: simplify options selection attr logic
logaretm Jan 15, 2025
7382b1a
refactor: add option element type
logaretm Jan 15, 2025
f4be544
feat: use styling to hide options instead of DOM rendering
logaretm Jan 16, 2025
a4909c0
feat: minor cleanup of filter types and optimizations
logaretm Jan 16, 2025
37cbc12
feat: debounce the filtering to 100ms by default
logaretm Jan 16, 2025
e35e4ee
refactor: move the debounce to the filter object
logaretm Jan 16, 2025
97a80dc
refactor: make the debounceMs zero by default
logaretm Jan 16, 2025
5cb54dc
fix: add aria hidden as well
logaretm Jan 16, 2025
d8b71ea
feat: expose is list empty
logaretm Jan 16, 2025
2ba469e
fix: show aria-selected attr at all times
logaretm Jan 16, 2025
6e90f8e
fix: unfocus the field if the popover closes
logaretm Jan 16, 2025
7e416f6
feat: implement allow custom value
logaretm Jan 17, 2025
e7916d4
test: added filter API tests
logaretm Jan 17, 2025
be3a75b
test: add combobox tests
logaretm Jan 17, 2025
bad613d
chore: add changeset
logaretm Jan 17, 2025
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
5 changes: 5 additions & 0 deletions .changeset/hip-crabs-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@formwerk/core': minor
---

feat: implement useComboBox
38 changes: 38 additions & 0 deletions packages/core/src/collections/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// import { computed, ComputedRef, MaybeRefOrGetter, toValue } from 'vue';
// import { getFromPath } from '../utils/path';
// import { isObject } from '../../../shared/src';

// export interface CollectionInit<TItem> {
// /**
// * The items to be displayed in the collection.
// */
// items: MaybeRefOrGetter<TItem[]>;
// /**
// * The property to track by, it can be a function that extracts the value from the item. Should be the same as the "value" prop of the option.
// */
// key?: string | ((item: TItem) => unknown);
// }

// export interface CollectionManager<TItem> {
// items: ComputedRef<TItem[]>;
// key: (item: TItem) => unknown;
// }

// // TODO: Implement fetching, loading, pagination, adding a new item, etc...
// export function defineCollection<TItem>(init: CollectionInit<TItem>): CollectionManager<TItem> {
// const { items, key } = init;

// return {
// items: computed(() => toValue(items)),
// key:
// typeof key === 'function'
// ? key
// : item => {
// if (key && isObject(item)) {
// return getFromPath(item, key, item);
// }

// return item;
// },
// };
// }
88 changes: 88 additions & 0 deletions packages/core/src/collections/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { useDefaultFilter, type FilterContext } from './filter';

describe('useDefaultFilter', () => {
const createContext = (search: string, label: string): FilterContext<string> => ({
search,
option: {
label,
value: label,
},
});

describe('case insensitive (default)', () => {
const { contains, startsWith, endsWith, equals } = useDefaultFilter();

it('contains should match substring regardless of case', () => {
expect(contains(createContext('world', 'Hello World'))).toBe(true);
expect(contains(createContext('WORLD', 'Hello World'))).toBe(true);
expect(contains(createContext('world', 'HELLO WORLD'))).toBe(true);
expect(contains(createContext('xyz', 'Hello World'))).toBe(false);
});

it('startsWith should match beginning of string regardless of case', () => {
expect(startsWith(createContext('hell', 'Hello World'))).toBe(true);
expect(startsWith(createContext('HELL', 'Hello World'))).toBe(true);
expect(startsWith(createContext('hell', 'HELLO WORLD'))).toBe(true);
expect(startsWith(createContext('world', 'Hello World'))).toBe(false);
});

it('endsWith should match end of string regardless of case', () => {
expect(endsWith(createContext('world', 'Hello World'))).toBe(true);
expect(endsWith(createContext('WORLD', 'Hello World'))).toBe(true);
expect(endsWith(createContext('world', 'HELLO WORLD'))).toBe(true);
expect(endsWith(createContext('hello', 'Hello World'))).toBe(false);
});

it('equals should match exact string regardless of case', () => {
expect(equals(createContext('hello world', 'Hello World'))).toBe(true);
expect(equals(createContext('HELLO WORLD', 'Hello World'))).toBe(true);
expect(equals(createContext('hello', 'Hello World'))).toBe(false);
});
});

describe('case sensitive', () => {
const { contains, startsWith, endsWith, equals } = useDefaultFilter({ caseSensitive: true });

it('contains should match substring with exact case', () => {
expect(contains(createContext('World', 'Hello World'))).toBe(true);
expect(contains(createContext('WORLD', 'Hello World'))).toBe(false);
expect(contains(createContext('world', 'Hello World'))).toBe(false);
});

it('startsWith should match beginning of string with exact case', () => {
expect(startsWith(createContext('Hello', 'Hello World'))).toBe(true);
expect(startsWith(createContext('HELLO', 'Hello World'))).toBe(false);
expect(startsWith(createContext('hello', 'Hello World'))).toBe(false);
});

it('endsWith should match end of string with exact case', () => {
expect(endsWith(createContext('World', 'Hello World'))).toBe(true);
expect(endsWith(createContext('WORLD', 'Hello World'))).toBe(false);
expect(endsWith(createContext('world', 'Hello World'))).toBe(false);
});

it('equals should match exact string with exact case', () => {
expect(equals(createContext('Hello World', 'Hello World'))).toBe(true);
expect(equals(createContext('HELLO WORLD', 'Hello World'))).toBe(false);
expect(equals(createContext('hello world', 'Hello World'))).toBe(false);
});
});

describe('debounce configuration', () => {
it('should set default debounce to 0', () => {
const { contains, startsWith, endsWith, equals } = useDefaultFilter();
expect(contains.debounceMs).toBe(0);
expect(startsWith.debounceMs).toBe(0);
expect(endsWith.debounceMs).toBe(0);
expect(equals.debounceMs).toBe(0);
});

it('should set custom debounce value', () => {
const { contains, startsWith, endsWith, equals } = useDefaultFilter({ debounceMs: 300 });
expect(contains.debounceMs).toBe(300);
expect(startsWith.debounceMs).toBe(300);
expect(endsWith.debounceMs).toBe(300);
expect(equals.debounceMs).toBe(300);
});
});
});
51 changes: 51 additions & 0 deletions packages/core/src/collections/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export interface FilterContext<TValue> {
search: string;
option: {
label: string;
value: TValue;
};
}

export interface FilterFn {
(context: FilterContext<unknown>): boolean;
debounceMs: number;
}

export interface FilterOptions {
caseSensitive?: boolean;
debounceMs?: number;
}

export function useDefaultFilter(options: FilterOptions = {}) {
const { caseSensitive = false, debounceMs = 0 } = options;

const withCaseSensitive = caseSensitive ? (value: string) => value : (value: string) => value.toLowerCase();

const contains: FilterFn = ({ search, option }) => {
return withCaseSensitive(option.label).includes(withCaseSensitive(search));
};

const startsWith: FilterFn = ({ search, option }) => {
return withCaseSensitive(option.label).startsWith(withCaseSensitive(search));
};

const endsWith: FilterFn = ({ search, option }) => {
return withCaseSensitive(option.label).endsWith(withCaseSensitive(search));
};

const equals: FilterFn = ({ search, option }) => {
return withCaseSensitive(option.label) === withCaseSensitive(search);
};

contains.debounceMs = debounceMs;
startsWith.debounceMs = debounceMs;
endsWith.debounceMs = debounceMs;
equals.debounceMs = debounceMs;

return {
contains,
startsWith,
endsWith,
equals,
};
}
2 changes: 2 additions & 0 deletions packages/core/src/collections/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// export * from './api';
export * from './filter';
2 changes: 2 additions & 0 deletions packages/core/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const FieldTypePrefixes = {
SliderThumb: 'st',
FormRepeater: 'fr',
CustomField: 'cf',
ComboBox: 'cbx',
ListBox: 'lb',
} as const;

export const NOOP = () => {};
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ export * from './useRadio';
export * from './useSlider';
export * from './useCheckbox';
export * from './useNumberField';
export * from './useOption';
export * from './useOptionGroup';
export * from './useListBox';
export * from './collections';
export * from './useSelect';
export * from './useComboBox';
export * from './useHiddenField';
export * from './useCustomField';
export * from './types';
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/useComboBox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useComboBox';
Loading
Loading