You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
811 B

4 years ago
  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;