-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): Initial implementations of controller required elements. (…
- Loading branch information
Showing
15 changed files
with
257 additions
and
141 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
import { Components } from '@ionic/core'; | ||
import { createControllerComponent } from './createControllerComponent'; | ||
import { Omit } from './types'; | ||
|
||
export type ActionSheetOptions = Omit<Components.IonActionSheetAttributes, 'overlayIndex'>; | ||
|
||
const IonActionSheet = createControllerComponent<ActionSheetOptions, HTMLIonActionSheetElement, HTMLIonActionSheetControllerElement>('ion-action-sheet', 'ion-action-sheet-controller') | ||
export default IonActionSheet; |
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,8 @@ | ||
import { Components } from '@ionic/core'; | ||
import { createControllerComponent } from './createControllerComponent'; | ||
import { Omit } from './types'; | ||
|
||
export type AlertOptions = Omit<Components.IonAlertAttributes, 'overlayIndex'>; | ||
|
||
const IonAlert = createControllerComponent<AlertOptions, HTMLIonAlertElement, HTMLIonAlertControllerElement>('ion-alert', 'ion-alert-controller') | ||
export default IonAlert; |
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,8 @@ | ||
import { Components } from '@ionic/core'; | ||
import { createControllerComponent } from './createControllerComponent'; | ||
import { Omit } from './types'; | ||
|
||
export type LoadingOptions = Omit<Components.IonLoadingAttributes, 'overlayIndex'>; | ||
|
||
const IonActionSheet = createControllerComponent<LoadingOptions, HTMLIonLoadingElement, HTMLIonLoadingControllerElement>('ion-loading', 'ion-loading-controller') | ||
export default IonActionSheet; |
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,8 @@ | ||
import { Components } from '@ionic/core'; | ||
import { createControllerComponent } from './createControllerComponent'; | ||
import { Omit } from './types'; | ||
|
||
export type ModalOptions = Omit<Components.IonModalAttributes, 'delegate' | 'overlayIndex' | 'component' | 'componentProps'>; | ||
|
||
const IonModal = createControllerComponent<ModalOptions, HTMLIonModalElement, HTMLIonModalControllerElement>('ion-modal', 'ion-modal-controller') | ||
export default IonModal; |
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,8 @@ | ||
import { Components } from '@ionic/core'; | ||
import { createControllerComponent } from './createControllerComponent'; | ||
import { Omit } from './types'; | ||
|
||
export type PopoverOptions = Omit<Components.IonPopoverAttributes, 'delegate' | 'overlayIndex' | 'component' | 'componentProps'>; | ||
|
||
const IonPopover = createControllerComponent<PopoverOptions, HTMLIonPopoverElement, HTMLIonPopoverControllerElement>('ion-popover', 'ion-popover-controller') | ||
export default IonPopover; |
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,8 @@ | ||
import { Components } from '@ionic/core'; | ||
import { createControllerComponent } from './createControllerComponent'; | ||
import { Omit } from './types'; | ||
|
||
export type ToastOptions = Omit<Components.IonToastAttributes, 'overlayIndex'>; | ||
|
||
const IonToast = createControllerComponent<ToastOptions, HTMLIonToastElement, HTMLIonToastControllerElement>('ion-toast', 'ion-toast-controller') | ||
export default IonToast; |
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { dashToPascalCase, attachEventProps } from './utils'; | ||
|
||
export function createReactComponent<T, E>(tagName: string) { | ||
const displayName = dashToPascalCase(tagName); | ||
|
||
type IonicReactInternalProps = { | ||
forwardedRef?: React.RefObject<E>; | ||
children?: React.ReactNode; | ||
} | ||
|
||
type IonicReactExternalProps = { | ||
ref?: React.RefObject<E>; | ||
children?: React.ReactNode; | ||
} | ||
|
||
class ReactComponent extends React.Component<T & IonicReactInternalProps> { | ||
componentRef: React.RefObject<E>; | ||
|
||
constructor(props: T & IonicReactInternalProps) { | ||
super(props); | ||
this.componentRef = React.createRef(); | ||
} | ||
|
||
static get displayName() { | ||
return displayName; | ||
} | ||
|
||
componentDidMount() { | ||
this.componentWillReceiveProps(this.props); | ||
} | ||
|
||
componentWillReceiveProps(props: any) { | ||
const node = ReactDOM.findDOMNode(this); | ||
|
||
if (!(node instanceof HTMLElement)) { | ||
return; | ||
} | ||
|
||
attachEventProps(node, props); | ||
} | ||
|
||
render() { | ||
const { children, forwardedRef, ...cProps } = this.props as any; | ||
cProps.ref = forwardedRef; | ||
|
||
return React.createElement(tagName, cProps, children); | ||
} | ||
} | ||
|
||
function forwardRef(props: T & IonicReactInternalProps, ref: React.RefObject<E>) { | ||
return <ReactComponent {...props} forwardedRef={ref} />; | ||
} | ||
forwardRef.displayName = displayName; | ||
|
||
return React.forwardRef<E, T & IonicReactExternalProps>(forwardRef); | ||
} |
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,50 @@ | ||
import React from 'react'; | ||
import { attachEventProps } from './utils' | ||
import { ensureElementInBody, dashToPascalCase } from './utils'; | ||
|
||
export function createControllerComponent<T, E extends HTMLElement, C extends HTMLElement>(tagName: string, controllerTagName: string) { | ||
const displayName = dashToPascalCase(tagName); | ||
|
||
type IonicReactInternalProps = { | ||
forwardedRef?: React.RefObject<E>; | ||
children?: React.ReactNode; | ||
show: boolean | ||
} | ||
|
||
return class ReactControllerComponent extends React.Component<T & IonicReactInternalProps> { | ||
element: E; | ||
controllerElement: C; | ||
|
||
constructor(props: T & IonicReactInternalProps) { | ||
super(props); | ||
} | ||
|
||
static get displayName() { | ||
return displayName; | ||
} | ||
|
||
async componentDidMount() { | ||
this.controllerElement = ensureElementInBody<C>(controllerTagName); | ||
await (this.controllerElement as any).componentOnReady(); | ||
} | ||
|
||
async componentDidUpdate(prevProps: T & IonicReactInternalProps) { | ||
if (prevProps.show !== this.props.show && this.props.show === true) { | ||
const { children, show, ...cProps} = this.props as any; | ||
|
||
this.element = await (this.controllerElement as any).create(cProps); | ||
await (this.element as any).present(); | ||
|
||
attachEventProps(this.element, cProps); | ||
} | ||
if (prevProps.show !== this.props.show && this.props.show === false) { | ||
return await (this.element as any).dismiss(); | ||
} | ||
} | ||
|
||
render(): null { | ||
return null; | ||
} | ||
} | ||
} | ||
|
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,2 @@ | ||
|
||
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; |
Oops, something went wrong.