Skip to content

Commit

Permalink
feat: add focus method to text inputs (#932)
Browse files Browse the repository at this point in the history
Co-authored-by: Lee Chase <lee.chase@uk.ibm.com>
  • Loading branch information
lee-chase and lee-chase authored Jun 23, 2020
1 parent 803d36c commit 64ab976
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/components/cv-dropdown/cv-dropdown-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ https://www.carbondesignsystem.com/components/dropdown/code
- up - diretction of dropdown
- value - string matching value of one of the options or ''

## Methods

Focus - sets focus to the text dropdown

### cv-dropdown-item

- value - The value that will be returned upon selection of the dropdown item by users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ http://www.carbondesignsystem.com/components/text-input/code

Other standard props e.g. disabled and placeholder

## Methods

Focus - sets focus to the text area

## slots

- helper-text: optional and overrides the helper-text attribute
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/components/cv-text-area/cv-text-area.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
v-bind="$attrs"
:value="value"
v-on="inputListeners"
ref="textarea"
></textarea>
</div>
<div class="bx--form-requirement" v-if="isInvalid">
Expand Down Expand Up @@ -75,6 +76,11 @@ export default {
this.isInvalid = !!(this.$slots['invalid-message'] || (this.invalidMessage && this.invalidMessage.length));
this.isHelper = !!(this.$slots['helper-text'] || (this.helperText && this.helperText.length));
},
focus() {
this.$nextTick(() => {
this.$refs.textarea.focus();
});
},
},
};
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ http://www.carbondesignsystem.com/components/text-input/code

Other standard props e.g. disabled and placeholder

## Methods

Focus - sets focus to the text input

## slots

- helper-text: optional and overrides the helper-text attribute
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/components/cv-text-input/cv-text-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export default {
this.$refs.input.value = currentValue;
});
},
focus() {
this.$nextTick(() => {
this.$refs.input.focus();
});
},
},
};
</script>
5 changes: 5 additions & 0 deletions storybook/stories/cv-dropdown-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ for (const story of storySet) {
const templateViewString = `
<sv-template-view
sv-margin
ref="templateView"
:sv-alt-back="this.$options.propsData.theme !== 'light'"
sv-source='${templateString.trim()}'>
<template slot="component">${templateString}</template>
Expand All @@ -159,6 +160,7 @@ for (const story of storySet) {
</select>
</span>
</div>
<button @click="focus">Call focus method on component</button>
</template>
</sv-template-view>
`;
Expand All @@ -177,6 +179,9 @@ for (const story of storySet) {
},
methods: {
actionChange: action('CV Dropdown - change'),
focus() {
this.$refs.templateView.$slots.component[0].componentInstance.focus();
},
},
template: templateViewString,
watch: {
Expand Down
5 changes: 5 additions & 0 deletions storybook/stories/cv-text-area-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ for (const story of storySet) {
const templateViewString = `
<sv-template-view
sv-margin
ref="templateView"
:sv-alt-back="this.$options.propsData.theme !== 'light'"
sv-source='${templateString.trim()}'>
<template slot="component">${templateString}</template>
Expand All @@ -117,6 +118,7 @@ for (const story of storySet) {
<textarea v-model="modelValue"></textarea>
</label>
</div>
<button @click="focus">Call focus method on component</button>
</template>
</sv-template-view>
`;
Expand All @@ -132,6 +134,9 @@ for (const story of storySet) {
props: settings.props,
methods: {
onInput: action('cv-text-area - input event'),
focus() {
this.$refs.templateView.$slots.component[0].componentInstance.focus();
},
},
};
},
Expand Down
5 changes: 5 additions & 0 deletions storybook/stories/cv-text-input-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ for (const story of storySet) {
const templateViewString = `
<sv-template-view
sv-margin
ref="templateView"
:sv-alt-back="this.$options.propsData.theme !== 'light'"
sv-source='${templateString.trim()}'>
<template slot="component">${templateString}</template>
Expand All @@ -143,6 +144,7 @@ for (const story of storySet) {
<input type="text" v-model="modelValue" />
</label>
</div>
<button @click="focus">Call focus method on component</button>
</template>
</sv-template-view>
`;
Expand All @@ -158,6 +160,9 @@ for (const story of storySet) {
props: settings.props,
methods: {
onInput: action('cv-text-input - input event'),
focus() {
this.$refs.templateView.$slots.component[0].componentInstance.focus();
},
},
};
},
Expand Down
5 changes: 3 additions & 2 deletions storybook/stories/cv-tooltip-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ for (const story of storySet) {

const templateViewString = `
<sv-template-view
ref="templateView"
sv-margin
sv-source='${templateString.trim()}'
sv-position="center"
Expand All @@ -114,10 +115,10 @@ for (const story of storySet) {
props: settings.props,
methods: {
show() {
this.$children[0].$children[0].$children[0].show();
this.$refs.templateView.$slots.component[0].componentInstance.show();
},
hide() {
this.$children[0].$children[0].$children[0].hide();
this.$refs.templateView.$slots.component[0].componentInstance.hide();
},
},
};
Expand Down

0 comments on commit 64ab976

Please sign in to comment.