|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { DualListSelector } from '@patternfly/react-core'; |
| 4 | +import { useFieldApi } from '@data-driven-forms/react-form-renderer'; |
| 5 | +import isEqual from 'lodash/isEqual'; |
| 6 | + |
| 7 | +import FormGroup from '../form-group'; |
| 8 | +import DualListContext from '../dual-list-context'; |
| 9 | + |
| 10 | +export const convertOptions = (options, sort) => { |
| 11 | + if (Array.isArray(options)) { |
| 12 | + let result = options.map((option) => convertOptions(option, sort)).filter(Boolean); |
| 13 | + |
| 14 | + if (sort) { |
| 15 | + const sortFn = (a, b) => (sort === 'asc' ? a.localeCompare(b) : b.localeCompare(a)); |
| 16 | + |
| 17 | + result = result.sort((a, b) => sortFn(a.text || a.label, b.text || b.label)); |
| 18 | + } |
| 19 | + |
| 20 | + return result; |
| 21 | + } |
| 22 | + |
| 23 | + return { |
| 24 | + text: options.label, |
| 25 | + id: options.value || options.key || options.label || options.text, |
| 26 | + isChecked: false, |
| 27 | + ...options, |
| 28 | + ...(options.children && { children: convertOptions(options.children, sort).filter(Boolean) }) |
| 29 | + }; |
| 30 | +}; |
| 31 | + |
| 32 | +export const selectedOptions = (options, value, selected) => { |
| 33 | + if (Array.isArray(options)) { |
| 34 | + return options.map((option) => selectedOptions(option, value, selected)).filter(Boolean); |
| 35 | + } |
| 36 | + |
| 37 | + if (options.value) { |
| 38 | + if (selected ? value.includes(options.value) : !value.includes(options.value)) { |
| 39 | + return options; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (options.children) { |
| 44 | + const someSelected = selectedOptions(options.children, value, selected).filter(Boolean); |
| 45 | + |
| 46 | + if (someSelected.length) { |
| 47 | + return { |
| 48 | + ...options, |
| 49 | + children: someSelected |
| 50 | + }; |
| 51 | + } |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +export const getValueFromSelected = (options, newValue = []) => { |
| 56 | + if (Array.isArray(options)) { |
| 57 | + options.map((option) => getValueFromSelected(option, newValue)); |
| 58 | + } |
| 59 | + |
| 60 | + if (options.value) { |
| 61 | + newValue.push(options.value); |
| 62 | + } |
| 63 | + |
| 64 | + if (options.children) { |
| 65 | + getValueFromSelected(options.children, newValue); |
| 66 | + } |
| 67 | + |
| 68 | + return newValue; |
| 69 | +}; |
| 70 | + |
| 71 | +const DualListTreeSelect = (props) => { |
| 72 | + const { |
| 73 | + label, |
| 74 | + isRequired, |
| 75 | + helperText, |
| 76 | + meta, |
| 77 | + validateOnMount, |
| 78 | + description, |
| 79 | + hideLabel, |
| 80 | + id, |
| 81 | + input, |
| 82 | + FormGroupProps, |
| 83 | + options, |
| 84 | + isSortable, |
| 85 | + ...rest |
| 86 | + } = useFieldApi({ |
| 87 | + ...props, |
| 88 | + FieldProps: { |
| 89 | + isEqual: (current, initial) => isEqual([...(current || [])].sort(), [...(initial || [])].sort()) |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + const [sortConfig, setSortConfig] = useState(() => ({ left: isSortable && 'asc', right: isSortable && 'asc' })); |
| 94 | + |
| 95 | + const value = input.value || []; |
| 96 | + |
| 97 | + const leftOptions = selectedOptions(options, value, false); |
| 98 | + const rightOptions = selectedOptions(options, value, true); |
| 99 | + |
| 100 | + const onListChange = (_newLeft, newRight) => input.onChange(getValueFromSelected(newRight)); |
| 101 | + |
| 102 | + return ( |
| 103 | + <FormGroup |
| 104 | + label={label} |
| 105 | + isRequired={isRequired} |
| 106 | + helperText={helperText} |
| 107 | + meta={meta} |
| 108 | + validateOnMount={validateOnMount} |
| 109 | + description={description} |
| 110 | + hideLabel={hideLabel} |
| 111 | + id={id || input.name} |
| 112 | + FormGroupProps={FormGroupProps} |
| 113 | + > |
| 114 | + <DualListContext.Provider value={{ sortConfig, setSortConfig }}> |
| 115 | + <DualListSelector |
| 116 | + availableOptions={convertOptions(leftOptions, sortConfig.left)} |
| 117 | + chosenOptions={convertOptions(rightOptions, sortConfig.right)} |
| 118 | + onListChange={onListChange} |
| 119 | + id={id || input.name} |
| 120 | + isTree |
| 121 | + {...rest} |
| 122 | + /> |
| 123 | + </DualListContext.Provider> |
| 124 | + </FormGroup> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +DualListTreeSelect.propTypes = { |
| 129 | + label: PropTypes.node, |
| 130 | + validateOnMount: PropTypes.bool, |
| 131 | + isRequired: PropTypes.bool, |
| 132 | + helperText: PropTypes.node, |
| 133 | + description: PropTypes.node, |
| 134 | + hideLabel: PropTypes.bool, |
| 135 | + id: PropTypes.string, |
| 136 | + isSearchable: PropTypes.bool, |
| 137 | + isSortable: PropTypes.bool |
| 138 | +}; |
| 139 | + |
| 140 | +export default DualListTreeSelect; |
0 commit comments