|
| 1 | +import { IonBackButton, IonButtons, IonContent, IonHeader, IonItem, IonList, IonPage, IonRadio, IonRadioGroup, IonSearchbar, IonTitle, IonToolbar } from '@ionic/react'; |
| 2 | +import React, { useState } from 'react'; |
| 3 | + |
| 4 | +interface RadioGroupProps { |
| 5 | + |
| 6 | +} |
| 7 | + |
| 8 | +export interface Item { |
| 9 | + text: string; |
| 10 | + value: string; |
| 11 | +} |
| 12 | + |
| 13 | +const items: Item[] = [ |
| 14 | + { text: 'Apple', value: 'apple' }, |
| 15 | + { text: 'Apricot', value: 'apricot' }, |
| 16 | + { text: 'Banana', value: 'banana' }, |
| 17 | + { text: 'Blackberry', value: 'blackberry' }, |
| 18 | + { text: 'Blueberry', value: 'blueberry' }, |
| 19 | + { text: 'Cherry', value: 'cherry' }, |
| 20 | + { text: 'Cranberry', value: 'cranberry' }, |
| 21 | + { text: 'Grape', value: 'grape' }, |
| 22 | + { text: 'Grapefruit', value: 'grapefruit' }, |
| 23 | + { text: 'Guava', value: 'guava' }, |
| 24 | +]; |
| 25 | + |
| 26 | +const RadioGroup: React.FC<RadioGroupProps> = () => { |
| 27 | + const [filteredItems, setFilteredItems] = useState<Item[]>([...items]); |
| 28 | + |
| 29 | + const searchbarInput = (event: any) => { |
| 30 | + filterList(event.target.value); |
| 31 | + }; |
| 32 | + |
| 33 | + /** |
| 34 | + * Update the rendered view with |
| 35 | + * the provided search query. If no |
| 36 | + * query is provided, all data |
| 37 | + * will be rendered. |
| 38 | + */ |
| 39 | + const filterList = (searchQuery: string | null | undefined) => { |
| 40 | + /** |
| 41 | + * If no search query is defined, |
| 42 | + * return all options. |
| 43 | + */ |
| 44 | + if (searchQuery === undefined || searchQuery === null) { |
| 45 | + setFilteredItems([...items]); |
| 46 | + } else { |
| 47 | + /** |
| 48 | + * Otherwise, normalize the search |
| 49 | + * query and check to see which items |
| 50 | + * contain the search query as a substring. |
| 51 | + */ |
| 52 | + const normalizedQuery = searchQuery.toLowerCase(); |
| 53 | + setFilteredItems( |
| 54 | + items.filter((item) => { |
| 55 | + return item.text.toLowerCase().includes(normalizedQuery); |
| 56 | + }) |
| 57 | + ); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + return ( |
| 62 | + <IonPage> |
| 63 | + <IonHeader> |
| 64 | + <IonToolbar> |
| 65 | + <IonButtons slot="start"> |
| 66 | + <IonBackButton></IonBackButton> |
| 67 | + </IonButtons> |
| 68 | + <IonTitle>Radio Group</IonTitle> |
| 69 | + </IonToolbar> |
| 70 | + <IonToolbar> |
| 71 | + <IonSearchbar onIonInput={searchbarInput}></IonSearchbar> |
| 72 | + </IonToolbar> |
| 73 | + </IonHeader> |
| 74 | + <IonContent> |
| 75 | + <IonList inset={true}> |
| 76 | + <IonRadioGroup> |
| 77 | + {filteredItems.map((item) => ( |
| 78 | + <IonItem key={item.value}> |
| 79 | + <IonRadio value={item.value}>{item.text}</IonRadio> |
| 80 | + </IonItem> |
| 81 | + ))} |
| 82 | + </IonRadioGroup> |
| 83 | + </IonList> |
| 84 | + </IonContent> |
| 85 | + </IonPage> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export default RadioGroup; |
0 commit comments