UNPKG

@shopify/polaris

Version:

Shopify’s product component library

125 lines (113 loc) 4.04 kB
import React$1, { useState, useCallback } from 'react'; import { useFeatures } from '../../utilities/features/hooks.js'; import { useUniqueId } from '../../utilities/unique-id/hooks.js'; import { classNames } from '../../utilities/css.js'; import { arraysAreEqual } from '../../utilities/arrays.js'; import { useDeepEffect as useDeepEffect$1 } from '../../utilities/use-deep-effect.js'; import { Option as Option$1 } from './components/Option/Option.js'; import styles from './OptionList.scss.js'; function OptionList({ options, sections, title, selected, allowMultiple, role, optionRole, onChange, id: idProp }) { const [normalizedOptions, setNormalizedOptions] = useState(createNormalizedOptions(options, sections, title)); const id = useUniqueId('OptionList', idProp); const { newDesignLanguage } = useFeatures(); useDeepEffect$1(() => { setNormalizedOptions(createNormalizedOptions(options || [], sections || [], title)); }, [options, sections, title], optionArraysAreEqual); const handleClick = useCallback((sectionIndex, optionIndex) => { const selectedValue = normalizedOptions[sectionIndex].options[optionIndex].value; const foundIndex = selected.indexOf(selectedValue); if (allowMultiple) { const newSelection = foundIndex === -1 ? [selectedValue, ...selected] : [...selected.slice(0, foundIndex), ...selected.slice(foundIndex + 1, selected.length)]; onChange(newSelection); return; } onChange([selectedValue]); }, [normalizedOptions, selected, allowMultiple, onChange]); const optionsExist = normalizedOptions.length > 0; const optionsMarkup = optionsExist ? normalizedOptions.map(({ title, options }, sectionIndex) => { const titleMarkup = title ? /*#__PURE__*/React$1.createElement("p", { className: styles.Title }, title) : null; const optionsMarkup = options && options.map((option, optionIndex) => { const isSelected = selected.includes(option.value); const optionId = option.id || `${id}-${sectionIndex}-${optionIndex}`; return /*#__PURE__*/React$1.createElement(Option$1, Object.assign({}, option, { key: optionId, id: optionId, section: sectionIndex, index: optionIndex, onClick: handleClick, select: isSelected, allowMultiple: allowMultiple, role: optionRole })); }); return /*#__PURE__*/React$1.createElement("li", { key: title || `noTitle-${sectionIndex}` }, titleMarkup, /*#__PURE__*/React$1.createElement("ul", { className: styles.Options, id: `${id}-${sectionIndex}`, role: role }, optionsMarkup)); }) : null; const optionListClassName = classNames(styles.OptionList, newDesignLanguage && styles.newDesignLanguage); return /*#__PURE__*/React$1.createElement("ul", { className: optionListClassName, role: role }, optionsMarkup); } function createNormalizedOptions(options, sections, title) { if (options == null) { const section = { options: [], title }; return sections == null ? [] : [section, ...sections]; } if (sections == null) { return [{ title, options }]; } return [{ title, options }, ...sections]; } function isSection(arr) { return typeof arr[0] === 'object' && Object.prototype.hasOwnProperty.call(arr[0], 'options'); } function optionArraysAreEqual(firstArray, secondArray) { if (isSection(firstArray) && isSection(secondArray)) { return arraysAreEqual(firstArray, secondArray, testSectionsPropEquality); } return arraysAreEqual(firstArray, secondArray); } function testSectionsPropEquality(previousSection, currentSection) { const { options: previousOptions } = previousSection; const { options: currentOptions } = currentSection; const optionsAreEqual = arraysAreEqual(previousOptions, currentOptions); const titlesAreEqual = previousSection.title === currentSection.title; return optionsAreEqual && titlesAreEqual; } export { OptionList };