Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[createReactClass] remove createReactClass from SegmentedControlIOS.ios.js #21888

Closed
wants to merge 6 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 45 additions & 66 deletions Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@

'use strict';

const DeprecatedViewPropTypes = require('DeprecatedViewPropTypes');
const NativeMethodsMixin = require('NativeMethodsMixin');
const PropTypes = require('prop-types');
const React = require('React');
const ReactNative = require('ReactNative');
const StyleSheet = require('StyleSheet');

const createReactClass = require('create-react-class');
const requireNativeComponent = require('requireNativeComponent');

import type {ViewProps} from 'ViewPropTypes';

const RCTSegmentedControl = requireNativeComponent('RCTSegmentedControl');
import type {NativeComponent} from 'ReactNative';

type DefaultProps = {
values: $ReadOnlyArray<string>,
Expand All @@ -31,18 +25,44 @@ type DefaultProps = {

type Props = $ReadOnly<{|
...ViewProps,
/**
* The labels for the control's segment buttons, in order.
*/
values?: ?$ReadOnlyArray<string>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have defaultProps this doesn't need to be optional.

  values: ?$ReadOnlyArray<string>,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, due to this being passed through React.forwardRef, it's not able to properly detect defaultProps

/**
* The index in `props.values` of the segment to be (pre)selected.
*/
selectedIndex?: ?number,
/**
* Callback that is called when the user taps a segment;
* passes the segment's value as an argument
*/
onValueChange?: ?Function,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the event is typed, you could type these callbacks like:

  onValueChange?: ?(value: number) => mixed,

  onChange?: ?(event: Event) => mixed,

/**
* Callback that is called when the user taps a segment;
* passes the event as an argument
*/
onChange?: ?Function,
/**
* If false the user won't be able to interact with the control.
* Default value is true.
*/
enabled?: ?boolean,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have defaultProps, this doesn't need to be optional.

  enabled: ?boolean,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/**
* Accent color of the control.
*/
tintColor?: ?string,
/**
* If true, then selecting a segment won't persist visually.
* The `onValueChange` callback will still work as expected.
*/
momentary?: ?boolean,
|}>;

const SEGMENTED_CONTROL_REFERENCE = 'segmentedcontrol';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


type Event = Object;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can type the event like this:

// Need to add this to your imports
import type {SyntheticEvent} from 'CoreEventTypes';

type Event = SyntheticEvent<
  $ReadOnly<{|
    value: number,
    selectedSegmentIndex: number,
  |}>,
>;

type NativeSegmentedControlIOS = Class<NativeComponent<Props>>;

/**
* Use `SegmentedControlIOS` to render a UISegmentedControl iOS.
Expand All @@ -64,66 +84,24 @@ type Event = Object;
* />
* ````
*/
const SegmentedControlIOS = createReactClass({
displayName: 'SegmentedControlIOS',
mixins: [NativeMethodsMixin],

propTypes: {
...DeprecatedViewPropTypes,
/**
* The labels for the control's segment buttons, in order.
*/
values: PropTypes.arrayOf(PropTypes.string),

/**
* The index in `props.values` of the segment to be (pre)selected.
*/
selectedIndex: PropTypes.number,

/**
* Callback that is called when the user taps a segment;
* passes the segment's value as an argument
*/
onValueChange: PropTypes.func,

/**
* Callback that is called when the user taps a segment;
* passes the event as an argument
*/
onChange: PropTypes.func,

/**
* If false the user won't be able to interact with the control.
* Default value is true.
*/
enabled: PropTypes.bool,

/**
* Accent color of the control.
*/
tintColor: PropTypes.string,

/**
* If true, then selecting a segment won't persist visually.
* The `onValueChange` callback will still work as expected.
*/
momentary: PropTypes.bool,
},

getDefaultProps: function(): DefaultProps {
return {
values: [],
enabled: true,
};
},
const RCTSegmentedControl = ((requireNativeComponent(
'RCTSegmentedControl',
): any): NativeSegmentedControlIOS);

class SegmentedControlIOS extends React.Component<Props> {
static defaultProps: DefaultProps = {
values: [],
enabled: true,
};

_onChange: function(event: Event) {
_onChange = (event: Event) => {
this.props.onChange && this.props.onChange(event);
this.props.onValueChange &&
this.props.onValueChange(event.nativeEvent.value);
},
};

render: function() {
render() {
return (
<RCTSegmentedControl
{...this.props}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be made more sophisticated:

render() {
  const { forwardedRef: ref, ...props } = this.props;
  return (
    <RCTSegmentedControl
      {...props}
      ref={ref}
      style={[styles.segmentedControl, this.props.style]}
      onChange={this._onChange}
    />   
  );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Expand All @@ -132,15 +110,16 @@ const SegmentedControlIOS = createReactClass({
onChange={this._onChange}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we have the string ref here. Pretty sure we can get rid of it, since it's not used anywhere in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/>
);
},
});
}
}

const styles = StyleSheet.create({
segmentedControl: {
height: 28,
},
});

module.exports = ((SegmentedControlIOS: any): Class<
ReactNative.NativeComponent<Props>,
>);
// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
const SegmentedControlIOSWithRef = React.forwardRef(SegmentedControlIOS);
Copy link
Contributor

@RSNara RSNara Oct 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, you can't pass in a component class to React.createRef. React.createRef expects a function. So what you really need to do is this:

const SegmentedControlIOSWithRef = React.forwardRef((
  props: Props, 
  ref: React.ref<typeof RCTSegmentedControl>
) => {
  return <SegmentedControlIOS {...props} forwardedRef={ref}/>
})

Copy link
Contributor Author

@nissy-dev nissy-dev Oct 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


module.exports = (SegmentedControlIOSWithRef: NativeSegmentedControlIOS);