Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(radio): playground for compareWith #3237

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/api/radio.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ import LabelPlacement from '@site/static/usage/v7/radio/label-placement/index.md

<LabelPlacement />

## 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';

<UsingComparewith />

## 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.
Expand Down
2 changes: 1 addition & 1 deletion docs/api/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See discussion here and updated design doc

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```html
<ion-list>
<ion-radio-group [compareWith]="compareWith" (ionChange)="handleChange($event)">
<ion-item *ngFor="let food of foods; trackBy: trackItems">
<ion-radio [value]="food">{{ food.name }}</ion-radio>
</ion-item>
</ion-radio-group>
</ion-list>
```
Original file line number Diff line number Diff line change
@@ -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;
}
}
```
67 changes: 67 additions & 0 deletions static/usage/v7/radio/using-comparewith/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Radio</title>
<link rel="stylesheet" href="../../../common.css" />
<script src="../../../common.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@7/dist/ionic/ionic.esm.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@7/css/ionic.bundle.css" />
</head>

<body>
<ion-app>
<ion-content>
<div class="container">
<ion-list>
<ion-radio-group></ion-radio-group>
</ion-list>
</div>
</ion-content>
</ion-app>

<script>
const foods = [
{
id: 1,
name: 'Apples',
type: 'fruit',
},
{
id: 2,
name: 'Carrots',
type: 'vegetable',
},
{
id: 3,
name: 'Cupcakes',
type: 'dessert',
},
];

const compareWithFn = (o1, o2) => {
return o1.id === o2.id;
};

const radioGroupEl = document.querySelector('ion-radio-group');
radioGroupEl.compareWith = (a, b) => a.value === b.value;

foods.forEach((option, i) => {
const radio = document.createElement('ion-radio');

radio.value = option;
radio.textContent = option.name;

const item = document.createElement('ion-item');
item.appendChild(radio);

radioGroupEl.appendChild(item);
});

radioGroupEl.addEventListener('ionChange', () => {
console.log('Current value:', JSON.stringify(radioGroupEl.value));
});
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions static/usage/v7/radio/using-comparewith/index.md
Original file line number Diff line number Diff line change
@@ -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';

<Playground
version="7"
code={{
javascript,
react,
vue,
angular: {
files: {
'src/app/example.component.html': angular_example_component_html,
'src/app/example.component.ts': angular_example_component_ts,
},
},
}}
src="usage/v7/radio/using-comparewith/demo.html"
showConsole={true}
/>
48 changes: 48 additions & 0 deletions static/usage/v7/radio/using-comparewith/javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
```html
<ion-list>
<ion-radio-group></ion-radio-group>
</ion-list>

<script>
const foods = [
{
id: 1,
name: 'Apples',
type: 'fruit',
},
{
id: 2,
name: 'Carrots',
type: 'vegetable',
},
{
id: 3,
name: 'Cupcakes',
type: 'dessert',
},
];

const compareWithFn = (o1, o2) => {
return o1.id === o2.id;
};

const radioGroupEl = document.querySelector('ion-radio-group');
radioGroupEl.compareWith = compareWithFn;

foods.forEach((option, i) => {
const radio = document.createElement('ion-radio');

radio.value = option;
radio.textContent = option.name;

const item = document.createElement('ion-item');
item.appendChild(radio);

radioGroupEl.appendChild(item);
});

radioGroupEl.addEventListener('ionChange', () => {
console.log('Current value:', JSON.stringify(radioGroupEl.value));
});
</script>
```
52 changes: 52 additions & 0 deletions static/usage/v7/radio/using-comparewith/react.md
Original file line number Diff line number Diff line change
@@ -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 (
<IonList>
<IonRadioGroup
compareWith={compareWith}
onIonChange={(ev) => console.log('Current value:', JSON.stringify(ev.detail.value))}
>
{foods.map((food) => (
<IonItem>
<IonRadio key={food.id} value={food}>
{food.name}
</IonRadio>
</IonItem>
))}
</IonRadioGroup>
</IonList>
);
}
export default Example;
```
54 changes: 54 additions & 0 deletions static/usage/v7/radio/using-comparewith/vue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
```html
<template>
<ion-list>
<ion-radio-group :compareWith="compareWith" @ionChange="handleChange($event)">
<ion-item v-for="food in foods">
<ion-radio :value="food">{{ food.name }}</ion-radio>
</ion-item>
</ion-radio-group>
</ion-list>
</template>

<script lang="ts">
import { IonItem, IonList, IonRadio, IonRadioGroup } from '@ionic/vue';
import { defineComponent } from 'vue';

export default defineComponent({
components: {
IonItem,
IonList,
IonRadio,
IonRadioGroup,
},
data() {
return {
foods: [
{
id: 1,
name: 'Apples',
type: 'fruit',
},
{
id: 2,
name: 'Carrots',
type: 'vegetable',
},
{
id: 3,
name: 'Cupcakes',
type: 'dessert',
},
],
};
},
methods: {
compareWith(o1, o2) {
return o1.id === o2.id;
},
handleChange(ev) {
console.log('Current value:', JSON.stringify(ev.detail.value));
},
},
});
</script>
```
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
selectEl.appendChild(selectOption);
});

const valueLabel = document.querySelector('ion-text');
selectEl.addEventListener('ionChange', () => {
console.log('Current value:', JSON.stringify(selectEl.value));
});
Expand Down
Loading