Skip to content

Commit

Permalink
feat(@dpc-sdp/ripple-ui-core): ✨ add debounce option to search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
dylankelly committed Aug 2, 2023
1 parent e26c8ba commit 2badcc4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/ripple-ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"storybook:build": "build-storybook",
"stylelint": "stylelint \"**/*.css\"",
"test:components": "cypress run --component",
"cy:components": "cypress open --component",
"test:generate-output": "jest --json --outputFile=.jest-test-results.json"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import RplSearchBar from './RplSearchBar.vue'
import { mockSuggestions } from './fixtures'

const baseProps = {
suggestions: mockSuggestions,
id: '1234'
}

describe('RplSearchBar', () => {
it('opens', () => {
cy.mount(RplSearchBar, {
props: {
...baseProps
}
})
cy.get('#1234__menu').should('not.exist')
cy.get('input#1234').click()
cy.get('#1234__menu').should('exist')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ import {
import RplSearchBar from './RplSearchBar.vue'
import { a11yStoryCheck } from './../../../stories/interactions.js'
import { action } from '@storybook/addon-actions';
import { mockSuggestions } from './fixtures'

export const SingleTemplate = (args) => ({
components: { RplSearchBar },
setup() {
return { args }
return {
args,
callAction: action
}
},
data: () => ({ inputValue: null }),
computed: {
suggestions() {
const mockSuggestions = [
'rip',
'ripe',
'ripen',
'riptide',
'ripple',
'ripple component',
'ripple components',
]
if (!this.inputValue) {
return []
}
Expand All @@ -36,6 +31,7 @@ export const SingleTemplate = (args) => ({
submitAction: action('onSubmit'),
onUpdate: function(val) {
this.inputValue = val
action('onUpdate')(val)
}
}
})
Expand Down Expand Up @@ -95,4 +91,13 @@ export const SingleTemplate = (args) => ({
>
{SingleTemplate.bind()}
</Story>
<Story
name='Debounced update'
play={a11yStoryCheck}
args={{
id: 'default'
}}
>
{SingleTemplate.bind()}
</Story>
</Canvas>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default { inheritAttrs: false }

<script setup lang="ts">
import { ref, watch, nextTick, computed, onMounted, onUnmounted } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { onClickOutside, useDebounceFn } from '@vueuse/core'
import RplIcon from '../icon/RplIcon.vue'
import {
useRippleEvent,
Expand All @@ -22,6 +22,7 @@ interface Props {
suggestions?: string[]
maxSuggestionsDisplayed?: number
placeholder?: string
debounce?: number
}
const props = withDefaults(defineProps<Props>(), {
Expand All @@ -32,7 +33,8 @@ const props = withDefaults(defineProps<Props>(), {
inputValue: '',
suggestions: () => [],
maxSuggestionsDisplayed: 10,
placeholder: undefined
placeholder: undefined,
debounce: 300
})
const emit = defineEmits<{
Expand Down Expand Up @@ -84,12 +86,11 @@ const handleSubmit = () => {
)
}
const handleInputChange = (e) => {
const handleInputChange = useDebounceFn((e) => {
internalValue.value = e.target.value
emit('update:inputValue', e.target.value)
isOpen.value = true
}
}, props.debounce)
const handleSelectOption = (optionValue, focusBackOnInput) => {
if (focusBackOnInput) {
Expand Down Expand Up @@ -274,7 +275,9 @@ watch(activeOptionId, async (newId) => {
@click="handleSelectOption(option, false)"
@keydown="handleKeydown"
>
{{ option }}
<slot name="suggestion" :option="{ option }">
{{ option }}
</slot>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const mockSuggestions = [
'rip',
'ripe',
'ripen',
'riptide',
'ripple',
'ripple components',
'ripple component'
]

0 comments on commit 2badcc4

Please sign in to comment.