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">
|
||||
// @ts-ignore raw
|
||||
import componentDocs from './stories/docs/component.md?raw';
|
||||
|
||||
import { defineMeta } from '@storybook/addon-svelte-csf';
|
||||
import SearchInput from './SearchInput.svelte';
|
||||
|
||||
import { withComponentDocs } from '$lib/docs/utils/withParams.js';
|
||||
|
||||
export const meta = {
|
||||
const { Story } = defineMeta({
|
||||
title: 'Components/Controls/SearchInput',
|
||||
component: SearchInput,
|
||||
...withComponentDocs(componentDocs),
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
import { Template, Story } from '@storybook/addon-svelte-csf';
|
||||
<script lang="ts">
|
||||
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>
|
||||
|
||||
<Template>
|
||||
{#snippet children({ args })}
|
||||
<SearchInput {...args} />
|
||||
{/snippet}
|
||||
</Template>
|
||||
|
||||
<Story name="Default" args={{}} />
|
||||
<Story name="Demo">
|
||||
<SearchInput onsearch={handleSearchInput} />
|
||||
</Story>
|
||||
|
|
|
|||
|
|
@ -1,32 +1,28 @@
|
|||
<!-- @component `SearchInput` [Read the docs.](https://reuters-graphics.github.io/graphics-components/?path=/docs/components-controls-searchinput--docs) -->
|
||||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
import MagnifyingGlass from './MagnifyingGlass.svelte';
|
||||
import X from './X.svelte';
|
||||
import MagnifyingGlass from './components/MagnifyingGlass.svelte';
|
||||
import X from './components/X.svelte';
|
||||
|
||||
interface Props {
|
||||
/**
|
||||
* The placeholder text that appears in the search box.
|
||||
* @type {string}
|
||||
*/
|
||||
/** The placeholder text that appears in the search box.*/
|
||||
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 active = $derived(value !== '');
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
function input(event) {
|
||||
value = event.target.value;
|
||||
dispatch('search', { value });
|
||||
}
|
||||
|
||||
function clear() {
|
||||
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>
|
||||
|
||||
|
|
@ -34,13 +30,14 @@
|
|||
<div class="search--icon absolute">
|
||||
<MagnifyingGlass />
|
||||
</div>
|
||||
|
||||
<input
|
||||
id="search--input"
|
||||
class="search--input body-caption pl-8"
|
||||
type="text"
|
||||
placeholder={searchPlaceholder}
|
||||
oninput={input}
|
||||
bind:value
|
||||
{oninput}
|
||||
/>
|
||||
<div
|
||||
class="search--x absolute"
|
||||
|
|
@ -55,7 +52,7 @@
|
|||
</div>
|
||||
|
||||
<style lang="scss">
|
||||
@use '../../scss/mixins' as *;
|
||||
@use '../../scss/mixins' as mixins;
|
||||
|
||||
.search {
|
||||
width: 250px;
|
||||
|
|
@ -64,11 +61,11 @@
|
|||
top: 0.55rem;
|
||||
width: 1.5rem;
|
||||
height: 1.5rem;
|
||||
fill: $theme-colour-brand-rules;
|
||||
fill: mixins.$theme-colour-brand-rules;
|
||||
}
|
||||
.search--input {
|
||||
height: 2.15rem;
|
||||
border: 1px solid $theme-colour-brand-rules;
|
||||
border: 1px solid mixins.$theme-colour-brand-rules;
|
||||
background: transparent;
|
||||
border-radius: 0.25rem;
|
||||
width: 100%;
|
||||
|
|
@ -78,7 +75,7 @@
|
|||
top: 0.55rem;
|
||||
width: 1.5rem;
|
||||
height: 1.5rem;
|
||||
fill: $theme-colour-text-primary;
|
||||
fill: mixins.$theme-colour-text-primary;
|
||||
cursor: pointer;
|
||||
&.invisible {
|
||||
display: none;
|
||||
|
|
|
|||
|
|
@ -11,11 +11,11 @@
|
|||
</svg>
|
||||
|
||||
<style lang="scss">
|
||||
@use '../../scss/mixins' as *;
|
||||
@use '../../../scss/mixins' as mixins;
|
||||
svg {
|
||||
vertical-align: middle;
|
||||
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>
|
||||
|
||||
<style lang="scss">
|
||||
@use '../../scss/mixins' as *;
|
||||
@use '../../../scss/mixins' as mixins;
|
||||
svg {
|
||||
vertical-align: middle;
|
||||
path {
|
||||
fill: $theme-colour-brand-rules;
|
||||
fill: mixins.$theme-colour-brand-rules;
|
||||
}
|
||||
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