Class: Playbook::Forms::Builder::FormFieldBuilder
- Inherits:
-
Module
- Object
- Module
- Playbook::Forms::Builder::FormFieldBuilder
- Defined in:
- lib/playbook/forms/builder/form_field_builder.rb
Constant Summary collapse
- MASK_PATTERNS =
{ "currency" => '^\$\d{1,3}(?:,\d{3})*(?:\.\d{2})?$', "zip_code" => '\d{5}', "postal_code" => '\d{5}-\d{4}', "ssn" => '\d{3}-\d{2}-\d{4}', "credit_card" => '\d{4} \d{4} \d{4} \d{4}', "cvv" => '\d{3,4}', }.freeze
Instance Method Summary collapse
-
#initialize(method_name, kit_name:) ⇒ FormFieldBuilder
constructor
A new instance of FormFieldBuilder.
Constructor Details
#initialize(method_name, kit_name:) ⇒ FormFieldBuilder
Returns a new instance of FormFieldBuilder.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/playbook/forms/builder/form_field_builder.rb', line 16 def initialize(method_name, kit_name:) define_method method_name do |name, props: {}, **, &block| props = props.dup = Hash() [:skip_default_ids] = false unless .key?(:skip_default_ids) [:required] = true if props[:required] [:placeholder] = props[:placeholder] || "" [:type] = props[:type] if props.key?(:type) [:value] = props[:value] if props.key?(:value) [:disabled] = true if props.key?(:disabled) && props[:disabled] if props.key?(:disabled) cursor_style = props[:disabled] ? "not-allowed" : "pointer" existing_style = [:style] || "" [:style] = existing_style.empty? ? "cursor: #{cursor_style}" : "#{existing_style}; cursor: #{cursor_style}" end if props.key?(:autocomplete) [:autocomplete] = props[:autocomplete] == true ? nil : (props[:autocomplete].presence || "off") end if props.key?(:mask) && props[:mask].present? [:mask] = props[:mask] [:data] = ([:data] || {}).merge(pb_input_mask: true) [:pattern] = MASK_PATTERNS[props[:mask]] end if props.key?(:validation) validation = props[:validation] [:pattern] = validation[:pattern] if validation[:pattern].present? [:data] = ([:data] || {}).merge(message: validation[:message]) if validation[:message].present? end input = super(name, **, &block) input_id = input[/\bid="([^"]+)"/, 1] || "#{@object_name}_#{name}" if props[:label] == true props[:label] = @template.label(@object_name, name) elsif props[:label].is_a?(String) props[:label] = @template.label_tag(input_id, props[:label]) end @template.pb_rails(kit_name, props: props) do input end end end |