ConversationForms
A Rails port of conversational-form inspired by SPACE10
Installation
Add this line to your application's Gemfile:
gem 'conversation_forms'
And then execute:
$ bundle
Or install it yourself as:
$ gem install conversation_forms
require the js:
//= require conversation_forms
and require the css:
*= require conversation_forms
Usage
on any pages that you want to use a conversation form, simply set the form's ID to convo-form
conversation forms will automatically detect your form's method and action
<form id="my-form-element" cf-form-element ...
with form_for add "cf-form-elemt" => true:
<%= form_for @user, html: { "cf-form-element" => true } do |f| %>
``
That's It!
# DOM Element attributes
### cf-questions
* to map questions directly to a tag.
* seperate by | to allow for more questions, app will shuffle.
```html
<input type="text" cf-questions="What is your name?|Please tell me your name." ..
{One way value-binding} with cf-questions:
For cui-questions, add {previous-answer} to insert the value from the previous user-answer.
<input type="text" cf-questions="Hello {previous-answer}" ..
previous input could be firstname.
<input type="text" cf-questions="So you want to travel to {previous-answer}" ..
previous input could be a select:option list with countries.
cf-label
- set a label to the field, [type="radio"|"checkbox"]
<input type="radio" cf-label="Subscribe to newsletter" ..
cf-error
- to map error messages directly to a tag.
- seperate by | to allow for more error, app will shuffle.
<input type="text" cf-error="Text is wrong wrong|Input is not right" ..
Validations
- Checks user input against the supplied regex
<input type="text" pattern="^[0-9a-zA-Z-']*$" ..
cf-validation-contains
- Checks if the input value is one of a pipe-separated list of values
- e.g. the following will check if the input is equal to "a", "b", "c", or "d"
<input type="text" cf-validation-contains="a|b|c|d" ..
cf-validation-email
- Basic email regex check to ensure the value contains only 1 "@" symbol and at lease 1 "." after the "@"
<input type="text" cf-validation-email ..
pattern/cf-validation-matches
- This will check user input against the supplied regex
- Using "pattern" is recommended over cf-validation-matches when possible as it is a native HTML5 attribute
<input type="text" pattern="^[0-9a-zA-Z-']*$" ..
<input type="text" cf-validation-matches="^[0-9a-zA-Z-']*$" ..
max/cf-validation-max
- Ensure User input is less than or equal to a maximum
- Using "max" is recommended over cf-validation-max when possible as it is a native HTML5 attribute
<input type="number" max=9 ..
<input type="text" cf-validation-max=9 ..
min/cf-validation-min
- Ensure User input is greater than or equal to a minimum
- Using "min" is recommended over cf-validation-min when possible as it is a native HTML5 attribute
<input type="number" min=1 ..
<input type="text" cf-validation-min=1 ..
required
- to require a field set the required attribute as normal
<input type="text" required ..
<input type="text" required="true" ..
<input type="text" required="required" ..
cf-validation-custom
- Check input against a custom Javascript function before submitting
- OBS. eval is used.
- two parameters are passed to your custom method
- value: String, the value of the input field
- tag: ITag, the actual DOM tag
<input type="text" cf-validation-custom="window.validateFunction" ..
Advanced Initialization
cf-context
If you want to have the ConversationalForm appended to a certain element (when auto-instantiating) then add attribute cf-context
to an element, otherwise the ConversationalForm will be appended to the form's parent element.
<div cf-context ...>
cf-prevent-autofocus
If you don't want to have the UserInput to auto focus.
<form id="my-form-element" cf-form-element cf-prevent-autofocus>
Customization
For more control over the output exclude the attribute cf-form-element
from the form element and instantiate either with vanilla JS or jQuery:
Self-instantiate with vanilla JS
new cf.ConversationalForm({
formEl: <HTMLFormElement>,
// dictionaryData?: {}, // empty will throw error, see Dictionaty.ts for values
// dictionaryAI?: {}, // empty will throw error, see Dictionaty.ts for values
// context?: // context of where to append the ConversationalForm (see also cf-context attribute)
// tags?: tags, // pass in custom tags (when prevent the auto-instantiation of ConversationalForm)
// submitCallback?: () => void | HTMLButtonElement // custom submit callback if button[type=submit] || form.submit() is not wanted..
// userImage: "..." //base64 || image url
});
Instantiate with jQuery
$("form").conversationalForm();
- formEl: HTMLFormElement | string
-
context?: HTMLElement | string
- Set the context of where the ConversationalForm will be appended to
- If not set then ConversationalForm will get appended to document.body
-
tags?: Array
- [cf.Tag.createTag(element), ...]
-
dictionaryData?: object
- Possibility to overwrite the default dictionary, empty will throw error, see Dictionaty.ts for values
-
dictionaryAI?: object
- Possibility to overwrite the default dictionary, empty will throw error, see Dictionaty.ts for values
-
submitCallback?: () => void | HTMLButtonElement
- An alternative way to submit the form. Can be a Function or an HTMLButtonElement (click will be called). If not defined the component will search in the formEl after a button[type=”submit”] and call click() if not button is found final fallback will be to call submit() on formEl.
-
userImage?: string
- Set a different userImage. "..." //base64 || image url
Map your own tags
The Conversational Form automatically detects the accepted tags in the passed in form element. If this is not desired then you are able to define your own tags, and pass them into the constructor.:
var fields = [].slice.call(formEl.querySelectorAll("input, select, button"), 0);
for (var i = 0; i < fields.length; i++) {
var element = fields[i];
tags.push(cf.Tag.createTag(element));
}
Tags can then be set in the instantiation object, see ConversationalFormOptions
Public API
When instantiating ConversationalForm a reference to the instance will be available in window scope.
window.ConversationalForm
using this reference you are able to remove the ConversationalForm by calling:
window.ConversationalForm.remove();
Overwrite styles
You can overwrite the UI with your own styles. Please see the source styles files for more info.
Contribute to ConversationalForm
We welcome contributions in the form of bug reports, pull requests, or thoughtful discussions in the GitHub issue tracker.
ConversationalForm is a concept originally by SPACE10. Brought to life by Felix Nielsen, RWATGG. Designed by Charlie Isslander.
Ported By Sampson Crowley
License
The gem is available as open source under the terms of the MIT License.