Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Arrays of Objects #46

Merged
merged 1 commit into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/view_component/storybook/controls/object_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ def type

def value_from_param(param)
if param.is_a?(String)
JSON.parse(param).deep_symbolize_keys
parsed_json = JSON.parse(param)
if parsed_json.is_a?(Array)
parsed_json.map(&:deep_symbolize_keys)
else
parsed_json.deep_symbolize_keys
end
else
super(param)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/view_component/storybook/controls/object_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@
let(:expected_csf_value) { { shape: "square", options: { color: "red", size: 10 } } }
end
end

context "with an array of objects" do
it_behaves_like "a controls config" do
let(:value) { [{ shape: 'square', color: 'red' }, { shape: 'circle', color: 'green' }] }
let(:param_value) { '[{"shape": "square", "color": "red"},{"shape": "circle", "color": "green"}]' }

let(:expected_csf_value) { [{ shape: 'square', color: 'red' }, { shape: 'circle', color: 'green' }] }
end
end
end