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

Autocomplete doesn't show the specific text value #276

Open
weslei-dias opened this issue Aug 22, 2018 · 0 comments
Open

Autocomplete doesn't show the specific text value #276

weslei-dias opened this issue Aug 22, 2018 · 0 comments

Comments

@weslei-dias
Copy link

I have a Autocomplete component whoose list an array of MenuItems. When i click in a specific value, only [Object Object] is show.

https://imgur.com/a/lYvEIS5

Here is my code:

class AutoCompleteEspecificacaoItemComponent extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      dataSource: [],
      hover: false,
      itemHover: '',
    };
  }

  onChangeDataSource = (value, event) => {
    const url = this.props.url;
    let textoAPesquisar = event;
    if (event.length >= this.props.minimunInputLength) {
      if (this.props.filtrarSomenteCodigo) {
        if (this.props.ehPropostaMaterialServico) {
          textoAPesquisar = formatarNumerosComZerosEsquerda(textoAPesquisar, 8);
        }
        else {
          textoAPesquisar = formatarNumerosComZerosEsquerda(textoAPesquisar, 9);
        }
      }
      axios.get(url, {
        params: {
          textoAPesquisar,
          page: 1,
          sizePerPage: 15,
          filtrarPorMaterial: this.props.filtrarPorMaterial,
          filtrarPorServico: this.props.filtrarPorServico,
        },
      })
        .then(({ data: { dados } }) => {
          const newArr = [];
          //eslint-disable-next-line
          dados.map((item) => {
            let codigoEspecificacao;

            if (this.props.filtrarSomenteCodigo) {
              codigoEspecificacao = item.codigo;
            }
            else if (this.props.ehPropostaMaterialServico) {
              codigoEspecificacao = item.nome;
            }
            else {
              codigoEspecificacao = item.especificacao;
            }
            const obj = {
              descricao: codigoEspecificacao,
            };
            newArr.push(obj);
          });
          this.setState({ dataSource: newArr });
        });
    }
  };

  mouseOver = (event) => {
    this.setState({
      hover: true,
      itemHover: event.target.textContent,
    });
  };

  mouseOut = (event) => {
    this.setState({
      hover: false,
      itemHover: event.target.textContent,
      searchText: '',
    });
  };

  filtro = (searchText, key) => {
    if (searchText) return key;
    return 'Sem resultados';
  };


  render() {
    return (
      <div style={{ whiteSpace: 'pre-wrap' }}>
        <Rotulo valor={this.props.rotulo} htmlFor={this.props.id} />
        {this.props.textoAjuda && <InfoButton info={this.props.textoAjuda} />}

        <Field
          id={this.props.id}
          name={this.props.name}
          component={AutoComplete}
          hintText={this.props.placeholder}
          openOnFocus
          onInput={this.props.onInput}
          filter={this.filtro}
          menuStyle={{ fontSize: '14px',
            lineHeight: 'unset',
            padding: '7px 0px',
            whiteSpace: 'normal',
            alignItems: 'center',
            display: 'inline-flex',
            minHeight: '28px',
            width: '100%',
          }}
          // dataSource={this.state.dataSource.map(item => item.descricao)}
          dataSource={this.state.dataSource.map(item => ({
            text: item.descricao,
            value: (
              <MenuItem
                primaryText={item.descricao}
                style={{ fontSize: '14px',
                  lineHeight: 'unset',
                  padding: '7px 0px',
                  whiteSpace: 'normal',
                  alignItems: 'center',
                  display: 'inline-flex',
                  minHeight: '28px',
                  width: '100%',
                }}
                className={this.state.hover && this.state.itemHover === item.descricao
          ? 'hoverColor' : 'noHoverColor'}
                onMouseOver={this.mouseOver}
                onMouseOut={this.mouseOut}
              />
            ),
          }))}
          onChange={this.onChangeDataSource}
          underlineStyle={{ fill: '#000000' }}
          style={{ marginTop: '-15px' }}
          listStyle={{ maxHeight: 200, overflow: 'auto', whiteSpace: 'wrap' }}
          fullWidth
        />
      </div>
    );
  }
}

Someone could help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant