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 | import React, { FC, useEffect, useState } from "react"; import { useAppSelector } from "store/hooks"; import { InstrumentType } from "store/features/editor/editorState"; import styled from "styled-components"; import { Option, Select, OptionLabelWithPreview, SingleValueWithPreview, SelectCommonProps, } from "ui/form/Select"; import { SingleValue } from "react-select"; const defaultInstrumentOptions = Array(15) .fill("") .map((_, i) => ({ value: `${i}`, label: `Instrument ${i + 1}`, })) as Option[]; interface LabelColorProps { $color: string; } const LabelColor = styled.div.attrs<LabelColorProps>((props) => ({ className: `label--instrument-${props.$color}`, }))` width: 10px; height: 10px; border-radius: 10px; flex-shrink: 0; margin-left: 5px; `; interface InstrumentSelectProps extends SelectCommonProps { name: string; value?: string; onChange?: (newId: string) => void; optional?: boolean; optionalLabel?: string; optionalDefaultInstrumentId?: string; instrumentType?: InstrumentType; } export const InstrumentSelect: FC<InstrumentSelectProps> = ({ value, instrumentType, onChange, ...selectProps }) => { const [options, setOptions] = useState<Option[]>([]); const [currentInstrument, setCurrentInstrument] = useState<Option>(); const [currentValue, setCurrentValue] = useState<Option>(); const song = useAppSelector((state) => state.trackerDocument.present.song); useEffect(() => { let instruments = defaultInstrumentOptions; Iif (song) { switch (instrumentType) { case "duty": instruments = song?.duty_instruments.map((instrument) => ({ value: `${instrument.index}`, label: instrument.name || `Duty ${instrument.index + 1}`, })); break; case "wave": instruments = song?.wave_instruments.map((instrument) => ({ value: `${instrument.index}`, label: instrument.name || `Wave ${instrument.index + 1}`, })); break; case "noise": instruments = song?.noise_instruments.map((instrument) => ({ value: `${instrument.index}`, label: instrument.name || `Noise ${instrument.index + 1}`, })); break; } } setOptions(([] as Option[]).concat([] as Option[], instruments)); }, [instrumentType, song]); useEffect(() => { setCurrentInstrument(options.find((v) => v.value === value)); }, [options, value]); useEffect(() => { if (currentInstrument) { setCurrentValue(currentInstrument); } else { const firstInstrument = options[0]; Iif (firstInstrument) { setCurrentValue(firstInstrument); } } }, [currentInstrument, options]); const onSelectChange = (newValue: SingleValue<Option>) => { Iif (newValue) { onChange?.(newValue.value); } }; return ( <Select value={currentValue} options={options} onChange={onSelectChange} formatOptionLabel={(option: Option) => { return ( <OptionLabelWithPreview preview={<LabelColor $color={option.value} />} > {option.label} </OptionLabelWithPreview> ); }} components={{ SingleValue: () => ( <SingleValueWithPreview preview={<LabelColor $color={value || ""} />}> {currentValue?.label} </SingleValueWithPreview> ), }} {...selectProps} /> ); }; |