3 const Select = require('./select');
4 const Form = require('./form');
5 const form = Form.prototype;
7 class Editable extends Select {
9 super({ ...options, multiple: true });
10 this.align = [this.options.align, 'left'].find(v => v != null);
16 let choice = this.focused;
17 let parent = choice.parent || {};
18 if (!choice.editable && !parent.editable) {
19 if (char === 'a' || char === 'i') return super[char]();
21 return form.dispatch.call(this, char, key);
25 return form.append.call(this, char, key);
29 return form.delete.call(this, char, key);
33 return this.focused.editable ? this.append(char) : super.space();
37 return this.focused.editable ? this.append(char) : super.number(char);
41 return this.focused.editable ? form.next.call(this) : super.next();
45 return this.focused.editable ? form.prev.call(this) : super.prev();
48 async indicator(choice, i) {
49 let symbol = choice.indicator || '';
50 let value = choice.editable ? symbol : super.indicator(choice, i);
51 return await this.resolve(value, this.state, choice, i) || '';
55 return choice.role === 'heading' ? '' : (choice.editable ? ' ' : ' ');
58 async renderChoice(choice, i) {
60 if (choice.editable) return form.renderChoice.call(this, choice, i);
61 return super.renderChoice(choice, i);
69 return this.state.error;
75 for (let choice of this.choices) {
76 if (typeof choice.validate !== 'function') {
80 if (choice.role === 'heading') {
84 let val = choice.parent ? this.value[choice.parent.name] : this.value;
86 if (choice.editable) {
87 val = choice.value === choice.name ? choice.initial || '' : choice.value;
88 } else if (!this.isDisabled(choice)) {
89 val = choice.enabled === true;
92 result = await choice.validate(val, this.state);
94 if (result !== true) {
99 if (result !== true) {
100 this.state.error = typeof result === 'string' ? result : 'Invalid Input';
107 if (this.focused.newChoice === true) return super.submit();
108 if (this.choices.some(ch => ch.newChoice)) {
114 for (let choice of this.choices) {
115 let val = choice.parent ? this.value[choice.parent.name] : this.value;
117 if (choice.role === 'heading') {
118 this.value[choice.name] = {};
122 if (choice.editable) {
123 val[choice.name] = choice.value === choice.name
124 ? (choice.initial || '')
127 } else if (!this.isDisabled(choice)) {
128 val[choice.name] = choice.enabled === true;
132 return this.base.submit.call(this);
136 module.exports = Editable;