-
Notifications
You must be signed in to change notification settings - Fork 42
/
Compose.tsx
78 lines (65 loc) · 1.91 KB
/
Compose.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { DSize } from '../../utils/types';
import React, { useMemo } from 'react';
import { getClassName } from '@react-devui/utils';
import { useGeneralContext } from '../../hooks';
import { registerComponentMate } from '../../utils';
import { useComponentConfig, usePrefixConfig } from '../root';
import { DComposeItem } from './ComposeItem';
export interface DComposeContextData {
gSize?: DSize;
gDisabled?: boolean;
}
export const DComposeContext = React.createContext<DComposeContextData | null>(null);
export interface DComposeProps extends React.HTMLAttributes<HTMLDivElement> {
dSize?: DSize;
dVertical?: boolean;
dDisabled?: boolean;
}
export interface DComposePrivateProps {
__noStyle?: boolean;
}
const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DCompose' as const });
export const DCompose: {
(props: DComposeProps): JSX.Element | null;
Item: typeof DComposeItem;
} = (props) => {
const {
children,
dSize,
dVertical = false,
dDisabled = false,
__noStyle,
...restProps
} = useComponentConfig(COMPONENT_NAME, props as DComposeProps & DComposePrivateProps);
//#region Context
const dPrefix = usePrefixConfig();
const { gSize, gDisabled } = useGeneralContext();
//#endregion
const size = dSize ?? gSize;
const disabled = dDisabled || gDisabled;
const contextValue = useMemo<DComposeContextData>(
() => ({
gSize: size,
gDisabled: disabled,
}),
[disabled, size]
);
return (
<DComposeContext.Provider value={contextValue}>
<div
{...restProps}
className={
__noStyle
? restProps.className
: getClassName(restProps.className, `${dPrefix}compose`, {
[`${dPrefix}compose--vertical`]: dVertical,
})
}
role="group"
>
{children}
</div>
</DComposeContext.Provider>
);
};
DCompose.Item = DComposeItem;