From 9d0991854c3954aa802b27ba7dccaa1a9e1e41fb Mon Sep 17 00:00:00 2001 From: Shawn Taylor Date: Wed, 8 Nov 2023 10:49:52 -0500 Subject: [PATCH 1/2] docs(radio): playground for compareWith --- docs/api/radio.md | 8 +++ docs/api/select.md | 2 +- .../angular/example_component_html.md | 9 +++ .../angular/example_component_ts.md | 35 ++++++++++ .../v7/radio/using-comparewith/demo.html | 67 +++++++++++++++++++ .../usage/v7/radio/using-comparewith/index.md | 25 +++++++ .../v7/radio/using-comparewith/javascript.md | 48 +++++++++++++ .../usage/v7/radio/using-comparewith/react.md | 52 ++++++++++++++ .../usage/v7/radio/using-comparewith/vue.md | 54 +++++++++++++++ .../using-comparewith/javascript.md | 1 - 10 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 static/usage/v7/radio/using-comparewith/angular/example_component_html.md create mode 100644 static/usage/v7/radio/using-comparewith/angular/example_component_ts.md create mode 100644 static/usage/v7/radio/using-comparewith/demo.html create mode 100644 static/usage/v7/radio/using-comparewith/index.md create mode 100644 static/usage/v7/radio/using-comparewith/javascript.md create mode 100644 static/usage/v7/radio/using-comparewith/react.md create mode 100644 static/usage/v7/radio/using-comparewith/vue.md diff --git a/docs/api/radio.md b/docs/api/radio.md index 9473c5cc790..ca519103739 100644 --- a/docs/api/radio.md +++ b/docs/api/radio.md @@ -36,6 +36,14 @@ import LabelPlacement from '@site/static/usage/v7/radio/label-placement/index.md +## Object Value References + +By default, the radio group uses strict equality (`===`) to determine if an option is selected. This can be overridden by providing a property name or a function to the `compareWith` property. + +import UsingComparewith from '@site/static/usage/v7/radio/using-comparewith/index.md'; + + + ## Alignment Developers can use the `alignment` property to control how the label and control are aligned on the cross axis. This property mirrors the flexbox `align-items` property. diff --git a/docs/api/select.md b/docs/api/select.md index 8dc81783b05..b462d92a696 100644 --- a/docs/api/select.md +++ b/docs/api/select.md @@ -119,7 +119,7 @@ import RespondingToInteractionExample from '@site/static/usage/v7/select/basic/r When using objects for select values, it is possible for the identities of these objects to change if they are coming from a server or database, while the selected value's identity remains the same. For example, this can occur when an existing record with the desired object value is loaded into the select, but the newly retrieved select options now have different identities. This will result in the select appearing to have no value at all, even though the original selection in still intact. -By default, the select uses object equality (`===`) to determine if an option is selected. This can be overridden by providing a property name or a function to the `compareWith` property. +By default, the select uses strict equality (`===`) to determine if an option is selected. This can be overridden by providing a property name or a function to the `compareWith` property. ### Using compareWith diff --git a/static/usage/v7/radio/using-comparewith/angular/example_component_html.md b/static/usage/v7/radio/using-comparewith/angular/example_component_html.md new file mode 100644 index 00000000000..725cb9cd76b --- /dev/null +++ b/static/usage/v7/radio/using-comparewith/angular/example_component_html.md @@ -0,0 +1,9 @@ +```html + + + + {{ food.name }} + + + +``` diff --git a/static/usage/v7/radio/using-comparewith/angular/example_component_ts.md b/static/usage/v7/radio/using-comparewith/angular/example_component_ts.md new file mode 100644 index 00000000000..7f9b5990559 --- /dev/null +++ b/static/usage/v7/radio/using-comparewith/angular/example_component_ts.md @@ -0,0 +1,35 @@ +```ts +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-example', + templateUrl: 'example.component.html', +}) +export class ExampleComponent { + foods = [ + { + id: 1, + name: 'Apples', + type: 'fruit', + }, + { + id: 2, + name: 'Carrots', + type: 'vegetable', + }, + { + id: 3, + name: 'Cupcakes', + type: 'dessert', + }, + ]; + + compareWith(o1, o2) { + return o1.id === o2.id; + } + + handleChange(ev) { + console.log('Current value:', JSON.stringify(ev.target.value)); + } +} +``` diff --git a/static/usage/v7/radio/using-comparewith/demo.html b/static/usage/v7/radio/using-comparewith/demo.html new file mode 100644 index 00000000000..4903e4b04d4 --- /dev/null +++ b/static/usage/v7/radio/using-comparewith/demo.html @@ -0,0 +1,67 @@ + + + + + + Radio + + + + + + + + + +
+ + + +
+
+
+ + + + diff --git a/static/usage/v7/radio/using-comparewith/index.md b/static/usage/v7/radio/using-comparewith/index.md new file mode 100644 index 00000000000..6db5886795d --- /dev/null +++ b/static/usage/v7/radio/using-comparewith/index.md @@ -0,0 +1,25 @@ +import Playground from '@site/src/components/global/Playground'; + +import javascript from './javascript.md'; +import react from './react.md'; +import vue from './vue.md'; + +import angular_example_component_html from './angular/example_component_html.md'; +import angular_example_component_ts from './angular/example_component_ts.md'; + + diff --git a/static/usage/v7/radio/using-comparewith/javascript.md b/static/usage/v7/radio/using-comparewith/javascript.md new file mode 100644 index 00000000000..8dd2130b3e8 --- /dev/null +++ b/static/usage/v7/radio/using-comparewith/javascript.md @@ -0,0 +1,48 @@ +```html + + + + + +``` diff --git a/static/usage/v7/radio/using-comparewith/react.md b/static/usage/v7/radio/using-comparewith/react.md new file mode 100644 index 00000000000..dff3b4233a6 --- /dev/null +++ b/static/usage/v7/radio/using-comparewith/react.md @@ -0,0 +1,52 @@ +```tsx +import React from 'react'; +import { IonItem, IonList, IonRadio, IonRadioGroup } from '@ionic/react'; + +interface Food { + id: number; + name: string; + type: string; +} + +const foods: Food[] = [ + { + id: 1, + name: 'Apples', + type: 'fruit', + }, + { + id: 2, + name: 'Carrots', + type: 'vegetable', + }, + { + id: 3, + name: 'Cupcakes', + type: 'dessert', + }, +]; + +const compareWith = (o1: Food, o2: Food) => { + return o1.id === o2.id; +}; + +function Example() { + return ( + + console.log('Current value:', JSON.stringify(ev.detail.value))} + > + {foods.map((food) => ( + + + {food.name} + + + ))} + + + ); +} +export default Example; +``` diff --git a/static/usage/v7/radio/using-comparewith/vue.md b/static/usage/v7/radio/using-comparewith/vue.md new file mode 100644 index 00000000000..28518284654 --- /dev/null +++ b/static/usage/v7/radio/using-comparewith/vue.md @@ -0,0 +1,54 @@ +```html + + + +``` diff --git a/static/usage/v7/select/objects-as-values/using-comparewith/javascript.md b/static/usage/v7/select/objects-as-values/using-comparewith/javascript.md index 4c4a831c91f..51504820ca1 100644 --- a/static/usage/v7/select/objects-as-values/using-comparewith/javascript.md +++ b/static/usage/v7/select/objects-as-values/using-comparewith/javascript.md @@ -38,7 +38,6 @@ selectEl.appendChild(selectOption); }); - const valueLabel = document.querySelector('ion-text'); selectEl.addEventListener('ionChange', () => { console.log('Current value:', JSON.stringify(selectEl.value)); }); From 7f989a486d263e34e2d2aaacbcda7761d30bf7af Mon Sep 17 00:00:00 2001 From: Shawn Taylor Date: Thu, 9 Nov 2023 10:19:54 -0500 Subject: [PATCH 2/2] trackBy --- .../radio/using-comparewith/angular/example_component_html.md | 2 +- .../radio/using-comparewith/angular/example_component_ts.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/static/usage/v7/radio/using-comparewith/angular/example_component_html.md b/static/usage/v7/radio/using-comparewith/angular/example_component_html.md index 725cb9cd76b..1e5e93819fa 100644 --- a/static/usage/v7/radio/using-comparewith/angular/example_component_html.md +++ b/static/usage/v7/radio/using-comparewith/angular/example_component_html.md @@ -1,7 +1,7 @@ ```html - + {{ food.name }} diff --git a/static/usage/v7/radio/using-comparewith/angular/example_component_ts.md b/static/usage/v7/radio/using-comparewith/angular/example_component_ts.md index 7f9b5990559..d7f803aea0b 100644 --- a/static/usage/v7/radio/using-comparewith/angular/example_component_ts.md +++ b/static/usage/v7/radio/using-comparewith/angular/example_component_ts.md @@ -31,5 +31,9 @@ export class ExampleComponent { handleChange(ev) { console.log('Current value:', JSON.stringify(ev.target.value)); } + + trackItems(index: number, item: any) { + return item.id; + } } ```