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..1e5e93819fa --- /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..d7f803aea0b --- /dev/null +++ b/static/usage/v7/radio/using-comparewith/angular/example_component_ts.md @@ -0,0 +1,39 @@ +```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)); + } + + trackItems(index: number, item: any) { + return item.id; + } +} +``` 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)); });