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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 27x 27x | import { forwardRef, useImperativeHandle } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import {
IconButton,
Icon,
} from '@folio/stripes/components';
import css from '../../../styles/IconSelect.css';
import RichSelect, { useSelectedOption } from '../RichSelect';
import FormattedKintMessage from '../FormattedKintMessage';
// A form component which acts as a "Select for states with corresponding icons"
const IconSelect = forwardRef(({
ariaLabel,
disabled = false,
id,
input,
intlKey: passedIntlKey,
intlNS: passedIntlNS,
label,
meta,
notSet,
onChange,
options: userOptions = [],
required = false,
value
}, ref) => {
const defaultNotSet = {
icon: 'ellipsis',
value: '',
label: <FormattedKintMessage
id="notSet"
intlKey={passedIntlKey}
intlNS={passedIntlNS}
/>
};
// Options with notSet
const options = [
notSet ?? defaultNotSet,
...userOptions
];
const [richSelectRef, selectedOption] = useSelectedOption();
useImperativeHandle(ref, () => ({
selectedOption
}));
const { className: iconButtonClassName, ...buttonProps } = selectedOption?.buttonProps ?? {};
return (
<RichSelect
ref={richSelectRef}
ariaLabel={ariaLabel}
disabled={disabled}
id={id}
input={input}
label={label}
meta={meta}
onChange={onChange}
options={options}
renderOption={(opt) => {
if (opt.icon) {
return (
<Icon
icon={opt.icon}
{...opt.iconProps}
>
{opt.label ?? opt.value}
</Icon>
);
}
return opt.label ?? opt.value;
}}
renderTrigger={({ onToggle, triggerRef, keyHandler, ariaProps, getTriggerProps }) => {
return (
<IconButton
ref={triggerRef}
disabled={disabled}
icon={selectedOption?.icon ?? 'ellipsis'}
marginBottom0
onClick={onToggle}
onKeyDown={keyHandler}
type="button"
{...{
className: classnames(
css.buttonStyle,
iconButtonClassName
),
...buttonProps
}}
{...getTriggerProps()}
{...ariaProps}
/>
);
}}
required={required}
value={value}
/>
);
});
IconSelect.propTypes = {
ariaLabel: PropTypes.string,
disabled: PropTypes.bool,
id: PropTypes.string,
input: PropTypes.object,
intlKey: PropTypes.string,
intlNS: PropTypes.string,
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.node
]),
meta: PropTypes.object,
notSet: PropTypes.shape({
icon: PropTypes.string,
value: PropTypes.string.isRequired,
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node
])
}),
onChange: PropTypes.func,
options: PropTypes.arrayOf(PropTypes.shape({
icon: PropTypes.string,
value: PropTypes.string.isRequired,
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node
])
})),
required: PropTypes.bool,
value: PropTypes.string,
};
export default IconSelect;
|