如何从devExtreme

时间:2018-10-10 12:35:47

标签: javascript reactjs devextreme

所以我想通过devextreme网格将自定义onChange函数传递给ReactJS中的自定义插件。

我的searchPanel覆盖如下:

import { withComponents } from '@devexpress/dx-react-core';
import { SearchPanel as SearchPanelBase } from '@devexpress/dx-react-grid';
import { SearchPanelInput as Input } from './SearchPanelInputBase';

export const SearchPanel = withComponents({ Input })(SearchPanelBase);

然后是我的searchPanelInputBase

import * as React from 'react';
import * as PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Input from '@material-ui/core/Input';
import InputAdornment from '@material-ui/core/InputAdornment';
import Search from '@material-ui/icons/Search';
import { Switch } from '@material-ui/core';
import StoreUsers from '../store/StoreUsers';

const styles = theme => ({
  root: {
    display: 'flex',
    alignItems: 'center',
    color: theme.palette.action.active,
  },
  flexSpaced: {
    display: 'flex',
    flexDirection: 'row',
    justifyContent: 'space-between',
    width: '100%',
  }
});


let getActives = false

const SearchPanelInputBase = ({
  myCustomFunctionFromProps, classes, onValueChange, value, getMessage, props, ...restProps
}) => (
  <div className={classes.flexSpaced}>
    <Switch
      onChange={myCustomFunctionFromProps}
    />
    <Input
      onChange={e => onValueChange(e.target.value)}
      value={value}
      type="text"
      placeholder={getMessage('searchPlaceholder')}
      {...restProps}
      startAdornment={(
        <InputAdornment position="start">
          <Search />
        </InputAdornment>
    )}
      />
  </div>
);

SearchPanelInputBase.propTypes = {
  myCustomFunctionFromProps: PropTypes.func.isRequired,
  onValueChange: PropTypes.func.isRequired,
  value: PropTypes.string,
  getMessage: PropTypes.func.isRequired,
};
SearchPanelInputBase.defaultProps = {
  value: '',
};

export const SearchPanelInput = withStyles(styles)(SearchPanelInputBase);

最后我打电话到我想要的地方

      <SearchPanel
        inputComponent={props => (
          <SearchPanelInput myCustomFunctionFromProps={this.myCustomFunctionFromProps} {...props} />
        )}
      />

但是这不起作用,我的道具类型说该函数未定义,我怀疑道具没有正确展开,但是我不知道该如何覆盖其他组件

编辑:

我的功能

  myCustomFunctionFromProps = () => console.log('lel')

1 个答案:

答案 0 :(得分:1)

withComponents没有传递您的自定义道具。

它可以与多个组件一起使用:

export const Table = withComponents({ Row, Cell })(TableBase);

它只是针对:

    <TableBase
      rowComponent={Row}
      cellComponent={Cell}
    />

那么它怎么知道定制道具要去哪一个呢?

您必须使用myCustomFunctionFromProps函数在范围内定义另一个Input包装器。

  Input2 = props => (
    <SearchPanelInput myCustomFunctionFromProps={this.myCustomFunctionFromProps} {...props} />
  )

并使用SearchPanel中的@devexpress/dx-react-grid

    <SearchPanel
      inputComponent={this.Input2}
    />

还应该更改:

onChange={() => myCustomFunctionFromProps}
myCustomFunctionFromProps={() => this.myCustomFunctionFromProps}

收件人:

onChange={myCustomFunctionFromProps}
myCustomFunctionFromProps={this.myCustomFunctionFromProps}

或(尽管它是多余的):

onChange={() => myCustomFunctionFromProps()}
myCustomFunctionFromProps={() => this.myCustomFunctionFromProps()}