UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

43 lines 1.45 kB
import "core-js/modules/es.string.replace.js"; import React, { useMemo } from 'react'; import { useItem } from '../hooks'; import { useTranslation } from '../../hooks'; import { convertJsxToString } from '../../../../shared/component-helper'; export function useIterateItemNo(_ref) { let { label: labelProp, labelSuffix, required } = _ref; const { index: iterateIndex } = useItem() || {}; const { optionalLabelSuffix } = useTranslation().Field; const labelSuffixText = useMemo(() => { if (required === false || typeof labelSuffix !== 'undefined') { return labelSuffix !== null && labelSuffix !== void 0 ? labelSuffix : optionalLabelSuffix; } return ''; }, [required, labelSuffix, optionalLabelSuffix]); return useMemo(() => { let content = labelProp; if (iterateIndex !== undefined) { content = convertJsxToString(labelProp).replace('{itemNo}', String(iterateIndex + 1)); } if (labelSuffixText) { if (convertJsxToString(content).includes(optionalLabelSuffix)) { return content; } if (typeof content === 'string') { return content + ' ' + labelSuffixText; } if (React.isValidElement(content)) { return React.createElement(React.Fragment, null, content, ' ', labelSuffixText); } } return content; }, [iterateIndex, labelProp, labelSuffixText, optionalLabelSuffix]); } //# sourceMappingURL=useIItemNo.js.map