Skip to content

Commit

Permalink
feat(reactNativeWeb): support in Downshift and hooks (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
silviuaavram committed Mar 20, 2023
1 parent 740b5b1 commit a4c3714
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"sideEffects": false,
"browserslist": [],
"scripts": {
"build": "npm run build:web --silent && npm run build:native --silent",
"build": "npm run build:web --silent && npm run build:native --silent && npm run build:nativeWeb --silent",
"build:web": "kcd-scripts build --bundle --p-react --no-clean --size-snapshot",
"build:native": "cross-env BUILD_REACT_NATIVE=true BUILD_FILENAME_SUFFIX=.native kcd-scripts build --bundle cjs --no-clean",
"build:nativeWeb": "cross-env BUILD_REACT_NATIVE_WEB=true BUILD_FILENAME_SUFFIX=.nativeweb kcd-scripts build --bundle cjs --no-clean",
"lint": "kcd-scripts lint",
"test": "kcd-scripts test",
"test:cover": "kcd-scripts test --coverage",
Expand Down
32 changes: 18 additions & 14 deletions src/downshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import PropTypes from 'prop-types'
import {Component, cloneElement} from 'react'
import {isForwardRef} from 'react-is'
import {isPreact, isReactNative} from './is.macro'
import {isPreact, isReactNative, isReactNativeWeb} from './is.macro'
import setA11yStatus from './set-a11y-status'
import * as stateChangeTypes from './stateChangeTypes'
import {
Expand Down Expand Up @@ -682,17 +682,21 @@ class Downshift extends Component {
...rest
} = {}) => {
const {isOpen} = this.getState()
const enabledEventHandlers = isReactNative
? /* istanbul ignore next (react-native) */
{
onPress: callAllEventHandlers(onPress, this.buttonHandleClick),
}
: {
onClick: callAllEventHandlers(onClick, this.buttonHandleClick),
onKeyDown: callAllEventHandlers(onKeyDown, this.buttonHandleKeyDown),
onKeyUp: callAllEventHandlers(onKeyUp, this.buttonHandleKeyUp),
onBlur: callAllEventHandlers(onBlur, this.buttonHandleBlur),
}
const enabledEventHandlers =
isReactNative || isReactNativeWeb
? /* istanbul ignore next (react-native) */
{
onPress: callAllEventHandlers(onPress, this.buttonHandleClick),
}
: {
onClick: callAllEventHandlers(onClick, this.buttonHandleClick),
onKeyDown: callAllEventHandlers(
onKeyDown,
this.buttonHandleKeyDown,
),
onKeyUp: callAllEventHandlers(onKeyUp, this.buttonHandleKeyUp),
onBlur: callAllEventHandlers(onBlur, this.buttonHandleBlur),
}
const eventHandlers = rest.disabled ? {} : enabledEventHandlers
return {
type: 'button',
Expand Down Expand Up @@ -844,7 +848,7 @@ class Downshift extends Component {
this.internalSetState({
type: stateChangeTypes.changeInput,
isOpen: true,
inputValue: isReactNative
inputValue: isReactNative || isReactNativeWeb
? /* istanbul ignore next (react-native) */ event.nativeEvent.text
: event.target.value,
highlightedIndex: this.props.defaultHighlightedIndex,
Expand Down Expand Up @@ -912,7 +916,7 @@ class Downshift extends Component {
this.items[index] = item
}

const onSelectKey = isReactNative
const onSelectKey = isReactNative || isReactNativeWeb
? /* istanbul ignore next (react-native) */ 'onPress'
: 'onClick'
const customClickHandler = isReactNative
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useCombobox/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-statements */
import {useRef, useEffect, useCallback, useMemo} from 'react'
import {isPreact, isReactNative} from '../../is.macro'
import {isPreact, isReactNative, isReactNativeWeb} from '../../is.macro'
import {handleRefs, normalizeArrowKey, callAllEventHandlers} from '../../utils'
import {
useA11yMessageSetter,
Expand Down Expand Up @@ -303,7 +303,7 @@ function useCombobox(userProps = {}) {
'Pass either item or index to getItemProps!',
)

const onSelectKey = isReactNative
const onSelectKey = isReactNative || isReactNativeWeb
? /* istanbul ignore next (react-native) */ 'onPress'
: 'onClick'
const customClickHandler = isReactNative
Expand Down Expand Up @@ -371,7 +371,7 @@ function useCombobox(userProps = {}) {
id: elementIds.toggleButtonId,
tabIndex: -1,
...(!rest.disabled && {
...(isReactNative
...(isReactNative || isReactNativeWeb
? /* istanbul ignore next (react-native) */ {
onPress: callAllEventHandlers(onPress, toggleButtonHandleClick),
}
Expand Down Expand Up @@ -411,7 +411,7 @@ function useCombobox(userProps = {}) {
const inputHandleChange = event => {
dispatch({
type: stateChangeTypes.InputChange,
inputValue: isReactNative
inputValue: isReactNative || isReactNativeWeb
? /* istanbul ignore next (react-native) */ event.nativeEvent.text
: event.target.value,
})
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useSelect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
debounce,
normalizeArrowKey,
} from '../../utils'
import {isReactNative} from '../../is.macro'
import {isReactNative, isReactNativeWeb} from '../../is.macro'
import downshiftSelectReducer from './reducer'
import {validatePropTypes, defaultProps} from './utils'
import * as stateChangeTypes from './stateChangeTypes'
Expand Down Expand Up @@ -407,7 +407,7 @@ function useSelect(userProps = {}) {

if (!rest.disabled) {
/* istanbul ignore if (react-native) */
if (isReactNative) {
if (isReactNative || isReactNativeWeb) {
toggleProps.onPress = callAllEventHandlers(
onPress,
toggleButtonHandleClick,
Expand Down Expand Up @@ -496,7 +496,7 @@ function useSelect(userProps = {}) {

if (!disabled) {
/* istanbul ignore next (react-native) */
if (isReactNative) {
if (isReactNative || isReactNativeWeb) {
itemProps.onPress = callAllEventHandlers(onPress, itemHandleClick)
} else {
itemProps.onClick = callAllEventHandlers(onClick, itemHandleClick)
Expand Down
1 change: 1 addition & 0 deletions src/is.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {createMacro, MacroError} = require('babel-plugin-macros')
const importToEnvVar = {
isPreact: 'BUILD_PREACT',
isReactNative: 'BUILD_REACT_NATIVE',
isReactNativeWeb: 'BUILD_REACT_NATIVE_WEB',
}

const arrToStr = arr => arr.join(', ')
Expand Down

0 comments on commit a4c3714

Please sign in to comment.