-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dropdown): add basic component and stories
- Loading branch information
Showing
5 changed files
with
401 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import './style.scss'; | ||
import * as React from 'react'; | ||
import { classNames, prefixClaName } from 'mo/common/className'; | ||
import { useContextView } from '../contextview'; | ||
import { triggerEvent, TriggerEvent } from 'mo/common/dom'; | ||
|
||
export interface IDropDown extends HTMLElementProps { | ||
overlay: ReactNode; | ||
trigger?: TriggerEvent; | ||
placement?: 'top' | 'right' | 'bottom' | 'left'; | ||
} | ||
|
||
export const defaultDropDownClassName = 'drop-down'; | ||
|
||
export function DropDown(props: React.PropsWithChildren<IDropDown>) { | ||
const { | ||
className, | ||
overlay, | ||
children, | ||
placement = 'bottom', | ||
trigger = 'click', | ||
...others | ||
} = props; | ||
const contextView = useContextView({ | ||
render: () => overlay, | ||
}); | ||
|
||
const claNames = classNames( | ||
prefixClaName(defaultDropDownClassName), | ||
placement, | ||
className | ||
); | ||
const events = { | ||
[triggerEvent(trigger)]: function (e: React.MouseEvent) { | ||
const target = e.currentTarget; | ||
const rect = target.getBoundingClientRect(); | ||
contextView.show({ | ||
x: rect.x + rect.width, | ||
y: rect.y, | ||
}); | ||
}, | ||
}; | ||
|
||
return ( | ||
<div className={claNames} {...events} {...others}> | ||
{children} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@import 'mo/style/const'; | ||
$dropDown: 'drop-down'; | ||
|
||
#{prefix($dropDown)} { | ||
align-items: center; | ||
display: flex; | ||
height: 100%; | ||
justify-content: center; | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.