Skip to content

Commit

Permalink
[SDPL-88] Field select letter (#565)
Browse files Browse the repository at this point in the history
* [SDPL-88] Added Option Button field to RplForm.

* [SDPL-88] Update snapshot.

* [SDPL-88] Add test for Option Button.
  • Loading branch information
alan-cole authored and tim-yao committed Nov 1, 2019
1 parent 039b446 commit 7bb06ba
Show file tree
Hide file tree
Showing 5 changed files with 851 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { mount } from '@vue/test-utils'
import RplOptionButton from './../fields/fieldRploptionbutton'

describe('RplOptionButton', () => {
it('has A-Z for default options', () => {
const wrapper = mount(RplOptionButton, {
propsData: {
schema: {
type: 'rploptionbutton',
label: 'Option button default',
hint: 'Option button test',
model: 'optionsButton'
}
}
})
expect(wrapper.isVueInstance()).toBeTruthy()
const alphabet = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ]
wrapper.findAll('.rpl-option-button__label').wrappers.forEach((item, idx) => {
expect(item.text()).toBe(alphabet[idx])
})
})

it('sets the value "D" when 4th option clicked', () => {
const wrapper = mount(RplOptionButton, {
propsData: {
model: {
optionsButton: null
},
schema: {
type: 'rploptionbutton',
label: 'Option button default',
hint: 'Option button test',
model: 'optionsButton'
}
}
})
wrapper.find('.rpl-option-button__label:nth-child(4)').trigger('click')
expect(wrapper.props().model.optionsButton).toBe('D')
})

it('has custom text for options when defined', () => {
const customOptions = [
'Option one',
'Option two',
'Option three'
]
const wrapper = mount(RplOptionButton, {
propsData: {
model: {
optionsButton: null
},
schema: {
type: 'rploptionbutton',
label: 'Option button default',
hint: 'Option button test',
model: 'optionsButton',
optionValues: customOptions
}
}
})
expect(wrapper.isVueInstance()).toBeTruthy()
wrapper.findAll('.rpl-option-button__label').wrappers.forEach((item, idx) => {
expect(item.text()).toBe(customOptions[idx])
})
})

it('sets the value "Option three" when 3rd option clicked', () => {
const wrapper = mount(RplOptionButton, {
propsData: {
model: {
optionsButton: null
},
schema: {
type: 'rploptionbutton',
label: 'Option button default',
hint: 'Option button test',
model: 'optionsButton',
optionValues: [
'Option one',
'Option two',
'Option three'
]
}
}
})
wrapper.find('.rpl-option-button__label:nth-child(3)').trigger('click')
expect(wrapper.props().model.optionsButton).toBe('Option three')
})
})
98 changes: 98 additions & 0 deletions packages/components/Molecules/Form/fields/fieldRploptionbutton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<template>
<div class="rpl-option-button" :disabled="disabled" v-attributes="'wrapper'">
<label v-for="(item, index) in optionValues" :key="index" class="rpl-option-button__label" :class="{ 'rpl-option-button__label--checked': isItemChecked(item) }">
<input :id="getFieldID(schema, true)" type="radio" :name="id" class="rpl-option-button__radio" @click="onSelection(item)" :value="item" :class="schema.fieldClasses" :required="schema.required" />
<span class="rpl-option-button__text">{{ item }}</span>
</label>
</div>
</template>

<script>
import { abstractField } from 'vue-form-generator'
export default {
name: 'RplOptionButton',
mixins: [abstractField],
data () {
return {
optionValues: this.schema.optionValues || [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ]
}
},
computed: {
id () {
return this.schema.model
}
},
methods: {
onSelection (item) {
this.value = item
},
isItemChecked (item) {
return (item === this.value)
}
}
}
</script>

