| 10 | cursor = 0; |
| 11 | |
| 12 | constructor(opts: SelectKeyOptions<T>) { |
| 13 | super(opts, false); |
| 14 | |
| 15 | this.options = opts.options; |
| 16 | const caseSensitive = opts.caseSensitive === true; |
| 17 | const keys = this.options.map(({ value: [initial] }) => { |
| 18 | return caseSensitive ? initial : initial?.toLowerCase(); |
| 19 | }); |
| 20 | this.cursor = Math.max(keys.indexOf(opts.initialValue), 0); |
| 21 | |
| 22 | this.on('key', (key) => { |
| 23 | if (!key) { |
| 24 | return; |
| 25 | } |
| 26 | const casedKey = caseSensitive ? key : key.toLowerCase(); |
| 27 | if (!keys.includes(casedKey)) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const value = this.options.find(({ value: [initial] }) => { |
| 32 | return caseSensitive ? initial === casedKey : initial?.toLowerCase() === casedKey; |
| 33 | }); |
| 34 | if (value) { |
| 35 | this.value = value.value; |
| 36 | this.state = 'submit'; |
| 37 | this.emit('submit'); |
| 38 | } |
| 39 | }); |
| 40 | } |
| 41 | } |