class React::Generators::ComponentGenerator
Constants
- REACT_PROP_TYPES
- TYPESCRIPT_TYPES
Private Class Methods
lookup(type = 'node', options = '')
click to toggle source
# File lib/generators/react/component_generator.rb, line 243 def self.lookup(type = 'node', options = '') react_prop_type = REACT_PROP_TYPES[type] if react_prop_type.blank? if type =~ /^[[:upper:]]/ react_prop_type = REACT_PROP_TYPES['instanceOf'] else react_prop_type = 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 = '')
click to toggle source
# File lib/generators/react/component_generator.rb, line 216 def self.ts_lookup(name, type = 'node', args = '') ts_type = TYPESCRIPT_TYPES[type] if ts_type.blank? if type =~ /^[[:upper:]]/ ts_type = TYPESCRIPT_TYPES['instanceOf'] else ts_type = TYPESCRIPT_TYPES['node'] end end args = args.to_s.gsub(/[{}]/, '').split(',') if ts_type.respond_to? :call if args.blank? return ts_type.call(type) end ts_type = ts_type.call(*args) end ts_type end
Public Instance Methods
create_component_file()
click to toggle source
# File lib/generators/react/component_generator.rb, line 124 def create_component_file template_extension = if options[:coffee] 'js.jsx.coffee' elsif options[:ts] 'js.jsx.tsx' elsif options[:es6] || webpacker? 'es6.jsx' else 'js.jsx' end # Prefer webpacker to sprockets: if webpacker? 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
Private Instance Methods
component_name()
click to toggle source
# File lib/generators/react/component_generator.rb, line 165 def component_name file_name.camelize end
file_header()
click to toggle source
# File lib/generators/react/component_generator.rb, line 169 def file_header if webpacker? return %|import * as React from "react"\n| if options[:ts] %|import React from "react"\nimport PropTypes from "prop-types"\n| else '' end end
lookup(type = 'node', options = '')
click to toggle source
# File lib/generators/react/component_generator.rb, line 259 def lookup(type = 'node', options = '') self.class.lookup(type, options) end
parse_attributes!()
click to toggle source
# File lib/generators/react/component_generator.rb, line 190 def parse_attributes! self.attributes = (attributes || []).map do |attr| name = '' type = '' args = '' args_regex = /(?<args>{.*})/ name, type = attr.split(':') if matchdata = args_regex.match(type) args = matchdata[:args] type = type.gsub(args_regex, '') end if options[:ts] { :name => name, :type => ts_lookup(name, type, args), :union => union?(args) } else { :name => name, :type => lookup(type, args) } end end end
ts_lookup(name, type = 'node', args = '')
click to toggle source
# File lib/generators/react/component_generator.rb, line 239 def ts_lookup(name, type = 'node', args = '') self.class.ts_lookup(name, type, args) end
union?(args = '')
click to toggle source
# File lib/generators/react/component_generator.rb, line 212 def union?(args = '') return args.to_s.gsub(/[{}]/, '').split(',').count > 1 end
webpack_configuration()
click to toggle source
# File lib/generators/react/component_generator.rb, line 161 def webpack_configuration Webpacker.respond_to?(:config) ? Webpacker.config : Webpacker::Configuration end
webpacker?()
click to toggle source
# File lib/generators/react/component_generator.rb, line 186 def webpacker? defined?(Webpacker) end