<style lang="scss">
@import "~@dpc-sdp/ripple-global/scss/settings";
@import "~@dpc-sdp/ripple-global/scss/tools";
$rpl-option-button-label-background-color: rpl-color('white') !default;
$rpl-option-button-label-color: rpl-color('extra_dark_neutral') !default;
$rpl-option-button-label-border-width: 1px !default;
$rpl-option-button-label-border: $rpl-option-button-label-border-width solid rpl-color('mid_neutral_1') !default;
$rpl-option-button-label-checked-border: $rpl-option-button-label-border-width solid rpl-color('primary') !default;
$rpl-option-button-text-focused-color: rpl-color('primary') !default;
$rpl-option-button-text-focused-border: $rpl-option-button-label-border-width solid rpl-color('primary') !default;
.rpl-option-button {
$root: &;
margin-left: $rpl-option-button-label-border-width;
margin-top: $rpl-option-button-label-border-width;
&__label {
position: relative;
display: inline-block;
background-color: $rpl-option-button-label-background-color;
color: $rpl-option-button-label-color;
border: $rpl-option-button-label-border;
padding: $rpl-space-2;
min-width: rem(40px);
box-sizing: border-box;
text-align: center;
margin-left: -$rpl-option-button-label-border-width;
margin-top: -$rpl-option-button-label-border-width;
&:focus-within {
border: $rpl-option-button-text-focused-border;
z-index: 1;
#{$root}__text {
text-decoration: underline;
color: $rpl-option-button-text-focused-color;
}
}
&--checked {
border: $rpl-option-button-label-checked-border;
z-index: 1;
#{$root}__text {
text-decoration: none;
}
}
}
&__radio {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
opacity: 0;
}
}
</style>
11 changes: 7 additions & 4 deletions packages/components/Molecules/Form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import fieldRplcheckbox from './fields/fieldRplcheckbox.vue'
import fieldRplchecklist from './fields/fieldRplchecklist.vue'
import fieldRpldatepicker from './fields/fieldRpldatepicker.vue'
import fieldRplsubmitloader from './fields/fieldRplsubmitloader.vue'
import fieldRploptionbutton from './fields/fieldRploptionbutton.vue'
import fieldRplclearform from './fields/fieldRplclearform.vue'
import fieldRpldivider from './fields/fieldRpldivider.vue'
import fieldRplmarkup from './fields/fieldRplmarkup.vue'
Expand All @@ -38,6 +39,7 @@ Vue.component('fieldRplcheckbox', fieldRplcheckbox)
Vue.component('fieldRplchecklist', fieldRplchecklist)
Vue.component('fieldRpldatepicker', fieldRpldatepicker)
Vue.component('fieldRplsubmitloader', fieldRplsubmitloader)
Vue.component('fieldRploptionbutton', fieldRploptionbutton)
Vue.component('fieldRplclearform', fieldRplclearform)
Vue.component('fieldRpldivider', fieldRpldivider)
Vue.component('fieldRplmarkup', fieldRplmarkup)
Expand All @@ -52,6 +54,7 @@ export default {
fieldRplchecklist,
fieldRpldatepicker,
fieldRplsubmitloader,
fieldRploptionbutton,
fieldRplclearform,
RplFormAlert,
RplFieldset
Expand Down Expand Up @@ -140,7 +143,7 @@ export default {
margin-top: 0;
}
label {
label:not(.rpl-option-button__label) {
@include rpl_typography_ruleset(('s', 1em, 'bold'));
color: rpl-color('extra_dark_neutral');
display: block;
Expand All @@ -166,7 +169,7 @@ export default {
@include rpl_form_text_element;
}
&[type='radio'],
&[type='radio']:not(.rpl-option-button__radio),
&[type='checkbox'] {
&+ label {
display: inline-block;
Expand All @@ -192,12 +195,12 @@ export default {
margin-right: $rpl-space-4;
}
input[type="radio"] {
input[type="radio"]:not(.rpl-option-button__radio) {
margin-right: $rpl-space-2;
}
}
input[type="radio"] {
input[type="radio"]:not(.rpl-option-button__radio) {
@include rpl_radio_button;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/components/Molecules/Form/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ storiesOf('Molecules/Form', module)
tel: '',
number: '',
radio: null,
optionButtonDefault: null,
optionButtonCustom: null,
textArea: null,
dateRange: ['', ''],
date: '',
Expand Down Expand Up @@ -194,6 +196,19 @@ storiesOf('Molecules/Form', module)
]
},

{
type: 'rploptionbutton',
label: 'Option button default',
model: 'optionButtonDefault'
},

{
type: 'rploptionbutton',
label: 'Option button custom',
model: 'optionButtonCustom',
optionValues: [ 'Option one', 'Option two', 'Option three', 'Option four', 'Option five', 'Option six', 'Option seven', 'Option eight' ]
},

{
type: 'textArea',
label: 'text area',
Expand Down
Loading

0 comments on commit 7bb06ba

Please sign in to comment.