1
- import { Children , HTMLAttributes , InputHTMLAttributes , isValidElement , PropsWithChildren , ReactNode } from 'react' ;
1
+ import {
2
+ Children ,
3
+ HTMLAttributes ,
4
+ InputHTMLAttributes ,
5
+ isValidElement ,
6
+ LabelHTMLAttributes ,
7
+ PropsWithChildren ,
8
+ ReactNode ,
9
+ } from 'react' ;
2
10
3
- import * as S from './style' ;
11
+ import * as S from './Input. style' ;
4
12
5
13
export interface BaseProps extends HTMLAttributes < HTMLDivElement > {
6
14
size ?: 'small' | 'medium' ;
@@ -10,6 +18,9 @@ export interface BaseProps extends HTMLAttributes<HTMLDivElement> {
10
18
export interface TextFieldProps extends InputHTMLAttributes < HTMLInputElement > {
11
19
inputSize ?: 'small' | 'medium' ;
12
20
}
21
+
22
+ export interface LabelProps extends LabelHTMLAttributes < HTMLLabelElement > { }
23
+
13
24
export interface AdornmentProps extends HTMLAttributes < HTMLDivElement > { }
14
25
15
26
export interface HelperTextProps extends HTMLAttributes < HTMLSpanElement > { }
@@ -20,14 +31,16 @@ const getChildOfType = (children: ReactNode, type: unknown) => {
20
31
return childrenArray . find ( ( child ) => isValidElement ( child ) && child . type === type ) ;
21
32
} ;
22
33
23
- const getChildrenWithoutType = ( children : ReactNode , type : unknown ) => {
34
+ const getChildrenWithoutTypes = ( children : ReactNode , types : unknown [ ] ) => {
24
35
const childrenArray = Children . toArray ( children ) ;
25
36
26
- return childrenArray . filter ( ( child ) => ! ( isValidElement ( child ) && child . type === type ) ) ;
37
+ return childrenArray . filter ( ( child ) => ! ( isValidElement ( child ) && types . includes ( child . type ) ) ) ;
27
38
} ;
28
39
29
40
const TextField = ( { ...rests } : TextFieldProps ) => < S . TextField { ...rests } /> ;
30
41
42
+ const Label = ( { children, ...rests } : PropsWithChildren < LabelProps > ) => < S . Label { ...rests } > { children } </ S . Label > ;
43
+
31
44
const Adornment = ( { children, ...rests } : PropsWithChildren < AdornmentProps > ) => (
32
45
< S . Adornment { ...rests } > { children } </ S . Adornment >
33
46
) ;
@@ -37,6 +50,7 @@ const HelperText = ({ children, ...rests }: PropsWithChildren<HelperTextProps>)
37
50
) ;
38
51
39
52
const HelperTextType = ( < HelperText /> ) . type ;
53
+ const LabelType = ( < Label /> ) . type ;
40
54
41
55
const Base = ( {
42
56
variant = 'filled' ,
@@ -45,21 +59,24 @@ const Base = ({
45
59
children,
46
60
...rests
47
61
} : PropsWithChildren < BaseProps > ) => {
48
- const inputWithAdornment = getChildrenWithoutType ( children , HelperTextType ) ;
62
+ const inputWithAdornment = getChildrenWithoutTypes ( children , [ HelperTextType , LabelType ] ) ;
49
63
const helperText = getChildOfType ( children , HelperTextType ) ;
64
+ const label = getChildOfType ( children , LabelType ) ;
50
65
51
66
return (
52
- < S . Wrapper >
67
+ < S . Container >
68
+ { label }
53
69
< S . Base variant = { variant } size = { size } isValid = { isValid } { ...rests } >
54
70
{ inputWithAdornment }
55
71
</ S . Base >
56
72
{ helperText }
57
- </ S . Wrapper >
73
+ </ S . Container >
58
74
) ;
59
75
} ;
60
76
61
77
const Input = Object . assign ( Base , {
62
78
TextField,
79
+ Label,
63
80
Adornment,
64
81
HelperText,
65
82
} ) ;
0 commit comments