Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

refactor(Portal): allow to use createRef() API in triggerRef #787

Merged
merged 6 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixes
- Fix layout of `Accordion` panel's title @kuzhelov ([#780](https://github.com/stardust-ui/react/pull/780))
- Allow to use `createRef()` API with `triggerRef` prop in `Portal` component @layershifter ([#787](https://github.com/stardust-ui/react/pull/787))

<!--------------------------------[ v0.19.0 ]------------------------------- -->
## [v0.19.0](https://github.com/stardust-ui/react/tree/v0.19.0) (2019-01-28)
Expand Down
10 changes: 8 additions & 2 deletions src/components/Dropdown/DropdownSearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import * as React from 'react'
import * as PropTypes from 'prop-types'
import * as _ from 'lodash'

import { UIComponent, RenderResultConfig, createShorthandFactory, commonPropTypes } from '../../lib'
import {
UIComponent,
RenderResultConfig,
createShorthandFactory,
commonPropTypes,
customPropTypes,
} from '../../lib'
import { ComponentEventHandler, ReactProps } from '../../../types/utils'
import { UIComponentProps } from '../../lib/commonPropInterfaces'
import Input from '../Input/Input'
Expand Down Expand Up @@ -68,7 +74,7 @@ class DropdownSearchInput extends UIComponent<ReactProps<DropdownSearchInputProp
accessibilityInputProps: PropTypes.object,
accessibilityComboboxProps: PropTypes.object,
hasToggleButton: PropTypes.bool,
inputRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
inputRef: customPropTypes.ref,
onFocus: PropTypes.func,
onInputBlur: PropTypes.func,
onInputKeyDown: PropTypes.func,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Input extends AutoControlledComponent<ReactProps<InputProps>, InputState>
icon: customPropTypes.itemShorthand,
iconPosition: PropTypes.oneOf(['start', 'end']),
input: customPropTypes.itemShorthand,
inputRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
inputRef: customPropTypes.ref,
inline: PropTypes.bool,
onChange: PropTypes.func,
type: PropTypes.string,
Expand Down
15 changes: 6 additions & 9 deletions src/components/Portal/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
ChildrenComponentProps,
commonPropTypes,
ContentComponentProps,
handleRef,
rtlTextContainer,
customPropTypes,
} from '../../lib'
import Ref from '../Ref/Ref'
import PortalInner from './PortalInner'
Expand Down Expand Up @@ -54,12 +56,8 @@ export interface PortalProps extends ChildrenComponentProps, ContentComponentPro
/** Accessibility behavior object to apply on trigger node. */
triggerAccessibility?: TriggerAccessibility

/**
* Called with a ref to the trigger node.
*
* @param {JSX.Element} node - Referred node.
*/
triggerRef?: (node: HTMLElement) => void
/** Sets trigger node to passed ref. */
triggerRef?: React.Ref<any>

/**
* Called when trigger node was clicked.
Expand Down Expand Up @@ -103,7 +101,7 @@ class Portal extends AutoControlledComponent<ReactPropsStrict<PortalProps>, Port
onUnmount: PropTypes.func,
open: PropTypes.bool,
trigger: PropTypes.node,
triggerRef: PropTypes.func,
triggerRef: customPropTypes.ref,
triggerAccessibility: PropTypes.object,
onTriggerClick: PropTypes.func,
onOutsideClick: PropTypes.func,
Expand Down Expand Up @@ -180,8 +178,7 @@ class Portal extends AutoControlledComponent<ReactPropsStrict<PortalProps>, Port

private handleTriggerRef = (triggerNode: HTMLElement) => {
this.triggerNode = triggerNode

_.invoke(this.props, 'triggerRef', triggerNode)
handleRef(this.props.triggerRef, triggerNode)
}

private handleTriggerClick = (e: ReactMouseEvent, ...unhandledProps) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Ref/Ref.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as PropTypes from 'prop-types'
import * as React from 'react'
import { isForwardRef } from 'react-is'

import { ChildrenComponentProps } from '../../lib'
import { ChildrenComponentProps, customPropTypes } from '../../lib'
import { ReactPropsStrict } from '../../../types/utils'
import RefFindNode from './RefFindNode'
import RefForward from './RefForward'
Expand All @@ -27,7 +27,7 @@ const Ref: React.SFC<ReactPropsStrict<RefProps>> = props => {

Ref.propTypes = {
children: PropTypes.element.isRequired,
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]) as PropTypes.Requireable<any>,
innerRef: customPropTypes.ref as PropTypes.Requireable<any>,
}

export default Ref
4 changes: 2 additions & 2 deletions src/components/Ref/RefFindNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as PropTypes from 'prop-types'
import * as React from 'react'
import * as ReactDOM from 'react-dom'

import { ChildrenComponentProps, handleRef } from '../../lib'
import { ChildrenComponentProps, customPropTypes, handleRef } from '../../lib'

export interface RefFindNodeProps extends ChildrenComponentProps<React.ReactElement<any>> {
/**
Expand All @@ -16,7 +16,7 @@ export interface RefFindNodeProps extends ChildrenComponentProps<React.ReactElem
export default class RefFindNode extends React.Component<RefFindNodeProps> {
static propTypes = {
children: PropTypes.element.isRequired,
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
innerRef: customPropTypes.ref,
}

componentDidMount() {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Ref/RefForward.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as PropTypes from 'prop-types'
import * as React from 'react'

import { ChildrenComponentProps, handleRef } from '../../lib'
import { ChildrenComponentProps, customPropTypes, handleRef } from '../../lib'

export interface RefForwardProps
extends ChildrenComponentProps<React.ReactElement<any> & { ref: React.Ref<any> }> {
Expand All @@ -16,7 +16,7 @@ export interface RefForwardProps
export default class RefForward extends React.Component<RefForwardProps> {
static propTypes = {
children: PropTypes.element.isRequired,
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
innerRef: customPropTypes.ref,
}

private handleRefOverride = (node: HTMLElement) => {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/customPropTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,6 @@ export const animation = PropTypes.oneOfType([
}) as any,
PropTypes.string,
])

/** A checker that matches the React.Ref type. */
export const ref = PropTypes.oneOfType([PropTypes.func, PropTypes.object])