|
| 1 | +module React |
| 2 | + module Generators |
| 3 | + class ComponentGenerator < ::Rails::Generators::NamedBase |
| 4 | + source_root File.expand_path '../../templates', __FILE__ |
| 5 | + desc <<-DESC.strip_heredoc |
| 6 | + Description: |
| 7 | + Scaffold a react component into app/assets/javascripts/components. |
| 8 | + The generated component will include a basic render function and a PropTypes |
| 9 | + hash to help with development. |
| 10 | +
|
| 11 | + Available field types: |
| 12 | +
|
| 13 | + Basic prop types do not take any additional arguments. If you do not specify |
| 14 | + a prop type, the generic node will be used. The basic types available are: |
| 15 | +
|
| 16 | + any |
| 17 | + array |
| 18 | + bool |
| 19 | + element |
| 20 | + func |
| 21 | + number |
| 22 | + object |
| 23 | + node |
| 24 | + shape |
| 25 | + string |
| 26 | +
|
| 27 | + Special PropTypes take additional arguments in {}, and must be enclosed in |
| 28 | + single quotes to keep bash from expanding the arguments in {}. |
| 29 | +
|
| 30 | + instanceOf |
| 31 | + takes an optional class name in the form of {className} |
| 32 | +
|
| 33 | + oneOf |
| 34 | + behaves like an enum, and takes an optional list of strings that will |
| 35 | + be allowed in the form of 'name:oneOf{one,two,three}'. |
| 36 | +
|
| 37 | + oneOfType. |
| 38 | + oneOfType takes an optional list of react and custom types in the form of |
| 39 | + 'model:oneOfType{string,number,OtherType}' |
| 40 | +
|
| 41 | + Examples: |
| 42 | + rails g react:component person name |
| 43 | + rails g react:component restaurant name:string rating:number owner:instanceOf{Person} |
| 44 | + rails g react:component food 'kind:oneOf{meat,cheese,vegetable}' |
| 45 | + rails g react:component events 'location:oneOfType{string,Restaurant}' |
| 46 | + DESC |
| 47 | + |
| 48 | + argument :attributes, |
| 49 | + :type => :array, |
| 50 | + :default => [], |
| 51 | + :banner => "field[:type] field[:type] ..." |
| 52 | + |
| 53 | + REACT_PROP_TYPES = { |
| 54 | + "node" => 'React.PropTypes.node', |
| 55 | + "bool" => 'React.PropTypes.bool', |
| 56 | + "boolean" => 'React.PropTypes.bool', |
| 57 | + "string" => 'React.PropTypes.string', |
| 58 | + "number" => 'React.PropTypes.number', |
| 59 | + "object" => 'React.PropTypes.object', |
| 60 | + "array" => 'React.PropTypes.array', |
| 61 | + "shape" => 'React.PropTypes.shape({})', |
| 62 | + "element" => 'React.PropTypes.element', |
| 63 | + "func" => 'React.PropTypes.func', |
| 64 | + "function" => 'React.PropTypes.func', |
| 65 | + "any" => 'React.PropTypes.any', |
| 66 | + |
| 67 | + "instanceOf" => ->(type) { |
| 68 | + 'React.PropTypes.instanceOf(%s)' % type.to_s.camelize |
| 69 | + }, |
| 70 | + |
| 71 | + "oneOf" => ->(*options) { |
| 72 | + enums = options.map{|k| "'#{k.to_s}'"}.join(',') |
| 73 | + 'React.PropTypes.oneOf([%s])' % enums |
| 74 | + }, |
| 75 | + |
| 76 | + "oneOfType" => ->(*options) { |
| 77 | + types = options.map{|k| "#{lookup(k.to_s, k.to_s)}" }.join(',') |
| 78 | + 'React.PropTypes.oneOfType([%s])' % types |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + def create_component_file |
| 83 | + extension = "js.jsx" |
| 84 | + file_path = File.join('app/assets/javascripts/components', "#{file_name}.#{extension}") |
| 85 | + template("component.#{extension}", file_path) |
| 86 | + end |
| 87 | + |
| 88 | + private |
| 89 | + |
| 90 | + def parse_attributes! |
| 91 | + self.attributes = (attributes || []).map do |attr| |
| 92 | + name, type, options = "", "", "" |
| 93 | + options_regex = /(?<options>{.*})/ |
| 94 | + |
| 95 | + name, type = attr.split(':') |
| 96 | + |
| 97 | + if matchdata = options_regex.match(type) |
| 98 | + options = matchdata[:options] |
| 99 | + type = type.gsub(options_regex, '') |
| 100 | + end |
| 101 | + |
| 102 | + { :name => name, :type => lookup(type, options) } |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + def self.lookup(type = "node", options = "") |
| 107 | + react_prop_type = REACT_PROP_TYPES[type] |
| 108 | + if react_prop_type.blank? |
| 109 | + if type =~ /^[[:upper:]]/ |
| 110 | + react_prop_type = REACT_PROP_TYPES['instanceOf'] |
| 111 | + else |
| 112 | + react_prop_type = REACT_PROP_TYPES['node'] |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + options = options.to_s.gsub(/[{}]/, '').split(',') |
| 117 | + |
| 118 | + react_prop_type = react_prop_type.call(*options) if react_prop_type.respond_to? :call |
| 119 | + react_prop_type |
| 120 | + end |
| 121 | + |
| 122 | + def lookup(type = "node", options = "") |
| 123 | + self.class.lookup(type, options) |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | +end |
0 commit comments