Skip to content

Commit

Permalink
Fix forwarding refs to Onsen components (#3383)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomTirapani authored Jun 1, 2023
1 parent 93fc4f2 commit 5985f33
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 57.0.0-SNAPSHOT - unreleased

### 🐞 Bug Fixes

* Fixed a bug where Onsen components wrappers could not forward refs.

## 56.6.0 - 2023-06-01

### 🎁 New Features
Expand Down
20 changes: 5 additions & 15 deletions kit/onsen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import onsen from 'onsenui';
import 'onsenui/css/onsen-css-components.css';
import 'onsenui/css/onsenui.css';
import {createElement, forwardRef, FunctionComponent} from 'react';
import {isForwardRef} from 'react-is';
import * as ons from 'react-onsenui';
import {omitBy} from 'lodash';
import './styles.scss';
Expand Down Expand Up @@ -56,20 +55,11 @@ export const [dialog, Dialog] = wrappedCmp(ons.Dialog),
* There is no reason for an Onsen Component to ever receive a HoistModel prop, so we can safely
* strip them out here.
*/
function safeCmp(rawCmp): FunctionComponent {
return isForwardRef(rawCmp)
? forwardRef((props, ref) => {
const safeProps = omitBy(props, it => it instanceof HoistModel);
safeProps.ref = ref;
return createElement(rawCmp, safeProps);
})
: props => {
const safeProps = omitBy(props, it => it instanceof HoistModel);
return createElement(rawCmp, safeProps);
};
}

function wrappedCmp(rawCmp): [ElementFactory, FunctionComponent] {
const cmp = safeCmp(rawCmp);
const cmp = forwardRef((props, ref) => {
const safeProps = omitBy(props, it => it instanceof HoistModel);
if (ref) safeProps.ref = ref;
return createElement(rawCmp, safeProps);
});
return [elementFactory(cmp), cmp];
}

0 comments on commit 5985f33

Please sign in to comment.