Skip to content

Commit a4c3714

Browse files
authored
feat(reactNativeWeb): support in Downshift and hooks (#1489)
1 parent 740b5b1 commit a4c3714

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
"sideEffects": false,
1010
"browserslist": [],
1111
"scripts": {
12-
"build": "npm run build:web --silent && npm run build:native --silent",
12+
"build": "npm run build:web --silent && npm run build:native --silent && npm run build:nativeWeb --silent",
1313
"build:web": "kcd-scripts build --bundle --p-react --no-clean --size-snapshot",
1414
"build:native": "cross-env BUILD_REACT_NATIVE=true BUILD_FILENAME_SUFFIX=.native kcd-scripts build --bundle cjs --no-clean",
15+
"build:nativeWeb": "cross-env BUILD_REACT_NATIVE_WEB=true BUILD_FILENAME_SUFFIX=.nativeweb kcd-scripts build --bundle cjs --no-clean",
1516
"lint": "kcd-scripts lint",
1617
"test": "kcd-scripts test",
1718
"test:cover": "kcd-scripts test --coverage",

src/downshift.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import PropTypes from 'prop-types'
44
import {Component, cloneElement} from 'react'
55
import {isForwardRef} from 'react-is'
6-
import {isPreact, isReactNative} from './is.macro'
6+
import {isPreact, isReactNative, isReactNativeWeb} from './is.macro'
77
import setA11yStatus from './set-a11y-status'
88
import * as stateChangeTypes from './stateChangeTypes'
99
import {
@@ -682,17 +682,21 @@ class Downshift extends Component {
682682
...rest
683683
} = {}) => {
684684
const {isOpen} = this.getState()
685-
const enabledEventHandlers = isReactNative
686-
? /* istanbul ignore next (react-native) */
687-
{
688-
onPress: callAllEventHandlers(onPress, this.buttonHandleClick),
689-
}
690-
: {
691-
onClick: callAllEventHandlers(onClick, this.buttonHandleClick),
692-
onKeyDown: callAllEventHandlers(onKeyDown, this.buttonHandleKeyDown),
693-
onKeyUp: callAllEventHandlers(onKeyUp, this.buttonHandleKeyUp),
694-
onBlur: callAllEventHandlers(onBlur, this.buttonHandleBlur),
695-
}
685+
const enabledEventHandlers =
686+
isReactNative || isReactNativeWeb
687+
? /* istanbul ignore next (react-native) */
688+
{
689+
onPress: callAllEventHandlers(onPress, this.buttonHandleClick),
690+
}
691+
: {
692+
onClick: callAllEventHandlers(onClick, this.buttonHandleClick),
693+
onKeyDown: callAllEventHandlers(
694+
onKeyDown,
695+
this.buttonHandleKeyDown,
696+
),
697+
onKeyUp: callAllEventHandlers(onKeyUp, this.buttonHandleKeyUp),
698+
onBlur: callAllEventHandlers(onBlur, this.buttonHandleBlur),
699+
}
696700
const eventHandlers = rest.disabled ? {} : enabledEventHandlers
697701
return {
698702
type: 'button',
@@ -844,7 +848,7 @@ class Downshift extends Component {
844848
this.internalSetState({
845849
type: stateChangeTypes.changeInput,
846850
isOpen: true,
847-
inputValue: isReactNative
851+
inputValue: isReactNative || isReactNativeWeb
848852
? /* istanbul ignore next (react-native) */ event.nativeEvent.text
849853
: event.target.value,
850854
highlightedIndex: this.props.defaultHighlightedIndex,
@@ -912,7 +916,7 @@ class Downshift extends Component {
912916
this.items[index] = item
913917
}
914918

915-
const onSelectKey = isReactNative
919+
const onSelectKey = isReactNative || isReactNativeWeb
916920
? /* istanbul ignore next (react-native) */ 'onPress'
917921
: 'onClick'
918922
const customClickHandler = isReactNative

src/hooks/useCombobox/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-statements */
22
import {useRef, useEffect, useCallback, useMemo} from 'react'
3-
import {isPreact, isReactNative} from '../../is.macro'
3+
import {isPreact, isReactNative, isReactNativeWeb} from '../../is.macro'
44
import {handleRefs, normalizeArrowKey, callAllEventHandlers} from '../../utils'
55
import {
66
useA11yMessageSetter,
@@ -303,7 +303,7 @@ function useCombobox(userProps = {}) {
303303
'Pass either item or index to getItemProps!',
304304
)
305305

306-
const onSelectKey = isReactNative
306+
const onSelectKey = isReactNative || isReactNativeWeb
307307
? /* istanbul ignore next (react-native) */ 'onPress'
308308
: 'onClick'
309309
const customClickHandler = isReactNative
@@ -371,7 +371,7 @@ function useCombobox(userProps = {}) {
371371
id: elementIds.toggleButtonId,
372372
tabIndex: -1,
373373
...(!rest.disabled && {
374-
...(isReactNative
374+
...(isReactNative || isReactNativeWeb
375375
? /* istanbul ignore next (react-native) */ {
376376
onPress: callAllEventHandlers(onPress, toggleButtonHandleClick),
377377
}
@@ -411,7 +411,7 @@ function useCombobox(userProps = {}) {
411411
const inputHandleChange = event => {
412412
dispatch({
413413
type: stateChangeTypes.InputChange,
414-
inputValue: isReactNative
414+
inputValue: isReactNative || isReactNativeWeb
415415
? /* istanbul ignore next (react-native) */ event.nativeEvent.text
416416
: event.target.value,
417417
})

src/hooks/useSelect/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
debounce,
1919
normalizeArrowKey,
2020
} from '../../utils'
21-
import {isReactNative} from '../../is.macro'
21+
import {isReactNative, isReactNativeWeb} from '../../is.macro'
2222
import downshiftSelectReducer from './reducer'
2323
import {validatePropTypes, defaultProps} from './utils'
2424
import * as stateChangeTypes from './stateChangeTypes'
@@ -407,7 +407,7 @@ function useSelect(userProps = {}) {
407407

408408
if (!rest.disabled) {
409409
/* istanbul ignore if (react-native) */
410-
if (isReactNative) {
410+
if (isReactNative || isReactNativeWeb) {
411411
toggleProps.onPress = callAllEventHandlers(
412412
onPress,
413413
toggleButtonHandleClick,
@@ -496,7 +496,7 @@ function useSelect(userProps = {}) {
496496

497497
if (!disabled) {
498498
/* istanbul ignore next (react-native) */
499-
if (isReactNative) {
499+
if (isReactNative || isReactNativeWeb) {
500500
itemProps.onPress = callAllEventHandlers(onPress, itemHandleClick)
501501
} else {
502502
itemProps.onClick = callAllEventHandlers(onClick, itemHandleClick)

src/is.macro.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {createMacro, MacroError} = require('babel-plugin-macros')
33
const importToEnvVar = {
44
isPreact: 'BUILD_PREACT',
55
isReactNative: 'BUILD_REACT_NATIVE',
6+
isReactNativeWeb: 'BUILD_REACT_NATIVE_WEB',
67
}
78

89
const arrToStr = arr => arr.join(', ')

0 commit comments

Comments
 (0)