lundi 10 août 2020

Mutiple filtering checkboxes react

import React, { Component } from "react";
import { Dropdown, Label, Icon, Checkbox } from "semantic-ui-react";
import "../../../styles/mobile.css";
import "../../../styles/player.css";

import SearchBox from "../../../../../components/SearchBox/SearchBox";
import { connect } from "react-redux";
import { dispatch } from "d3";
import { setSearchField } from "../../../../../actions/actions";
import playbutton from "../../../../../assets/Playbutton/play-circle.svg";

const allOption = [
  {
    text: "All",
    value: "all",
  },
];

const mapStateToProps = (state) => {
  return {
    searchField: state.searchMusic.searchField,
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    onSearchChange: (event) => dispatch(setSearchField(event.target.value)),
  };
};

class FilterTracks extends Component {
  state = {
    selectedGenre: {},
    selectedInstrument: {},
    instruments: [" "],
 checked:false
  };
  //for filteration

  onChange = (name, value) => {
    this.setState({
      checked : true,
     [name]: value
    
    })
    
    if (name === "selectedRegion") {
      this.props.updateRegion(value);
    }
  };

  render() {
    const { selectedGenre, selectedInstrument } = this.state;
    const {
      song,
      music,
      playSong,
      genres,
      instruments,
      regions,
      selectedRegion,
      onSearchChange,
    } = this.props;
    const genreOptions = allOption.concat(
      genres.map((genre) =>
        Object.assign(
          {},
          {
            text: genre.name,
            value: genre.id,
          }
        )
      )
    );

    const musicIns = music.map((instrument, index) => {
      return music[index].instruments;
    });
    const instrumentsId = instruments.map((id, index) => {
      return instruments[index].id;
    });

    const handleCheckbox = (e) => {
      let val = e.target.value;
      console.log({ val });
      if (musicIns.includes(instrumentsId)) {
        this.setState((prevState) => ({
          instruments: prevState.instruments.filter((instrument) => instrument !== val),
        }));
      } else {
        this.setState((prevState) => ({
          instruments: [...prevState.instruments, val],
        }));
      }
    };

    console.log(musicIns);
    console.log(instrumentsId);
    const instrumentsOptions = allOption.concat(
      instruments.map((instrument) =>
        Object.assign(
          {},
          {
            text: instrument.name,
            value: instrument.id,
          }
        )
      )
    );
    const regionsOptions = allOption.concat(
      regions.map((region) =>
        Object.assign(
          {},
          {
            text: region.name,
            value: region.id,
          }
        )
      )
    );




    const filterMusic = music.filter((song) => {
      const { genre, instruments, region } = song;
      const regionFilter =
        !selectedRegion.value || selectedRegion.value === "all" || selectedRegion.value === region;
      const instrumentsFilter =
        !selectedInstrument.value ||
        selectedInstrument.value === "all" ||
        // instruments.sort((id) => id === selectedInstrument.value) ||
        instruments.some((id) => id === selectedInstrument.value);
      const genreFilter =
        !selectedGenre.value ||
        selectedGenre.value === "all" ||
        genre.some((id) => id === selectedGenre.value);
      return regionFilter && instrumentsFilter && genreFilter;
    });

    return (
      <div>
        <div className="filter-container">
          <div className="title">Filter:</div>
          {/* <div> */}
          {/* <Dropdown
              style=
              text="Genre"
              className="dropdown-container"
            >
              <Dropdown.Menu style=>
                {genreOptions.map((genre, index) => {
                  return (
                    <Dropdown.Item
                      className="dropdown-item"
                      text={genre.text}
                      onClick={() => {
                        this.onChange("selectedGenre", genre);
                      }}
                    />
                  );
                })}
              </Dropdown.Menu>
            </Dropdown>
            {selectedGenre.value && <Label className="selected-label">{selectedGenre.text}</Label>}
          </div> */}
          <div>
            <Dropdown
              text="Instruments"
              style=
              className="dropdown-container"
            >
              <Dropdown.Menu style=>
                {instrumentsOptions.map((instrument, index) => {
                  console.log(instrument.text);
                  return (
                    <Checkbox
                      fitted
                      className="dropdown-item"
                      label={instrument.text}
                      value={instrument.value}
                      name={instrument.text}
                      
                      onMouseDown={() => {
                        this.onChange("selectedInstrument", instrument);
                      }}
                    />
                  );
                })}
              </Dropdown.Menu>
            </Dropdown>
            {selectedInstrument.value && (
              <Label className="selected-label">{selectedInstrument.text}</Label>
            )}
          </div>
          <div>
            <Dropdown
              text="Regions"
              className="dropdown-container"
              style=
            >
              <Dropdown.Menu style=>
                {regionsOptions.map((region, index) => {
                  return (
                    <Dropdown.Item
                      className="dropdown-item"
                      text={region.text}
                      onClick={() => {
                        this.onChange("selectedRegion", region);
                      }}
                    />
                  );
                })}
              </Dropdown.Menu>
            </Dropdown>
            {selectedRegion.value && (
              <Label className="selected-label">{selectedRegion.text}</Label>
            )}
          </div>
        </div>
        <div className="playlist">
          {filterMusic.map((song, index) => {
            return (
              <div
                className="song-item"
                key={index}
                style=
                onClick={() => {
                  playSong(song);
                }}
              >
                <div className="middle aligned content">
                  <img alt="playbutton" className="mr2" src={playbutton} />
                  {song.title}
                </div>
              </div>
            );
          })}
        </div>
      </div>
    );
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(FilterTracks);

attached are my fields of filtering

Above is my code for filtering data from radio button but for the instruments Dropdown i tried making it as a checkbox which filters multiple fields but it still acts as a radio button how do I achieve this?. Im getting my data from firebase database

I can filter my data with just single field but i want it to be multiple field, how can i achieve this?




Aucun commentaire:

Enregistrer un commentaire