代码之家  ›  专栏  ›  技术社区  ›  Ricardo Costa

如何将自定义道具从devExtreme发送到我的自定义搜索面板

  •  1
  • Ricardo Costa  · 技术社区  · 6 年前

    所以我想把我的自定义onChange函数传递给带有devextreme网格的ReactJS中的自定义插件。

    我的搜索面板覆盖如下:

    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 回复  |  直到 6 年前
        1
  •  1
  •   Grin    6 年前

    withComponents

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

    它只是为了:

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

    您必须在作用域中定义另一个输入包装器 myCustomFunctionFromProps 功能。

      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}
    

    或者(虽然是renundant):

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