diff --git a/package-scripts.js b/package-scripts.js index 00cebfb4..729fa9b0 100644 --- a/package-scripts.js +++ b/package-scripts.js @@ -26,6 +26,7 @@ module.exports = { concurrent.nps( 'build.es', 'build.cjs', + 'build.native', 'build.umd.main', 'build.umd.min', 'copyTypes' @@ -39,6 +40,10 @@ module.exports = { description: 'run rollup build with CommonJS format', script: 'rollup --config --environment FORMAT:cjs' }, + native: { + description: 'run rollup build resolving alias files for React Native', + script: 'rollup --config --environment NATIVE,FORMAT:cjs' + }, umd: { min: { description: 'run the rollup build with sourcemaps', diff --git a/package.json b/package.json index 7cb9c26b..fed42b0b 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "dist/react-final-form.cjs.js", "jsnext:main": "dist/react-final-form.es.js", "module": "dist/react-final-form.es.js", + "react-native": "dist/react-final-form.native.js", "typings": "dist/index.d.ts", "files": [ "dist" diff --git a/rollup.config.js b/rollup.config.js index 15f5ec5d..3cccf43c 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -8,6 +8,7 @@ import pkg from './package.json' const minify = process.env.MINIFY const format = process.env.FORMAT +const native = process.env.NATIVE const es = format === 'es' const umd = format === 'umd' const cjs = format === 'cjs' @@ -15,7 +16,7 @@ const cjs = format === 'cjs' let output if (es) { - output = { file: `dist/react-final-form.es.js`, format: 'es' } + output = { file: pkg.module, format: 'es' } } else if (umd) { if (minify) { output = { @@ -25,8 +26,10 @@ if (es) { } else { output = { file: `dist/react-final-form.umd.js`, format: 'umd' } } +} else if (native) { + output = { file: pkg['react-native'], format: 'cjs' } } else if (cjs) { - output = { file: `dist/react-final-form.cjs.js`, format: 'cjs' } + output = { file: pkg.main, format: 'cjs' } } else if (format) { throw new Error(`invalid format specified: "${format}".`) } else { @@ -54,7 +57,11 @@ export default { ...Object.keys(pkg.peerDependencies || {}) ], plugins: [ - resolve({ jsnext: true, main: true }), + resolve({ + jsnext: true, + main: true, + extensions: native ? [ '.native.js', '.js' ] : undefined + }), flow(), commonjs({ include: 'node_modules/**' }), babel({ diff --git a/src/Field.js b/src/Field.js index 8dbff795..f0f1079e 100644 --- a/src/Field.js +++ b/src/Field.js @@ -10,7 +10,6 @@ import type { ReactContext } from './types' import renderComponent from './renderComponent' -import isReactNative from './isReactNative' import getValue from './getValue' const all: FieldSubscription = fieldSubscriptionItems.reduce((result, key) => { @@ -147,7 +146,6 @@ class Field extends React.Component { event, this.state.state && this.state.state.value, _value, - isReactNative ) : event this.state.state && diff --git a/src/getValue.js b/src/getValue.js index 6de52120..c3e76646 100644 --- a/src/getValue.js +++ b/src/getValue.js @@ -16,18 +16,14 @@ const getValue = ( event: SyntheticInputEvent<*>, currentValue: any, valueProp: any, - isReactNative: boolean ) => { if ( - !isReactNative && event.nativeEvent && (event.nativeEvent: Object).text !== undefined ) { return (event.nativeEvent: Object).text } - if (isReactNative && event.nativeEvent) { - return (event.nativeEvent: any).text - } + const detypedEvent: any = event const { target: { type, value, checked } } = detypedEvent switch (type) { diff --git a/src/getValue.native.js b/src/getValue.native.js new file mode 100644 index 00000000..758b77a6 --- /dev/null +++ b/src/getValue.native.js @@ -0,0 +1,13 @@ +// @flow + +const getValue = ( + event: SyntheticInputEvent<*>, + currentValue: any, + valueProp: any, +) => { + if (event.nativeEvent) { + return (event.nativeEvent: any).text + } +} + +export default getValue diff --git a/src/isReactNative.js b/src/isReactNative.js deleted file mode 100644 index 95634d95..00000000 --- a/src/isReactNative.js +++ /dev/null @@ -1,8 +0,0 @@ -// @flow -const isReactNative = - typeof window !== 'undefined' && - window.navigator && - window.navigator.product && - window.navigator.product === 'ReactNative' - -export default isReactNative