|
| 1 | +import React, { useMemo, useState } from 'react'; |
| 2 | +import { |
| 3 | + IconButton, |
| 4 | + MultiSelectBox_ListPreviewSortable, |
| 5 | + SelectBox_Option_SingleLine, |
| 6 | + TextInput, |
| 7 | +} from '@neos-project/react-ui-components'; |
| 8 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 9 | +// @ts-ignore |
| 10 | +import styles from './StringsEditor.module.css'; |
| 11 | + |
| 12 | +type StringsEditorProps = { |
| 13 | + id: string; |
| 14 | + value: string[]; |
| 15 | + options: unknown; |
| 16 | + commit: (value: string[]) => void; |
| 17 | + i18nRegistry: { translate: (key: string) => string }; |
| 18 | + disabled?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +const valueAccessor = (x: { key: string }): string => x.key; |
| 22 | + |
| 23 | +const isObject = (e: unknown): e is Record<string, unknown> => { |
| 24 | + return typeof e === 'object' && e !== null && !Array.isArray(e); |
| 25 | +}; |
| 26 | + |
| 27 | +type StringsEditorOptions = { |
| 28 | + unique: boolean; |
| 29 | + placeholder?: string; |
| 30 | + maximumItems?: number; |
| 31 | +} |
| 32 | + |
| 33 | +const getOptions = (options: unknown): StringsEditorOptions => { |
| 34 | + const result: StringsEditorOptions = { |
| 35 | + unique: true, |
| 36 | + }; |
| 37 | + |
| 38 | + if (isObject(options)) { |
| 39 | + if ('unique' in options) { |
| 40 | + result.unique = options.unique !== false; |
| 41 | + } |
| 42 | + |
| 43 | + if (typeof options.placeholder === 'string') { |
| 44 | + result.placeholder = options.placeholder; |
| 45 | + } |
| 46 | + |
| 47 | + if (typeof options.maximumItems === 'number' && result.maximumItems > 0) { |
| 48 | + result.maximumItems = options.maximumItems; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return result; |
| 53 | +}; |
| 54 | + |
| 55 | +export const StringsEditor = (props: StringsEditorProps) => { |
| 56 | + console.log('StringsEditor', props); |
| 57 | + const [ input, setInput ] = useState(''); |
| 58 | + |
| 59 | + const { |
| 60 | + unique, |
| 61 | + placeholder, |
| 62 | + maximumItems, |
| 63 | + } = useMemo(() => { |
| 64 | + return getOptions(props.options); |
| 65 | + }, [ props.options ]); |
| 66 | + |
| 67 | + const inputEmpty = input.trim().length === 0; |
| 68 | + const inputUnique = !unique || !props.value.includes(input); |
| 69 | + const hasMaxItems = maximumItems !== undefined && props.value.length >= maximumItems; |
| 70 | + const inputValid = !inputEmpty && inputUnique && !hasMaxItems; |
| 71 | + |
| 72 | + const handleSubmit = () => { |
| 73 | + if (!inputValid) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + props.commit([ ...props.value, input ]); |
| 78 | + setInput(''); |
| 79 | + }; |
| 80 | + |
| 81 | + const handleKeyPress = (e: React.KeyboardEvent) => { |
| 82 | + if (e.key === 'Enter') { |
| 83 | + e.preventDefault(); |
| 84 | + handleSubmit(); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + const options = useMemo(() => { |
| 89 | + return props.value.map(item => ({ |
| 90 | + label: item, |
| 91 | + key: item, |
| 92 | + })); |
| 93 | + }, [ props.value ]); |
| 94 | + |
| 95 | + return ( |
| 96 | + <div> |
| 97 | + <ul className={styles.list}> |
| 98 | + <MultiSelectBox_ListPreviewSortable |
| 99 | + disabled={props.disabled} |
| 100 | + values={props.value} |
| 101 | + options={options} |
| 102 | + optionValueAccessor={valueAccessor} |
| 103 | + onValuesChange={props.commit} |
| 104 | + ListPreviewElement={SelectBox_Option_SingleLine} |
| 105 | + dndType="multiselect-box-value" |
| 106 | + /> |
| 107 | + </ul> |
| 108 | + <div className={styles.inputWrapper}> |
| 109 | + <TextInput |
| 110 | + id={props.id} |
| 111 | + value={input} |
| 112 | + onChange={setInput} |
| 113 | + onKeyUp={handleKeyPress} |
| 114 | + disabled={props.disabled || hasMaxItems} |
| 115 | + placeholder={placeholder ? props.i18nRegistry.translate(placeholder) : undefined} |
| 116 | + className={styles.input} |
| 117 | + /> |
| 118 | + <IconButton |
| 119 | + icon="check" |
| 120 | + onClick={handleSubmit} |
| 121 | + disabled={props.disabled || !inputValid} |
| 122 | + className={styles.submitButton} |
| 123 | + style="neutral" |
| 124 | + /> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
0 commit comments