lodash#xor JavaScript Examples

The following examples show how to use lodash#xor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js    From datapass with GNU Affero General Public License v3.0 5 votes vote down vote up
MultiSelect = ({
  options = [],
  values = [],
  disabled = false,
  onChange,
}) => {
  const [isContentOpen, setIsContentOpen] = useState(false);

  const handleButtonClick = () => {
    setIsContentOpen(!isContentOpen);
  };

  const handleOutsideClick = () => {
    setIsContentOpen(false);
  };

  const handleChange = (event) => {
    const {
      target: { name, checked },
    } = event;
    let newValues = checked ? union(values, [name]) : xor(values, [name]);
    onChange(newValues);
  };

  let overviewLabel;
  if (values.length === 0) {
    overviewLabel = 'Tous';
  } else if (values.length === 1) {
    overviewLabel = options.find(({ key }) => key === values[0])?.label;
  } else {
    overviewLabel = `${values.length} sélections`;
  }

  return (
    <div>
      <div className="multiselect-dropdown-button" onClick={handleButtonClick}>
        {overviewLabel}
      </div>
      {isContentOpen && (
        <Dropdown onOutsideClick={handleOutsideClick}>
          <FieldsetWrapper small>
            {options.map(({ key, label }) => (
              <div key={key}>
                <CheckboxInput
                  name={key}
                  label={label}
                  onChange={handleChange}
                  value={values.includes(key)}
                  disabled={disabled}
                />
              </div>
            ))}
          </FieldsetWrapper>
        </Dropdown>
      )}
    </div>
  );
}