All files / Settings/StaticSettingsField/StaticSettingsFieldComponent StaticSettingsFieldComponent.js

93.75% Statements 15/16
77.77% Branches 7/9
100% Functions 4/4
92.85% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61            27x             34x 16x   16x       8x 8x 4x     8x           6x   12x   6x     2x       27x                                    
import { useCallback, useMemo } from 'react';
import PropTypes from 'prop-types';
 
import { Select, TextField } from '@folio/stripes/components';
import RefdataButtons from '../../../RefdataButtons';
 
const StaticSettingsFieldComponent = ({
  refdata,
  setting = {},
  templates,
  ...fieldProps // Anything else will be passed directly onto the <Field> component
}) => {
  // Adding default sort to refdata object in ascending order by label
  const sortByLabel = useCallback((a, b) => (a.label.localeCompare(b.label)), []);
  const sortedRefdata = useMemo(() => refdata?.sort(sortByLabel), [refdata, sortByLabel]);
 
  switch (setting.settingType) {
    case 'Refdata':
      // Grab refdata values corresponding to setting
      // eslint-disable-next-line no-case-declarations
      let RefdataComponent = Select;
      if (refdata.length > 0 && refdata.length <= 4) {
        RefdataComponent = RefdataButtons;
      }
 
      return <RefdataComponent {...fieldProps} dataOptions={sortedRefdata} />;
    case 'Password':
      return <TextField {...fieldProps} type="password" />;
    case 'Template':
      // Grab template values corresponding to setting
      // eslint-disable-next-line no-case-declarations
      const selectTemplateValues = [
        { value: '', label: '' },
        ...templates.map(t => ({ value: t.id, label: t.name }))
      ];
      return <Select {...fieldProps} dataOptions={selectTemplateValues} />;
    default:
      // If in doubt, go with String
      return <TextField {...fieldProps} />;
  }
};
 
StaticSettingsFieldComponent.propTypes = {
  setting: PropTypes.shape({
    settingType: PropTypes.string,
    key: PropTypes.string,
  }),
  name: PropTypes.string.isRequired,
  refdata: PropTypes.arrayOf(PropTypes.shape({
    id: PropTypes.string,
    label: PropTypes.string,
    value: PropTypes.string,
  })),
  templates: PropTypes.arrayOf(PropTypes.shape({
    name: PropTypes.string,
    id: PropTypes.string
  }))
};
 
export default StaticSettingsFieldComponent;