Skip to content

Commit b6dc4a6

Browse files
committed
improvements to enquiry form
1 parent 7d967e0 commit b6dc4a6

File tree

5 files changed

+71
-30
lines changed

5 files changed

+71
-30
lines changed

src/components/enquiry.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
<div v-for="field in visible_fields">
1414
<tcs-input :field="field"></tcs-input>
1515
</div>
16-
<div v-for="field in attribute_fields">
17-
<tcs-input :field="field" prefix="attributes"></tcs-input>
18-
</div>
1916

2017
<div :id="grecaptcha_container_id" class="grecaptcha"></div>
2118
<div v-if="grecaptcha_missing" class="error-msg">

src/components/input.vue

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,80 @@
11
<template>
2-
<div class="tcs-field">
3-
<textarea v-if="is_textarea"
2+
<div class="tcs-field" :id="'field-' + name">
3+
<textarea v-if="field.type === 'text' && field.max_length > 500"
44
:name="name"
5-
:placeholder="field.label"
5+
:placeholder="label"
66
:required="field.required"
77
:maxlength="field.max_length || 255"
88
:value="value"
99
@input="changed"
1010
rows="5">
1111
</textarea>
12+
13+
<label v-else-if="field.type === 'checkbox'">
14+
<input type="checkbox" :name="name" :required="field.required" :checked="value" @change="changed">
15+
{{ label }}
16+
</label>
17+
18+
<label v-else-if="field.type === 'select'">
19+
{{ label }}
20+
<select :name="name" :required="field.required" @input="changed">
21+
<option v-for="choice in field.choices" :value="choice.value" :selected="choice.value === value">
22+
{{ choice.display_name }}
23+
</option>
24+
</select>
25+
</label>
26+
1227
<input v-else
1328
:type="field.type"
1429
:name="name"
15-
:placeholder="field.label"
30+
:placeholder="label"
1631
:required="field.required"
1732
:maxlength="field.max_length || 255"
1833
:value="value"
1934
@input="changed">
35+
36+
<div :class="'help-text' + (this.field.prefix ? '' : ' muted')">
37+
{{ field.help_text }}
38+
</div>
2039
</div>
2140
</template>
2241

2342
<script>
2443
export default {
2544
props: {
2645
field: Object,
27-
prefix: {
28-
type: String,
29-
default: ''
30-
},
3146
},
3247
computed: {
3348
is_textarea: function () {
34-
// TODO return this.field.type === 'text' && this.field.max_length > 500
35-
return this.field.type === 'text' && this.prefix === 'attributes'
49+
return this.field.type === 'text' && this.field.max_length > 500
50+
},
51+
label: function () {
52+
return this.field.label + (this.field.required ? this.$root.get_text('required_message') : '')
3653
},
3754
name: function () {
38-
return this.prefix + '.' + this.field.field
55+
if (this.field.prefix) {
56+
return this.field.prefix + '-' + this.field.field
57+
} else {
58+
return this.field.field
59+
}
3960
},
4061
value: function () {
41-
if (this.prefix === '') {
42-
return this.$root.enquiry_data[this.field.field] || ''
62+
if (this.field.prefix) {
63+
return (this.$root.enquiry_data[this.field.prefix] || {})[this.field.field] || ''
4364
} else {
44-
return (this.$root.enquiry_data[this.prefix] || {})[this.field.field] || ''
65+
return this.$root.enquiry_data[this.field.field] || ''
4566
}
4667
}
4768
},
4869
methods: {
4970
changed: function (event) {
50-
if (this.prefix === '') {
51-
this.$set(this.$root.enquiry_data, this.field.field, event.target.value)
71+
if (this.field.prefix) {
72+
let obj = this.$root.enquiry_data[this.field.prefix] || {}
73+
obj[this.field.field] = this.field.type === 'checkbox' ? event.target.checked : event.target.value
74+
this.$set(this.$root.enquiry_data, this.field.prefix, obj)
75+
this.$root.enquiry_data = Object.assign({}, this.$root.enquiry_data)
5276
} else {
53-
var obj = this.$root.enquiry_data[this.prefix] || {}
54-
obj[this.field.field] = event.target.value
55-
this.$set(this.$root.enquiry_data, this.prefix, obj)
77+
this.$set(this.$root.enquiry_data, this.field.field, event.target.value)
5678
}
5779
}
5880
},
@@ -64,23 +86,46 @@ export default {
6486
6587
$border-colour: #66afe9;
6688
.tcs-field {
89+
&.required {
90+
.help-text {
91+
content: "Read this: ";
92+
}
93+
}
94+
6795
padding: 8px 0;
6896
width: 100%;
69-
input, textarea {
97+
input, textarea, select {
7098
font-size: 16px;
7199
box-sizing: border-box;
72100
width: 100%;
73-
padding: 10px 12px;
101+
padding: 8px 12px 10px;
74102
margin: 0;
75103
height: inherit;
76104
border-radius: 5px;
77105
border: 1px solid #aaa;
78106
font-family: inherit;
79107
outline: none;
108+
background-color: white;
80109
&:focus {
81110
border-color: $border-colour;
82111
box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px lighten($border-colour, 20%);
83112
}
113+
&:required {
114+
border: 1px solid #888;
115+
}
116+
}
117+
label {
118+
display: block;
119+
}
120+
input[type="checkbox"] {
121+
width: auto;
122+
}
123+
.help-text {
124+
font-size: 14px;
125+
color: #888;
126+
&.muted {
127+
display: none;
128+
}
84129
}
85130
}
86131
</style>

src/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const STRINGS = {
8484
enquiry_modal_submitted_thanks: 'Enquiry submitted, thank you.\n\nYou can now close this window.',
8585
enquiry_button: 'Get in touch',
8686
grecaptcha_missing: 'This captcha is required',
87+
required_message: ' (Required)',
8788
}
8889

8990
const MODES = ['grid', 'enquiry', 'enquiry-modal']

test/unit/specs/_shared.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ class TestConsole {
2626

2727
const enquiry_options = {
2828
visible: [
29-
{field: 'first_field', type: 'text', label: 'Foobar', max_length: 255}
29+
{field: 'first_field', prefix: null, type: 'text', label: 'Foobar', max_length: 255},
30+
{field: 'custom_field', prefix: 'attributes', type: 'text', label: 'Custom Field', max_length: 2047},
3031
],
31-
attributes: [
32-
{field: 'custom_field', type: 'text', label: 'Custom Field', max_length: 2047}
33-
]
3432
}
3533

3634
const vm_data = () => ({

test/unit/specs/enquiry.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ describe('con-modal.vue, enquiry.vue', () => {
1111
await tick()
1212
await sleep(50) // wait for transition
1313
expect(vm.$el.querySelector('input').attributes['placeholder'].value).to.equal('Foobar')
14-
expect(vm.$el.querySelector('input').attributes['name'].value).to.equal('.first_field')
14+
expect(vm.$el.querySelector('input').attributes['name'].value).to.equal('first_field')
1515
expect(vm.$el.querySelector('textarea').attributes['placeholder'].value).to.equal('Custom Field')
16-
expect(vm.$el.querySelector('textarea').attributes['name'].value).to.equal('attributes.custom_field')
16+
expect(vm.$el.querySelector('textarea').attributes['name'].value).to.equal('attributes-custom_field')
1717
})
1818
})
1919

0 commit comments

Comments
 (0)