-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datepicker): add flatpickr plugins
- Loading branch information
1 parent
b040f51
commit 4ea62b4
Showing
6 changed files
with
412 additions
and
35 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @license | ||
* | ||
* Copyright IBM Corp. 2019 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/* | ||
* COPIED FROM carbon-components-react | ||
*/ | ||
|
||
/** | ||
* @param {object} config Plugin configuration. | ||
* @returns {Plugin} A Flatpickr plugin to put adjust the position of calendar dropdown. | ||
*/ | ||
export default config => fp => { | ||
/** | ||
* Adjusts the floating meun position after Flatpicker sets it. | ||
*/ | ||
const handlePreCalendarPosition = () => { | ||
Promise.resolve().then(() => { | ||
const { | ||
calendarContainer, | ||
config: fpConfig, | ||
_positionElement: positionElement, | ||
} = fp; | ||
const { appendTo } = fpConfig; | ||
const { left: containerLeft, top: containerTop } = | ||
appendTo.getBoundingClientRect(); | ||
const { left: refLeft, bottom: refBottom } = | ||
positionElement.getBoundingClientRect(); | ||
if ( | ||
(appendTo !== appendTo.ownerDocument.body || | ||
containerLeft !== 0 || | ||
containerTop !== 0) && | ||
appendTo.ownerDocument.defaultView | ||
.getComputedStyle(appendTo) | ||
.getPropertyValue('position') === 'static' | ||
) { | ||
throw new Error( | ||
'Floating menu container must not have `position:static`.' | ||
); | ||
} | ||
// `2` for negative mergin on calendar dropdown | ||
calendarContainer.style.top = `${refBottom - containerTop + 2}px`; | ||
calendarContainer.style.left = `${refLeft - containerLeft}px`; | ||
}); | ||
}; | ||
|
||
/** | ||
* Registers this Flatpickr plugin. | ||
*/ | ||
const register = () => { | ||
fp.loadedPlugins.push('carbonFlatpickrAppendToPlugin'); | ||
}; | ||
|
||
return { | ||
appendTo: config.appendTo, | ||
onReady: register, | ||
onPreCalendarPosition: handlePreCalendarPosition, | ||
}; | ||
}; |
Oops, something went wrong.