2021-12-13 16:13:09 +00:00
|
|
|
import _ from 'lodash';
|
2022-02-02 02:41:52 +00:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2021-04-14 17:01:34 +00:00
|
|
|
import SelectSearch, { fuzzySearch } from 'react-select-search';
|
|
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
export const Multiselect = function Multiselect({
|
|
|
|
|
height,
|
2021-12-13 16:13:09 +00:00
|
|
|
properties,
|
|
|
|
|
styles,
|
|
|
|
|
exposedVariables,
|
|
|
|
|
setExposedVariable,
|
|
|
|
|
fireEvent,
|
|
|
|
|
}) {
|
|
|
|
|
const { label, value, values, display_values } = properties;
|
2022-02-02 02:41:52 +00:00
|
|
|
const { borderRadius, visibility, disabledState } = styles;
|
|
|
|
|
const selectRef = useRef(null);
|
2021-04-30 06:31:32 +00:00
|
|
|
|
2021-12-13 16:13:09 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
let newValues = [];
|
2021-04-30 06:31:32 +00:00
|
|
|
|
2021-12-13 16:13:09 +00:00
|
|
|
if (_.intersection(values, value)?.length === value?.length) newValues = value;
|
2021-04-30 06:31:32 +00:00
|
|
|
|
2021-12-13 16:13:09 +00:00
|
|
|
setExposedVariable('values', newValues);
|
|
|
|
|
setCurrentValue(newValues);
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [JSON.stringify(values)]);
|
2021-04-30 06:31:32 +00:00
|
|
|
|
2021-12-13 16:13:09 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
setExposedVariable('values', value);
|
|
|
|
|
setCurrentValue(value);
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [JSON.stringify(value)]);
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2021-12-13 16:13:09 +00:00
|
|
|
const [currentValue, setCurrentValue] = useState(() => value);
|
|
|
|
|
let selectOptions = [];
|
2021-08-30 11:43:05 +00:00
|
|
|
try {
|
2021-12-13 16:13:09 +00:00
|
|
|
selectOptions = [
|
|
|
|
|
...values.map((value, index) => {
|
|
|
|
|
return { name: display_values[index], value: value };
|
|
|
|
|
}),
|
|
|
|
|
];
|
2021-09-21 13:48:28 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
2021-08-30 11:43:05 +00:00
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
useEffect(() => {
|
2021-12-13 16:13:09 +00:00
|
|
|
if (value && !currentValue) {
|
|
|
|
|
setCurrentValue(properties.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (JSON.stringify(exposedVariables.values) === '{}') {
|
|
|
|
|
setCurrentValue(properties.value);
|
|
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-03-03 15:06:49 +00:00
|
|
|
}, []);
|
2021-12-13 16:13:09 +00:00
|
|
|
|
2022-02-02 02:41:52 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
selectRef.current.querySelector('.select-search__input').style.borderRadius = `${Number.parseFloat(
|
|
|
|
|
borderRadius
|
|
|
|
|
)}px`;
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [borderRadius, selectRef.current]);
|
|
|
|
|
|
2021-12-13 16:13:09 +00:00
|
|
|
const handleChange = (value) => {
|
2022-03-03 15:06:49 +00:00
|
|
|
// setCurrentValue(value);
|
2021-12-13 16:13:09 +00:00
|
|
|
setExposedVariable('values', value).then(() => fireEvent('onSelect'));
|
|
|
|
|
};
|
2021-04-30 06:31:32 +00:00
|
|
|
|
|
|
|
|
return (
|
2021-12-13 16:13:09 +00:00
|
|
|
<div className="multiselect-widget row g-0" style={{ height, display: visibility ? '' : 'none' }}>
|
2021-10-22 09:12:37 +00:00
|
|
|
<div className="col-auto my-auto">
|
2022-01-15 02:37:05 +00:00
|
|
|
<label style={{ marginRight: label ? '1rem' : '' }} className="form-label py-1">
|
2021-09-21 13:48:28 +00:00
|
|
|
{label}
|
|
|
|
|
</label>
|
2021-04-30 06:31:32 +00:00
|
|
|
</div>
|
2021-10-22 09:12:37 +00:00
|
|
|
<div className="col px-0 h-100">
|
2021-04-30 06:31:32 +00:00
|
|
|
<SelectSearch
|
2021-12-13 16:13:09 +00:00
|
|
|
disabled={disabledState}
|
2021-04-30 06:31:32 +00:00
|
|
|
options={selectOptions}
|
|
|
|
|
value={currentValue}
|
|
|
|
|
search={true}
|
|
|
|
|
multiple={true}
|
|
|
|
|
printOptions="on-focus"
|
2021-04-30 08:10:57 +00:00
|
|
|
onChange={(newValues) => {
|
2021-12-13 16:13:09 +00:00
|
|
|
handleChange(newValues);
|
2021-04-30 06:31:32 +00:00
|
|
|
}}
|
|
|
|
|
filterOptions={fuzzySearch}
|
|
|
|
|
placeholder="Select.."
|
2022-02-02 02:41:52 +00:00
|
|
|
ref={selectRef}
|
2022-02-28 08:37:13 +00:00
|
|
|
closeOnSelect={false}
|
2021-04-30 06:31:32 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-04-14 17:01:34 +00:00
|
|
|
};
|