Skip to content

Commit 68ec9cf

Browse files
committed
feat: add function objectEntriesToFormInputOptions
1 parent 8ec3076 commit 68ec9cf

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Select/formInput.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
mapToFormInputOptions,
3+
objectEntriesToFormInputOptions,
34
objectToFormInputOptions,
45
toFormInputOption,
56
} from './formInput';
@@ -32,6 +33,32 @@ describe('objectToFormInputOptions', () => {
3233
});
3334
});
3435

36+
describe('objectEntriesToFormInputOptions', () => {
37+
it('converts an array of manually declared object entries', () => {
38+
expect(
39+
objectEntriesToFormInputOptions([['foo', 'A descriptive label']]),
40+
).toEqual([
41+
{
42+
value: 'foo',
43+
label: 'A descriptive label',
44+
},
45+
]);
46+
});
47+
48+
it('converts the result of calling Object.entries', () => {
49+
expect(
50+
objectEntriesToFormInputOptions(
51+
Object.entries({ foo: 'A descriptive label' }),
52+
),
53+
).toEqual([
54+
{
55+
value: 'foo',
56+
label: 'A descriptive label',
57+
},
58+
]);
59+
});
60+
});
61+
3562
describe('mapToFormInputOptions', () => {
3663
it('converts a map with boolean keys', () => {
3764
expect(

src/Select/formInput.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@ export function objectToFormInputOptions<
1010
TLabels extends Record<string, TLabel>,
1111
TLabel extends ReactNode,
1212
>(labels: TLabels): Array<FormInputOption<keyof TLabels, TLabel>> {
13-
return Object.entries(labels).map(([key, value]) => ({
14-
value: key as keyof TLabels,
13+
return objectEntriesToFormInputOptions(Object.entries(labels));
14+
}
15+
16+
export function objectEntriesToFormInputOptions<
17+
TLabelEntries extends Array<[string, TLabel]>,
18+
TLabel extends ReactNode,
19+
>(
20+
labelEntries: TLabelEntries,
21+
): Array<FormInputOption<TLabelEntries[number][0], TLabel>> {
22+
return labelEntries.map(([key, value]) => ({
23+
value: key as TLabelEntries[number][0],
1524
label: value,
1625
}));
1726
}

0 commit comments

Comments
 (0)