1 |
|
---|
2 | import * as React from 'react';
|
---|
3 | import OutlinedInput from '@mui/material/OutlinedInput';
|
---|
4 | import InputLabel from '@mui/material/InputLabel';
|
---|
5 | import MenuItem from '@mui/material/MenuItem';
|
---|
6 | import FormControl from '@mui/material/FormControl';
|
---|
7 | import ListItemText from '@mui/material/ListItemText';
|
---|
8 | import Select from '@mui/material/Select';
|
---|
9 | import Checkbox from '@mui/material/Checkbox';
|
---|
10 | import "./FilterSelectComponent.css"
|
---|
11 | import axios from 'axios';
|
---|
12 |
|
---|
13 | export class FilterSelectComponent extends React.Component {
|
---|
14 |
|
---|
15 | constructor(props) {
|
---|
16 | super(props)
|
---|
17 |
|
---|
18 | this.state = {
|
---|
19 | pickedItems: [],
|
---|
20 | items: [],
|
---|
21 | type: '',
|
---|
22 | ITEM_HEIGHT: 48,
|
---|
23 | ITEM_PADDING_TOP: 8,
|
---|
24 | MenuProps: {}
|
---|
25 | }
|
---|
26 |
|
---|
27 | }
|
---|
28 |
|
---|
29 | componentDidMount(){
|
---|
30 | this.state.MenuProps = {
|
---|
31 | PaperProps: {
|
---|
32 | style: {
|
---|
33 | maxHeight: this.state.ITEM_HEIGHT * 5.5 + this.state.ITEM_PADDING_TOP,
|
---|
34 | width: 250,
|
---|
35 | },
|
---|
36 | },
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | let endpoint
|
---|
41 | if(this.props.type == 'brands')
|
---|
42 | {
|
---|
43 | endpoint = '/brands'
|
---|
44 | this.setState({
|
---|
45 | type: 'Брендови'
|
---|
46 | })
|
---|
47 | }
|
---|
48 | else{
|
---|
49 | endpoint = '/shops'
|
---|
50 | this.setState({
|
---|
51 | type: 'Продавници'
|
---|
52 | })
|
---|
53 | }
|
---|
54 |
|
---|
55 | axios.get(endpoint)
|
---|
56 | .then(response => this.setState({items: response.data}))
|
---|
57 | .catch(error => console.log(error))
|
---|
58 | }
|
---|
59 |
|
---|
60 | handleChange = (event) => {
|
---|
61 | let value = event.target.value
|
---|
62 | this.setState({
|
---|
63 | pickedItems: typeof value === 'string' ? value.split(',') : value
|
---|
64 | }, ()=>{
|
---|
65 | if(this.props.type == 'brands')
|
---|
66 | {
|
---|
67 | this.props.changeHandler({brands: this.state.pickedItems.join(',')})
|
---|
68 | }
|
---|
69 |
|
---|
70 | if(this.props.type == 'shops')
|
---|
71 | {
|
---|
72 | this.props.changeHandler({shops: this.state.pickedItems.join(',')})
|
---|
73 | }
|
---|
74 |
|
---|
75 | })
|
---|
76 | };
|
---|
77 |
|
---|
78 |
|
---|
79 | render() {
|
---|
80 | return (
|
---|
81 | <div>
|
---|
82 | <FormControl className="form-select-component" sx={{ m: 1, width: 200 }}>
|
---|
83 | <InputLabel className="input-select-label">{this.state.type}</InputLabel>
|
---|
84 | <Select
|
---|
85 | size={"small"}
|
---|
86 | labelId="input-label-id"
|
---|
87 | className="input-select-option"
|
---|
88 | multiple
|
---|
89 | value={this.state.pickedItems}
|
---|
90 | onChange={this.handleChange}
|
---|
91 | input={<OutlinedInput className='inner-input-selectfilter' label={this.state.type} />}
|
---|
92 | renderValue={(selected) => selected.join(', ')}
|
---|
93 | MenuProps={this.state.MenuProps}
|
---|
94 | >
|
---|
95 | {this.state.items.map((item) => (
|
---|
96 | <MenuItem key={item} value={item}>
|
---|
97 | <Checkbox checked={this.state.pickedItems.indexOf(item) > -1} />
|
---|
98 | <ListItemText primary={item} />
|
---|
99 | </MenuItem>
|
---|
100 | ))}
|
---|
101 | </Select>
|
---|
102 | </FormControl>
|
---|
103 | </div>
|
---|
104 | )
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | export default FilterSelectComponent
|
---|