File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed
Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 11import {
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+
3562describe ( 'mapToFormInputOptions' , ( ) => {
3663 it ( 'converts a map with boolean keys' , ( ) => {
3764 expect (
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments