Permalink
Browse files

add RmElement example field; adjust t/repeatable/js.t

  • Loading branch information...
gshank committed Feb 9, 2013
1 parent edf55ec commit 5fa7391b58a271f93bc2f4f59f911dcaf3d1a524
@@ -1,4 +1,5 @@
package HTML::FormHandler::Field::AddElement;
+# ABSTRACT: Field to support repeatable javascript add
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field::Display';
use HTML::FormHandler::Render::Util ('process_attrs');
@@ -52,7 +53,8 @@ sub build_render_method {
$attrs->{'data-rep-id'} = $rep_field->id;
$attrs->{id} = $self->id;
my $attr_str = process_attrs($attrs);
- my $output = qq{<div$attr_str>$value</div>};
+ my $wrapper_tag = $self->get_tag('wrapper_tag') || 'div';
+ my $output = qq{<$wrapper_tag$attr_str>$value</$wrapper_tag>};
$output = $self->wrap_field($self->result, $output);
return $output;
};
@@ -0,0 +1,56 @@
+package HTML::FormHandler::Field::RmElement;
+# ABSTRACT: field to support repeatable javascript remove
+use HTML::FormHandler::Moose;
+extends 'HTML::FormHandler::Field::Display';
+use HTML::FormHandler::Render::Util ('process_attrs');
+
+=head1 NAME
+
+HTML::FormHandler::Field::RmElement
+
+=head1 SYNOPSIS
+
+EXAMPLE field for rendering an RmElement field for
+doing javascript removals of repeatable elements.
+
+You probably want to make your own.
+
+The main requirements are that the button have 1) the
+'rm_element' class, 2) a 'data-rep-elem-id' attribute that
+contains the id of the repeatable instance that you want
+to remove (C<< $self->parent->id >>).
+
+This field should be a subfield of the Repeatable, probably
+either first or last.
+
+=head1 ATTRIBUTES
+
+ has_field 'rm_element' => ( type => 'RmElement',
+ value => 'Remove',
+ );
+
+=cut
+
+has '+do_wrapper' => ( default => 1 );
+has '+value' => ( default => 'Remove' );
+
+sub build_render_method {
+ return sub {
+ my ( $self, $result ) = @_;
+ $result ||= $self->result;
+
+ my $value = $self->html || $self->html_filter($self->_localize($self->value));
+ my $attrs = $self->element_attributes($result);
+ push @{$attrs->{class}}, ( 'rm_element', 'btn' );
+ $attrs->{'data-rep-elem-id'} = $self->parent->id;
+ $attrs->{id} = $self->id;
+ my $attr_str = process_attrs($attrs);
+ my $wrapper_tag = $self->get_tag('wrapper_tag') || 'div';
+ my $output = qq{<$wrapper_tag$attr_str>$value</$wrapper_tag>};
+ $output = $self->wrap_field($self->result, $output);
+ return $output;
+ };
+}
+
+__PACKAGE__->meta->make_immutable;
+1;
@@ -1,4 +1,5 @@
package HTML::FormHandler::Render::RepeatableJs;
+# ABSTRACT: role providing method to construct repeatable javascript
use Moose::Role;
use JSON ('encode_json');
@@ -23,9 +24,9 @@ Or just write out the rep_ data to javascript variables, and write the
function in javascript.
This function uses a plain javascript confirmation dialog.
You almost certainly want to do something else.
-This javascript depends on the Bootstrap 'controls' div class
-in order to position the new elements. You will have to modify
-it to work if you don't use Bootstrap rendering.
+This javascript depends on the Repeatable field having a 'controls' div class
+in order to position the new elements. Use the Bootstrap wrapper or the
+'control_div' tag on the Simple wrapper.
A role to be used in a Form Class:
@@ -61,26 +62,13 @@ repeatable element. That requires that the wrapper have the repeatable
instance ID (now rendered by default). If you don't have wrappers around
your repeatable elements, this won't work.
-See HTML::FormHandler::Field::AddElement for an example of rendering
+See L<HTML::FormHandler::Field::AddElement> for an example of rendering
an HTML element that can be used to provide the AddElement button.
See that field for the requirements for the add HTML.
-There is no example of a remove button because it's very basic
-and there are too many different places to put it and ways to do it.
-The main requirements are that the button have a 'data-rep-elem-id'
-attribute that contains the id of the repeatable element to remove,
-and a class of 'rm_element'. It should be a child field of the
-repeatable.
-
-This one works:
-
- has_field 'elements.rm_element' => (
- type => 'Display', render_method => \&render_rm_element );
- sub render_rm_element {
- my $self = shift;
- my $id = $self->parent->id;
- return qq{<span class="btn rm_element" data-rep-elem-id="$id">Remove</span>};
- }
+See L<HTML::FormHandler::Field::RmElement> for an example of rendering
+an HTML element that can be used to provide a 'remove' button.
+See that field for the requirements for the remove HTML.
=cut
View
@@ -24,12 +24,9 @@ use HTML::FormHandler::Test;
# The 'remove' doesn't have to be a display field. It could be other html associated
# with the repeatable element wrapper or label.
has_field 'foo.remove' => (
- type => 'Display', render_method => \&render_remove );
- sub render_remove {
- my $self = shift; # self is field
- my $id = $self->parent->id;
- return qq{<span class="btn rm_element" data-rep-elem-id="$id">Remove</span>};
- }
+ type => 'RmElement',
+ value => 'Remove',
+ );
has_field 'foo.one';
has_field 'foo.two';
@@ -65,7 +62,9 @@ $expected = '
<fieldset id="foo">
<div class="controls">
<div class="hfh-repinst" id="foo.0">
- <span class="btn rm_element" data-rep-elem-id="foo.0">Remove</span>
+ <div>
+ <div class="rm_element btn" data-rep-elem-id="foo.0" id="foo.0.remove">Remove</div>
+ </div>
<div>
<label for="foo.0.one">One</label>
<input id="foo.0.one" name="foo.0.one" type="text" value="" />

0 comments on commit 5fa7391

Please sign in to comment.