Skip to content

Commit

Permalink
feat(form): add support of static form with layout elements (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasalexandre9 authored Oct 10, 2024
1 parent 9efd400 commit ecf5f1a
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.build_schema(collection, name)
layout = form_elements[:layout]
else
fields = DEFAULT_FIELDS
layout = nil
layout = []
end

schema = {
Expand All @@ -57,9 +57,9 @@ def self.build_schema(collection, name)
}
}

return schema unless layout && !layout.empty?
return schema if layout.all? { |element| element.component == 'Input' }

# schema[:layout] = build_layout(layout)
schema[:layout] = build_layout(layout)

schema
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ module Schema
widgetEdit: nil
}
],
# layout: [
# { component: 'input', fieldId: 'label', type: 'Layout' },
# { component: 'separator', type: 'Layout' }
# ],
layout: [
{ component: 'input', fieldId: 'label', type: 'Layout' },
{ component: 'separator', type: 'Layout' }
],
hooks: { load: false, change: ['changeHook'] }
}
)
Expand Down Expand Up @@ -320,10 +320,10 @@ module Schema
widgetEdit: nil
}
],
# layout: [
# { component: 'input', fieldId: 'label', type: 'Layout' },
# { component: 'htmlBlock', type: 'Layout', content: '<p>foo</p>' }
# ],
layout: [
{ component: 'input', fieldId: 'label_id', type: 'Layout' },
{ component: 'htmlBlock', type: 'Layout', content: '<p>foo</p>' }
],
hooks: { load: false, change: ['changeHook'] }
}
)
Expand Down Expand Up @@ -388,9 +388,16 @@ module Schema
defaultValue: nil
}
],
# layout: [
# { component: 'row', type: 'Layout', fields: ['label'] }
# ],
layout: [
{
component: 'row',
type: 'Layout',
fields: [
{ component: 'input', fieldId: 'label_id', type: 'Layout' },
{ component: 'input', fieldId: 'amount_id', type: 'Layout' }
]
}
],
hooks: { load: false, change: ['changeHook'] }
}
)
Expand Down Expand Up @@ -462,15 +469,160 @@ module Schema
defaultValue: nil
}
],
# layout: [
# { component: 'page', type: 'Layout', elements: ['htmlBlock', 'row'] }
# ],
layout: [
{
component: 'page',
type: 'Layout',
nextButtonLabel: 'Next',
previousButtonLabel: 'Previous',
elements: [
{
component: 'htmlBlock',
content: '<h1>Charge the credit card of the customer</h1>',
type: 'Layout'
},
{
component: 'row',
type: 'Layout',
fields: [
{ component: 'input', fieldId: 'label', type: 'Layout' },
{ component: 'input', fieldId: 'amount', type: 'Layout' }
]
}
]
}
],
hooks: { load: false, change: ['changeHook'] }
}
)
end
end

context 'with dynamic element' do
before do
@collection = collection_build(
schema: {
actions: {
'Charge credit card' => BaseAction.new(
scope: Types::ActionScope::SINGLE,
form: [
FormLayoutElement::PageElement.new(
if_condition: proc { true },
elements: []
)
]
)
}
}
)
end

it 'generate default loading schema' do
schema = described_class.build_schema(@collection, 'Charge credit card')

expect(schema).to eq(
{
id: 'collection-0-charge-credit-card',
name: 'Charge credit card',
submitButtonLabel: nil,
description: nil,
type: 'single',
baseUrl: nil,
endpoint: '/forest/_actions/collection/0/charge-credit-card',
httpMethod: 'POST',
redirect: nil,
download: false,
fields: [
{
defaultValue: 'Form is loading',
description: '',
enums: nil,
field: 'Loading...',
hook: nil,
isReadOnly: true,
isRequired: false,
label: 'Loading...',
reference: nil,
type: 'String',
value: nil,
widgetEdit: nil
}
],
hooks: { load: true, change: ['changeHook'] }
}
)
end
end

context 'with nested dynamic element in page' do
before do
@collection = collection_build(
schema: {
actions: {
'Charge credit card' => BaseAction.new(
scope: Types::ActionScope::SINGLE,
form: [
FormLayoutElement::PageElement.new(
elements: [
{
type: 'Layout',
component: 'HtmlBlock',
content: '<h1>Charge the credit card of the customer</h1>'
},
{
type: 'Layout',
component: 'Row',
fields: [
{ id: 'label', label: 'label', type: 'String', if_condition: proc { true } },
{ id: 'amount', label: 'amount', type: 'String' }
]
}
]
)
]
)
}
}
)
end

it 'generate default loading schema' do
schema = described_class.build_schema(@collection, 'Charge credit card')

expect(schema).to eq(
{
id: 'collection-0-charge-credit-card',
name: 'Charge credit card',
submitButtonLabel: nil,
description: nil,
type: 'single',
baseUrl: nil,
endpoint: '/forest/_actions/collection/0/charge-credit-card',
httpMethod: 'POST',
redirect: nil,
download: false,
fields: [
{
defaultValue: 'Form is loading',
description: '',
enums: nil,
field: 'Loading...',
hook: nil,
isReadOnly: true,
isRequired: false,
label: 'Loading...',
reference: nil,
type: 'String',
value: nil,
widgetEdit: nil
}
],
hooks: { load: true, change: ['changeHook'] }
}
)
end
end

describe 'extract_fields_and_layout' do
before do
@collection = collection_build(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def build_elements
end

def static_form?
return form&.all?(&:static?) && form&.none? { |field| field.type == 'Layout' } if form
return form&.all?(&:static?) if form

true
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def initialize(options)
@fields = instantiate_subfields(options[:fields] || [])
end

def static?
super && fields&.all?(&:static?)
end

private

def validate_fields_presence!(options)
Expand Down Expand Up @@ -77,6 +81,10 @@ def initialize(options)
@elements = instantiate_elements(options[:elements] || [])
end

def static?
super && elements&.all?(&:static?)
end

private

def validate_elements_presence!(options)
Expand Down

0 comments on commit ecf5f1a

Please sign in to comment.