14b288aa0e911e64f070362d1161f6490dc4dcc150d121ef7c5ad47ae31ee8d48c352a07a7928a42bcbf0b68f58b563aeb3c7523af84114a5da1d0f0384b50 811 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. const StringPrompt = require('../types/string');
  3. class ListPrompt extends StringPrompt {
  4. constructor(options = {}) {
  5. super(options);
  6. this.sep = this.options.separator || /, */;
  7. this.initial = options.initial || '';
  8. }
  9. split(input = this.value) {
  10. return input ? String(input).split(this.sep) : [];
  11. }
  12. format() {
  13. let style = this.state.submitted ? this.styles.primary : val => val;
  14. return this.list.map(style).join(', ');
  15. }
  16. async submit(value) {
  17. let result = this.state.error || await this.validate(this.list, this.state);
  18. if (result !== true) {
  19. this.state.error = result;
  20. return super.submit();
  21. }
  22. this.value = this.list;
  23. return super.submit();
  24. }
  25. get list() {
  26. return this.split();
  27. }
  28. }
  29. module.exports = ListPrompt;