Merge pull request #258 from reuters-graphics/mf-search-bar
Updates SearchInput
This commit is contained in:
commit
64c3857d0b
6 changed files with 69 additions and 62 deletions
29
src/components/SearchInput/SearchInput.mdx
Normal file
29
src/components/SearchInput/SearchInput.mdx
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { Meta, Canvas } from '@storybook/blocks';
|
||||||
|
|
||||||
|
import * as SearchInputStories from './SearchInput.stories.svelte';
|
||||||
|
|
||||||
|
<Meta of={SearchInputStories} />
|
||||||
|
|
||||||
|
# SearchInput
|
||||||
|
|
||||||
|
The `SearchInput` component creates a search bar.
|
||||||
|
|
||||||
|
```svelte
|
||||||
|
<script>
|
||||||
|
import { SearchInput } from '@reuters-graphics/graphics-components';
|
||||||
|
|
||||||
|
function filterData(newSearchText: string) {
|
||||||
|
/** This function would typically filter a dataset based on the search input.*/
|
||||||
|
console.log('Filtering data with:', newSearchText);
|
||||||
|
}
|
||||||
|
function handleSearchInput(newSearchText: string) {
|
||||||
|
/** Here's where you might update a variable,
|
||||||
|
filter a dataset or make an API call based on the user's input.*/
|
||||||
|
filterData(newSearchText);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<SearchInput onsearch={handleSearchInput} />
|
||||||
|
```
|
||||||
|
|
||||||
|
<Canvas of={SearchInputStories.Demo} />
|
||||||
|
|
@ -1,26 +1,24 @@
|
||||||
<script module lang="ts">
|
<script module lang="ts">
|
||||||
// @ts-ignore raw
|
import { defineMeta } from '@storybook/addon-svelte-csf';
|
||||||
import componentDocs from './stories/docs/component.md?raw';
|
|
||||||
|
|
||||||
import SearchInput from './SearchInput.svelte';
|
import SearchInput from './SearchInput.svelte';
|
||||||
|
|
||||||
import { withComponentDocs } from '$lib/docs/utils/withParams.js';
|
const { Story } = defineMeta({
|
||||||
|
|
||||||
export const meta = {
|
|
||||||
title: 'Components/Controls/SearchInput',
|
title: 'Components/Controls/SearchInput',
|
||||||
component: SearchInput,
|
component: SearchInput,
|
||||||
...withComponentDocs(componentDocs),
|
});
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script lang="ts">
|
||||||
import { Template, Story } from '@storybook/addon-svelte-csf';
|
function filterData(newSearchText: string) {
|
||||||
|
/** This function would typically filter a dataset based on the search input.*/
|
||||||
|
console.log('Filtering data with:', newSearchText);
|
||||||
|
}
|
||||||
|
function handleSearchInput(newSearchText: string) {
|
||||||
|
/** Here's where you might update a variable, filter a dataset or make an API call based on the user's input.*/
|
||||||
|
filterData(newSearchText);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Template>
|
<Story name="Demo">
|
||||||
{#snippet children({ args })}
|
<SearchInput onsearch={handleSearchInput} />
|
||||||
<SearchInput {...args} />
|
</Story>
|
||||||
{/snippet}
|
|
||||||
</Template>
|
|
||||||
|
|
||||||
<Story name="Default" args={{}} />
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,28 @@
|
||||||
<!-- @component `SearchInput` [Read the docs.](https://reuters-graphics.github.io/graphics-components/?path=/docs/components-controls-searchinput--docs) -->
|
<!-- @component `SearchInput` [Read the docs.](https://reuters-graphics.github.io/graphics-components/?path=/docs/components-controls-searchinput--docs) -->
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { createEventDispatcher } from 'svelte';
|
import MagnifyingGlass from './components/MagnifyingGlass.svelte';
|
||||||
import MagnifyingGlass from './MagnifyingGlass.svelte';
|
import X from './components/X.svelte';
|
||||||
import X from './X.svelte';
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
/**
|
/** The placeholder text that appears in the search box.*/
|
||||||
* The placeholder text that appears in the search box.
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
searchPlaceholder?: string;
|
searchPlaceholder?: string;
|
||||||
|
/** Optional function that runs when the input value changes. */
|
||||||
|
onsearch?: (newValue: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { searchPlaceholder = 'Search...' }: Props = $props();
|
let { searchPlaceholder = 'Search...', onsearch }: Props = $props();
|
||||||
|
|
||||||
let value = $state('');
|
let value = $state('');
|
||||||
let active = $derived(value !== '');
|
let active = $derived(value !== '');
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
|
||||||
|
|
||||||
function input(event) {
|
|
||||||
value = event.target.value;
|
|
||||||
dispatch('search', { value });
|
|
||||||
}
|
|
||||||
|
|
||||||
function clear() {
|
function clear() {
|
||||||
value = '';
|
value = '';
|
||||||
dispatch('search', { value: '' });
|
if (onsearch) onsearch(value); // Call the prop to update the parent when cleared
|
||||||
|
}
|
||||||
|
|
||||||
|
function oninput(event: Event) {
|
||||||
|
value = (event.target as HTMLInputElement).value;
|
||||||
|
if (onsearch) onsearch(value); // Update the parent on every input change
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
@ -34,13 +30,14 @@
|
||||||
<div class="search--icon absolute">
|
<div class="search--icon absolute">
|
||||||
<MagnifyingGlass />
|
<MagnifyingGlass />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<input
|
<input
|
||||||
id="search--input"
|
id="search--input"
|
||||||
class="search--input body-caption pl-8"
|
class="search--input body-caption pl-8"
|
||||||
type="text"
|
type="text"
|
||||||
placeholder={searchPlaceholder}
|
placeholder={searchPlaceholder}
|
||||||
oninput={input}
|
|
||||||
bind:value
|
bind:value
|
||||||
|
{oninput}
|
||||||
/>
|
/>
|
||||||
<div
|
<div
|
||||||
class="search--x absolute"
|
class="search--x absolute"
|
||||||
|
|
@ -55,7 +52,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
@use '../../scss/mixins' as *;
|
@use '../../scss/mixins' as mixins;
|
||||||
|
|
||||||
.search {
|
.search {
|
||||||
width: 250px;
|
width: 250px;
|
||||||
|
|
@ -64,11 +61,11 @@
|
||||||
top: 0.55rem;
|
top: 0.55rem;
|
||||||
width: 1.5rem;
|
width: 1.5rem;
|
||||||
height: 1.5rem;
|
height: 1.5rem;
|
||||||
fill: $theme-colour-brand-rules;
|
fill: mixins.$theme-colour-brand-rules;
|
||||||
}
|
}
|
||||||
.search--input {
|
.search--input {
|
||||||
height: 2.15rem;
|
height: 2.15rem;
|
||||||
border: 1px solid $theme-colour-brand-rules;
|
border: 1px solid mixins.$theme-colour-brand-rules;
|
||||||
background: transparent;
|
background: transparent;
|
||||||
border-radius: 0.25rem;
|
border-radius: 0.25rem;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
|
@ -78,7 +75,7 @@
|
||||||
top: 0.55rem;
|
top: 0.55rem;
|
||||||
width: 1.5rem;
|
width: 1.5rem;
|
||||||
height: 1.5rem;
|
height: 1.5rem;
|
||||||
fill: $theme-colour-text-primary;
|
fill: mixins.$theme-colour-text-primary;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
&.invisible {
|
&.invisible {
|
||||||
display: none;
|
display: none;
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,11 @@
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
@use '../../scss/mixins' as *;
|
@use '../../../scss/mixins' as mixins;
|
||||||
svg {
|
svg {
|
||||||
vertical-align: middle;
|
vertical-align: middle;
|
||||||
path {
|
path {
|
||||||
fill: $theme-colour-brand-rules;
|
fill: mixins.$theme-colour-brand-rules;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.7 KiB |
|
|
@ -20,14 +20,14 @@
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
@use '../../scss/mixins' as *;
|
@use '../../../scss/mixins' as mixins;
|
||||||
svg {
|
svg {
|
||||||
vertical-align: middle;
|
vertical-align: middle;
|
||||||
path {
|
path {
|
||||||
fill: $theme-colour-brand-rules;
|
fill: mixins.$theme-colour-brand-rules;
|
||||||
}
|
}
|
||||||
rect {
|
rect {
|
||||||
fill: $theme-colour-background;
|
fill: mixins.$theme-colour-background;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
Before Width: | Height: | Size: 2 KiB After Width: | Height: | Size: 2 KiB |
|
|
@ -1,17 +0,0 @@
|
||||||
Invite users to search for something.
|
|
||||||
|
|
||||||
```svelte
|
|
||||||
<script>
|
|
||||||
import { SearchInput } from '@reuters-graphics/graphics-components';
|
|
||||||
|
|
||||||
function handleSearchInput(event) {
|
|
||||||
const searchText = event.detail.value;
|
|
||||||
// Here's where you might update a variable,
|
|
||||||
// filter a dataset or make an API call
|
|
||||||
// based on the user's input.
|
|
||||||
console.log(`Search for ${searchText}`);
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<SearchInput on:search="{handleSearchInput}" />
|
|
||||||
```
|
|
||||||
Loading…
Reference in a new issue