1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { AriaAttributes } from '@builder.io/qwik' ;
3
2
import {
4
3
ExtendedPropsByAriaAttribute ,
4
+ QwikAriaAttributeCamelCaseElement ,
5
+ QwikIntrinsicAriaAttributes ,
6
+ QwikUiAreaAttributesFunctionReturnType ,
5
7
QwikUiAreaAttributesFunctionType ,
6
- QwikUiAriaAttributesKebab ,
7
- isKeyOfAriaAttributes ,
8
- isKeyOfQwikUiAriaAttributes ,
8
+ isKeyOfQwikCamelAriaAttributes ,
9
+ isKeyOfQwikIntrinsicAriaAttributes ,
9
10
} from './aria-attributes.type' ;
10
11
11
- export function keyToKebabCase ( str : string ) : keyof AriaAttributes {
12
+ export function keyToKebabCase (
13
+ str : string
14
+ ) : keyof QwikIntrinsicAriaAttributes < string > {
12
15
const newStr = str
13
16
. replace ( / ( [ a - z 0 - 9 ] | (? = [ A - Z ] ) ) ( [ A - Z ] ) / g, '$1-$2' )
14
17
. toLowerCase ( ) ;
15
- if ( isKeyOfAriaAttributes ( newStr ) ) {
18
+ if ( isKeyOfQwikIntrinsicAriaAttributes ( newStr ) ) {
16
19
return newStr ;
17
20
} else {
18
21
throw new Error ( 'The key you speficied was not an aria attribute.' ) ;
19
22
}
20
23
}
21
24
22
- const cacheMap : Map < string , Partial < AriaAttributes > > = new Map ( ) ;
25
+ const cacheMap = new Map ( ) ;
23
26
24
- const memoize = (
25
- func : QwikUiAreaAttributesFunctionType
26
- ) : QwikUiAreaAttributesFunctionType => {
27
+ const memoize = < K extends string > (
28
+ func : QwikUiAreaAttributesFunctionType < K >
29
+ ) : QwikUiAreaAttributesFunctionType < K > => {
27
30
return (
28
- qwikUiAriaAttributes ?: Partial < QwikUiAriaAttributesKebab > ,
31
+ qwikUiAriaAttributes ?: Partial < QwikAriaAttributeCamelCaseElement < K > > ,
29
32
lastKey ?: string
30
- ) : ReturnType < QwikUiAreaAttributesFunctionType > => {
33
+ ) : ReturnType < QwikUiAreaAttributesFunctionType < K > > => {
31
34
const key = JSON . stringify ( qwikUiAriaAttributes ) ;
32
35
if ( lastKey ) {
33
36
cacheMap . delete ( lastKey ) ;
@@ -42,38 +45,41 @@ const memoize = (
42
45
} ;
43
46
} ;
44
47
45
- export const extractQwikUiAriaAttributes = < T = any > (
46
- props : ExtendedPropsByAriaAttribute < T >
48
+ export const extractCamelAriaAttributes = < K extends string , T = any > (
49
+ props : ExtendedPropsByAriaAttribute < K , T >
47
50
) => {
51
+ if ( ! props ) {
52
+ return { } ;
53
+ }
48
54
return Object . keys ( props ) . reduce (
49
55
( cur , propKey ) =>
50
- isKeyOfQwikUiAriaAttributes ( propKey )
56
+ isKeyOfQwikCamelAriaAttributes ( propKey )
51
57
? { ...cur , [ propKey ] : props [ propKey ] }
52
58
: cur ,
53
59
{ }
54
60
) ;
55
61
} ;
56
62
57
- export const getAriaAttributes = < T = any > (
58
- props : ExtendedPropsByAriaAttribute < T > ,
63
+ export const getAriaAttributes = < K extends string , T = any > (
64
+ props : ExtendedPropsByAriaAttribute < K , T > ,
59
65
lastKey ?: string
60
- ) : ReturnType < QwikUiAreaAttributesFunctionType > => {
66
+ ) : QwikUiAreaAttributesFunctionReturnType < K > => {
61
67
const process = (
62
- qwikUiAriaAttributes ?: Partial < QwikUiAriaAttributesKebab >
63
- ) : ReturnType < QwikUiAreaAttributesFunctionType > => {
68
+ qwikUiAriaAttributes ?: Partial < QwikAriaAttributeCamelCaseElement < K > >
69
+ ) : ReturnType < QwikUiAreaAttributesFunctionType < K > > => {
64
70
return {
65
71
lastKey : JSON . stringify ( qwikUiAriaAttributes ) ,
66
72
ariaAttributes : qwikUiAriaAttributes
67
73
? Object . keys ( qwikUiAriaAttributes ) . reduce (
68
74
( cur , key ) =>
69
- isKeyOfQwikUiAriaAttributes ( key )
75
+ isKeyOfQwikCamelAriaAttributes ( key )
70
76
? { ...cur , [ keyToKebabCase ( key ) ] : qwikUiAriaAttributes [ key ] }
71
77
: cur ,
72
78
{ }
73
79
)
74
80
: { } ,
75
81
} ;
76
82
} ;
77
- const qwikUiAriaAttributes = extractQwikUiAriaAttributes < T > ( props ) ;
83
+ const qwikUiAriaAttributes = extractCamelAriaAttributes < K , T > ( props ) ;
78
84
return memoize ( process ) ( qwikUiAriaAttributes , lastKey ) ;
79
85
} ;
0 commit comments