Class: React::Generators::ComponentGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/react/component_generator.rb

Constant Summary collapse

REACT_PROP_TYPES =
{
  "node" => "PropTypes.node",
  "bool" => "PropTypes.bool",
  "boolean" => "PropTypes.bool",
  "string" => "PropTypes.string",
  "number" => "PropTypes.number",
  "object" => "PropTypes.object",
  "array" => "PropTypes.array",
  "shape" => "PropTypes.shape({})",
  "element" => "PropTypes.element",
  "func" => "PropTypes.func",
  "function" => "PropTypes.func",
  "any" => "PropTypes.any",

  "instanceOf" => lambda { |type|
    "PropTypes.instanceOf(#{type.to_s.camelize})"
  },

  "oneOf" => lambda { |*options|
    enums = options.map { |k| "'#{k}'" }.join(",")
    "PropTypes.oneOf([#{enums}])"
  },

  "oneOfType" => lambda { |*options|
    types = options.map { |k| lookup(k.to_s, k.to_s).to_s }.join(",")
    "PropTypes.oneOfType([#{types}])"
  }
}.freeze
TYPESCRIPT_TYPES =
{
  "node" => "React.ReactNode",
  "bool" => "boolean",
  "boolean" => "boolean",
  "string" => "string",
  "number" => "number",
  "object" => "object",
  "array" => "Array<any>",
  "shape" => "object",
  "element" => "object",
  "func" => "object",
  "function" => "object",
  "any" => "any",

  "instanceOf" => lambda { |type|
    type.to_s.camelize
  },

  "oneOf" => lambda { |*opts|
    opts.map { |k| "'#{k}'" }.join(" | ")
  },

  "oneOfType" => lambda { |*opts|
    opts.map { |k| ts_lookup(k.to_s, k.to_s).to_s }.join(" | ")
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lookup(type = "node", options = "") ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/generators/react/component_generator.rb', line 236

def self.lookup(type = "node", options = "")
  react_prop_type = REACT_PROP_TYPES[type]
  if react_prop_type.blank?
    react_prop_type = if /^[[:upper:]]/.match?(type)
                        REACT_PROP_TYPES["instanceOf"]
                      else
                        REACT_PROP_TYPES["node"]
                      end
  end

  options = options.to_s.gsub(/[{}]/, "").split(",")

  react_prop_type = react_prop_type.call(*options) if react_prop_type.respond_to? :call
  react_prop_type
end

.ts_lookup(_name, type = "node", args = "") ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/generators/react/component_generator.rb', line 211

def self.ts_lookup(_name, type = "node", args = "")
  ts_type = TYPESCRIPT_TYPES[type]
  if ts_type.blank?
    ts_type = if /^[[:upper:]]/.match?(type)
                TYPESCRIPT_TYPES["instanceOf"]
              else
                TYPESCRIPT_TYPES["node"]
              end
  end

  args = args.to_s.gsub(/[{}]/, "").split(",")

  if ts_type.respond_to? :call
    return ts_type.call(type) if args.blank?

    ts_type = ts_type.call(*args)
  end

  ts_type
end

Instance Method Details

#create_component_fileObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/generators/react/component_generator.rb', line 126

def create_component_file
  template_extension = detect_template_extension
  # Prefer Shakapacker to Sprockets:
  if shakapacker?
    new_file_name = file_name.camelize
    extension = if options[:coffee]
                  "coffee"
                elsif options[:ts]
                  "tsx"
                else
                  "js"
                end
    target_dir = webpack_configuration.source_path
                                      .join("components")
                                      .relative_path_from(::Rails.root)
                                      .to_s
  else
    new_file_name = file_name
    extension = template_extension
    target_dir = "app/assets/javascripts/components"
  end

  file_path = File.join(target_dir, class_path, "#{new_file_name}.#{extension}")
  template("component.#{template_extension}", file_path)
end