app/pb_kits/playbook/pb_typeahead/typeahead.rb



# frozen_string_literal: true

module Playbook
  module PbTypeahead
    class Typeahead < Playbook::KitBase
      prop :async, type: Playbook::Props::Boolean, default: false
      prop :default_options, type: Playbook::Props::HashArray, default: []
      prop :error, type: Playbook::Props::String,
                   default: ""
      prop :get_option_label
      prop :get_option_value
      prop :id
      prop :inline, type: Playbook::Props::Boolean,
                    default: false
      prop :label
      prop :load_options
      prop :multi_kit, type: Playbook::Props::String,
                       default: ""
      prop :name
      prop :options, type: Playbook::Props::HashArray,
                     default: []
      prop :input_options, type: Playbook::Props::HashProp,
                           default: {}

      prop :is_multi, type: Playbook::Props::Boolean,
                      default: true

      prop :pills, type: Playbook::Props::Boolean,
                   default: false

      prop :placeholder
      prop :plus_icon, type: Playbook::Props::Boolean,
                       default: false
      prop :search_term_minimum_length, default: 3
      prop :search_debounce_timeout, default: 250
      prop :value
      prop :margin_bottom, type: Playbook::Props::Enum,
                           values: %w[none xxs xs sm md lg xl],
                           default: "sm"
      prop :pill_color, type: Playbook::Props::Enum,
                        values: %w[primary neutral success warning error info data_1 data_2 data_3 data_4 data_5 data_6 data_7 data_8 windows siding roofing doors gutters solar insulation accessories],
                        default: "primary"

      def classname
        default_margin_bottom = margin_bottom.present? ? "" : " mb_sm"
        generate_classname("pb_typeahead_kit") + default_margin_bottom
      end

      def inline_class
        inline ? " inline" : ""
      end

      def data
        Hash(values[:data]).merge(
          pb_typeahead_kit: true,
          pb_typeahead_kit_search_term_minimum_length: search_term_minimum_length,
          pb_typeahead_kit_search_debounce_timeout: search_debounce_timeout
        )
      end

      def is_react?
        pills || !is_multi
      end

      def typeahead_react_options
        base_options = {
          className: classname,
          pillColor: pill_color,
          dark: dark,
          defaultValue: default_options,
          error: error,
          id: id,
          inline: inline,
          isMulti: is_multi,
          label: label,
          marginBottom: margin_bottom,
          multiKit: multi_kit,
          name: name,
          options: options,
          placeholder: placeholder,
          plusIcon: plus_icon,
        }

        base_options[:getOptionLabel] = get_option_label if get_option_label.present?
        base_options[:getOptionValue] = get_option_value if get_option_value.present?
        if async
          base_options[:async] = true
          base_options[:loadOptions] = load_options
        end
        base_options
      end
    end
  end
end