Skip to content

Commit

Permalink
Merge pull request #11 from fractaledmind/name-form-fields
Browse files Browse the repository at this point in the history
Allow users to specify a new for form fields
  • Loading branch information
fractaledmind authored Oct 8, 2024
2 parents df5f122 + 409ed60 commit 053d23e
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/prompts/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,51 @@ def self.submit(&block)

def initialize
@content = Prompts::Content.new
@prompts = []
@results = []
@index = 0
@prompts = {}
@results = {}
end

def content(&block)
yield @content
@content
end

def text(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
def text(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, name: nil, &block)
prompt = TextPrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
yield(prompt) if block
prepend_form_content_to_prompt(prompt)
@prompts << prompt
key = name || (@index += 1)
@prompts[key] = prompt
end

def select(label: nil, options: nil, prompt: "> ", hint: nil, default: nil, validate: nil, &block)
def select(label: nil, options: nil, prompt: "> ", hint: nil, default: nil, validate: nil, name: nil, &block)
prompt = SelectPrompt.new(label: label, options: options, prompt: prompt, hint: hint, default: default, validate: validate)
yield(prompt) if block
prepend_form_content_to_prompt(prompt)
@prompts << prompt
key = name || (@index += 1)
@prompts[key] = prompt
end

def pause(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
def pause(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, name: nil, &block)
prompt = PausePrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
yield(prompt) if block
prepend_form_content_to_prompt(prompt)
@prompts << prompt
key = name || (@index += 1)
@prompts[key] = prompt
end

def confirm(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
def confirm(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, name: nil, &block)
prompt = ConfirmPrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
yield(prompt) if block
prepend_form_content_to_prompt(prompt)
@prompts << prompt
key = name || (@index += 1)
@prompts[key] = prompt
end

def submit
@prompts.each do |prompt|
@results << prompt.ask
@prompts.each do |key, prompt|
@results[key] = prompt.ask
end
@results
end
Expand Down

0 comments on commit 053d23e

Please sign in to comment.