• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1113439055

19 Dec 2023 09:01PM UTC coverage: 82.752% (-17.2%) from 99.964%
1113439055

Pull #4258

gitlab-ci

mender-test-bot
chore: Types update

Signed-off-by: Mender Test Bot <mender@northern.tech>
Pull Request #4258: chore: Types update

4326 of 6319 branches covered (0.0%)

8348 of 10088 relevant lines covered (82.75%)

189.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/src/js/components/common/forms/filters.js
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useEffect, useState } from 'react';
15
import { FormProvider, useForm } from 'react-hook-form';
16

17
import { makeStyles } from 'tss-react/mui';
18

19
import { TIMEOUTS } from '../../../constants/appConstants';
20
import { useDebounce } from '../../../utils/debouncehook';
21

22
const useStyles = makeStyles()(theme => ({
10✔
23
  filters: {
24
    backgroundColor: theme.palette.background.lightgrey,
25
    columnGap: theme.spacing(2),
26
    display: 'flex',
27
    flexWrap: 'wrap',
28
    padding: `10px ${theme.spacing(3)} ${theme.spacing(3)}`,
29
    rowGap: theme.spacing(2),
30
    '.filter-item': {
31
      display: 'grid'
32
    },
33
    '.filter-item > div': {
34
      alignSelf: 'end'
35
    }
36
  },
37
  filterReset: { right: theme.spacing(3) }
38
}));
39

40
export const Filters = ({ className = '', defaultValues, filters = [], initialValues, onChange, fieldResetTrigger = '', dirtyField, clearDirty }) => {
8!
41
  const { classes } = useStyles();
137✔
42
  const [values, setValues] = useState(initialValues);
137✔
43

44
  const methods = useForm({ mode: 'onChange', defaultValues });
137✔
45
  const { formState, reset, resetField, watch, setValue, getValues } = methods;
137✔
46
  const { isDirty } = formState;
137✔
47

48
  useEffect(() => {
137✔
49
    Object.entries(initialValues).map(([key, value]) => setValue(key, value));
63✔
50
    // eslint-disable-next-line react-hooks/exhaustive-deps
51
  }, [JSON.stringify(initialValues), setValue]);
52

53
  useEffect(() => {
137✔
54
    if (dirtyField && !formState.isDirty) {
137✔
55
      setValue(dirtyField, getValues(dirtyField), { shouldDirty: true });
1✔
56
      clearDirty('');
1✔
57
    }
58
  }, [clearDirty, dirtyField, formState, getValues, setValue]);
59

60
  useEffect(() => {
137✔
61
    if (!fieldResetTrigger) {
10!
62
      return;
10✔
63
    }
64
    resetField(fieldResetTrigger);
×
65
  }, [fieldResetTrigger, resetField]);
66

67
  watch(setValues);
137✔
68
  const debouncedValues = useDebounce(values, TIMEOUTS.oneSecond);
137✔
69

70
  useEffect(() => {
137✔
71
    onChange(debouncedValues);
14✔
72
    // eslint-disable-next-line react-hooks/exhaustive-deps
73
  }, [JSON.stringify(debouncedValues), onChange]);
74

75
  return (
137✔
76
    <FormProvider {...methods}>
77
      <form className={`margin-bottom relative margin-top ${classes.filters} ${className}`} noValidate>
78
        {filters.map(({ key, title, Component, componentProps }) => (
79
          <div className="filter-item" key={key}>
432✔
80
            <h5 className="margin-top-small margin-bottom-none muted">{title}</h5>
81
            <Component name={key} {...componentProps} />
82
          </div>
83
        ))}
84
        {isDirty && (
146✔
85
          <span className={`link absolute ${classes.filterReset}`} onClick={() => reset()}>
2✔
86
            Clear filter
87
          </span>
88
        )}
89
      </form>
90
    </FormProvider>
91
  );
92
};
93

94
export default Filters;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc