How to upgrade from V1 to V2? Need full examples

末鹿安然 提交于 2020-03-05 03:59:12

问题


I don't see any samples showing before and after view of the code for going from v1 to v2.

We have a wrapper for react-select in our file Select.jsx that in V1 contains

import React from 'react';
import ReactSelect from 'react-select';
import css from 'react-css-modules';
import globalStyles from '../../../node_modules/react-select/dist/react-select.css';
import styles from './Select.css';

/**
 * Select component
 * How do we style this?
 */
const Select = (props) => {
  const onChange = values => {
    props.multi ? props.onChange(_.map(values, obj => obj.value)) : props.onChange(values.value)
  };

  return <ReactSelect
    multi={props.multi}
    value={props.value}
    options={props.options}
    disabled={props.disabled}
    placeholder={props.placeholder}
    clearable={props.clearable}
    className={`${props.type} ${props.kind}`}
    tabSelectsValue={props.tabSelectsValue}
    searchable={props.searchable}
    optionComponent={props.optionComponent}
    valueComponent={props.valueComponent}
    onChange={onChange} />;
}

export default css(Select, styles);

A preliminary stab in the dark attempt to convert it (but not styling) to react-select V2 looks like:

import React from 'react';
import ReactSelect, { components } from 'react-select';
import css from 'react-css-modules';
import styles from './Select.css';

/**
 * Select component
 * How do we style this?
 */
const Select = (props) => {
  const onChange = values => {
    props.multi ? props.onChange(_.map(values, obj => obj.value)) : props.onChange(values.value)
  };

  const Options = (props) => {
    return (
        <components.Option {...props.optionComponent}/>
        /*<components.ValueContainer {...props.valueComponent}/>*/
    );
  };

  const MultiValueContainer = (props) => {
    return (
      <components.MultiValueContainer {...props.valueComponent}/>
    );
  };

  return <ReactSelect
    isMulti={props.multi}
    value={props.value}
    options={props.options}
    isDisabled={props.disabled}
    placeholder={props.placeholder}
    isClearable={props.clearable}
    className={`${props.type} ${props.kind}`}
    tabSelectsValue={props.tabSelectsValue}
    isSearchable={props.searchable}
    components={{ Options, MultiValueContainer }}
    onChange={onChange} />;
}

export default css(Select, styles);

A sample use in V1 looks like:

来源:https://stackoverflow.com/questions/53638863/how-to-upgrade-from-v1-to-v2-need-full-examples

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!