mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
fix(segment): checked can be changed dynamically
This commit is contained in:
2
core/src/components.d.ts
vendored
2
core/src/components.d.ts
vendored
@ -5422,7 +5422,6 @@ declare global {
|
|||||||
|
|
||||||
namespace StencilComponents {
|
namespace StencilComponents {
|
||||||
interface IonSegmentButton {
|
interface IonSegmentButton {
|
||||||
'activated': boolean;
|
|
||||||
/**
|
/**
|
||||||
* If true, the segment button is selected. Defaults to `false`.
|
* If true, the segment button is selected. Defaults to `false`.
|
||||||
*/
|
*/
|
||||||
@ -5466,7 +5465,6 @@ declare global {
|
|||||||
}
|
}
|
||||||
namespace JSXElements {
|
namespace JSXElements {
|
||||||
export interface IonSegmentButtonAttributes extends HTMLAttributes {
|
export interface IonSegmentButtonAttributes extends HTMLAttributes {
|
||||||
'activated'?: boolean;
|
|
||||||
/**
|
/**
|
||||||
* If true, the segment button is selected. Defaults to `false`.
|
* If true, the segment button is selected. Defaults to `false`.
|
||||||
*/
|
*/
|
||||||
|
@ -8,11 +8,6 @@ Segment buttons are groups of related buttons inside of a [Segment](../../segmen
|
|||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
#### activated
|
|
||||||
|
|
||||||
boolean
|
|
||||||
|
|
||||||
|
|
||||||
#### checked
|
#### checked
|
||||||
|
|
||||||
boolean
|
boolean
|
||||||
@ -58,11 +53,6 @@ The value of the segment button.
|
|||||||
|
|
||||||
## Attributes
|
## Attributes
|
||||||
|
|
||||||
#### activated
|
|
||||||
|
|
||||||
boolean
|
|
||||||
|
|
||||||
|
|
||||||
#### checked
|
#### checked
|
||||||
|
|
||||||
boolean
|
boolean
|
||||||
|
@ -42,18 +42,18 @@ ion-segment-button {
|
|||||||
line-height: $segment-button-ios-icon-line-height;
|
line-height: $segment-button-ios-icon-line-height;
|
||||||
}
|
}
|
||||||
|
|
||||||
&.segment-activated {
|
&.segment-checked {
|
||||||
color: $segment-button-ios-text-color;
|
color: $segment-button-ios-text-color;
|
||||||
background-color: $segment-button-ios-background-color-activated;
|
background-color: $segment-button-ios-background-color-activated;
|
||||||
transition: $segment-button-ios-transition-activated;
|
transition: $segment-button-ios-transition-activated;
|
||||||
}
|
}
|
||||||
|
|
||||||
&:hover:not(.segment-activated) {
|
&:hover:not(.segment-checked) {
|
||||||
background-color: $segment-button-ios-background-color-hover;
|
background-color: $segment-button-ios-background-color-hover;
|
||||||
transition: $segment-button-ios-transition-hover;
|
transition: $segment-button-ios-transition-hover;
|
||||||
}
|
}
|
||||||
|
|
||||||
&:active:not(.segment-activated) {
|
&:active:not(.segment-checked) {
|
||||||
background-color: $segment-button-ios-background-color-active;
|
background-color: $segment-button-ios-background-color-active;
|
||||||
transition: $segment-button-ios-transition-active;
|
transition: $segment-button-ios-transition-active;
|
||||||
}
|
}
|
||||||
@ -116,15 +116,15 @@ ion-segment-button {
|
|||||||
border-color: $color-base;
|
border-color: $color-base;
|
||||||
color: $color-base;
|
color: $color-base;
|
||||||
|
|
||||||
&:hover:not(.segment-activated) {
|
&:hover:not(.segment-checked) {
|
||||||
background-color: ion-color($colors-ios, $color-name, base, ios, $segment-button-ios-background-color-alpha-hover);
|
background-color: ion-color($colors-ios, $color-name, base, ios, $segment-button-ios-background-color-alpha-hover);
|
||||||
}
|
}
|
||||||
|
|
||||||
&:active:not(.segment-activated) {
|
&:active:not(.segment-checked) {
|
||||||
background-color: ion-color($colors-ios, $color-name, base, ios, $segment-button-ios-background-color-alpha-active);
|
background-color: ion-color($colors-ios, $color-name, base, ios, $segment-button-ios-background-color-alpha-active);
|
||||||
}
|
}
|
||||||
|
|
||||||
&.segment-activated {
|
&.segment-checked {
|
||||||
color: $color-contrast;
|
color: $color-contrast;
|
||||||
background-color: $color-base;
|
background-color: $color-base;
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ ion-segment-button {
|
|||||||
|
|
||||||
@include ios-segment-button($color-name);
|
@include ios-segment-button($color-name);
|
||||||
|
|
||||||
.toolbar-ios-#{$color-name} .segment-button-ios.segment-activated {
|
.toolbar-ios-#{$color-name} .segment-button-ios.segment-checked {
|
||||||
color: $color-base;
|
color: $color-base;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
&.activated,
|
&.activated,
|
||||||
&.segment-activated {
|
&.segment-checked {
|
||||||
border-color: $segment-button-md-border-color-activated;
|
border-color: $segment-button-md-border-color-activated;
|
||||||
opacity: $segment-button-md-opacity-activated;
|
opacity: $segment-button-md-opacity-activated;
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@
|
|||||||
color: $color-base;
|
color: $color-base;
|
||||||
|
|
||||||
&.activated,
|
&.activated,
|
||||||
&.segment-activated {
|
&.segment-checked {
|
||||||
border-color: $color-base;
|
border-color: $color-base;
|
||||||
color: $color-base;
|
color: $color-base;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Component, Element, Event, EventEmitter, Prop } from '@stencil/core';
|
import { Component, Element, Event, EventEmitter, Prop, Watch } from '@stencil/core';
|
||||||
import { Mode } from '../../interface';
|
import { Mode } from '../../interface';
|
||||||
import { createThemedClasses, getElementClassMap } from '../../utils/theme';
|
import { createThemedClasses, getElementClassMap } from '../../utils/theme';
|
||||||
|
|
||||||
@ -15,8 +15,6 @@ export class SegmentButton {
|
|||||||
|
|
||||||
@Element() el!: HTMLElement;
|
@Element() el!: HTMLElement;
|
||||||
|
|
||||||
@Prop({ mutable: true }) activated = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The color to use for the text color.
|
* The color to use for the text color.
|
||||||
* Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`.
|
* Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`.
|
||||||
@ -32,7 +30,7 @@ export class SegmentButton {
|
|||||||
/**
|
/**
|
||||||
* If true, the segment button is selected. Defaults to `false`.
|
* If true, the segment button is selected. Defaults to `false`.
|
||||||
*/
|
*/
|
||||||
@Prop() checked = false;
|
@Prop({mutable: true}) checked = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If true, the user cannot interact with the segment button. Default false.
|
* If true, the user cannot interact with the segment button. Default false.
|
||||||
@ -55,11 +53,11 @@ export class SegmentButton {
|
|||||||
*/
|
*/
|
||||||
@Event() ionSelect!: EventEmitter<void>;
|
@Event() ionSelect!: EventEmitter<void>;
|
||||||
|
|
||||||
/**
|
@Watch('checked')
|
||||||
* Emit the click event to the parent segment
|
checkedChanged(checked: boolean, prev: boolean) {
|
||||||
*/
|
if (checked && !prev) {
|
||||||
private onClick() {
|
this.ionSelect.emit();
|
||||||
this.ionSelect.emit();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@ -68,7 +66,7 @@ export class SegmentButton {
|
|||||||
|
|
||||||
const buttonClasses = {
|
const buttonClasses = {
|
||||||
'segment-button-disabled': this.disabled,
|
'segment-button-disabled': this.disabled,
|
||||||
'segment-activated': this.activated,
|
'segment-checked': this.checked,
|
||||||
...themedClasses,
|
...themedClasses,
|
||||||
...hostClasses,
|
...hostClasses,
|
||||||
};
|
};
|
||||||
@ -81,11 +79,11 @@ export class SegmentButton {
|
|||||||
return [
|
return [
|
||||||
<TagType
|
<TagType
|
||||||
{...attrs}
|
{...attrs}
|
||||||
aria-pressed={this.activated}
|
aria-pressed={this.checked}
|
||||||
class={buttonClasses}
|
class={buttonClasses}
|
||||||
disabled={this.disabled}
|
disabled={this.disabled}
|
||||||
href={this.href}
|
href={this.href}
|
||||||
onClick={this.onClick.bind(this)}>
|
onClick={() => this.checked = true }>
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
{ this.mode === 'md' && <ion-ripple-effect tapClick={true}/> }
|
{ this.mode === 'md' && <ion-ripple-effect tapClick={true}/> }
|
||||||
</TagType>
|
</TagType>
|
||||||
|
@ -40,7 +40,7 @@ export class Segment {
|
|||||||
|
|
||||||
@Watch('value')
|
@Watch('value')
|
||||||
protected valueChanged(value: string | undefined) {
|
protected valueChanged(value: string | undefined) {
|
||||||
this.selectButton();
|
this.update();
|
||||||
this.ionChange.emit({value});
|
this.ionChange.emit({value});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,20 +56,21 @@ export class Segment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidLoad() {
|
componentDidLoad() {
|
||||||
this.selectButton();
|
if (this.value === undefined) {
|
||||||
|
const buttons = Array.from(this.el.querySelectorAll('ion-segment-button'));
|
||||||
|
const checked = buttons.find(b => b.checked);
|
||||||
|
if (checked) {
|
||||||
|
this.value = checked.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
private selectButton() {
|
private update() {
|
||||||
const value = this.value;
|
const value = this.value;
|
||||||
const buttons = Array.from(this.el.querySelectorAll('ion-segment-button'));
|
const buttons = Array.from(this.el.querySelectorAll('ion-segment-button'));
|
||||||
for (const button of buttons) {
|
for (const button of buttons) {
|
||||||
button.activated = (button.value === value);
|
button.checked = (button.value === value);
|
||||||
|
|
||||||
// If there is no value set on the segment and a button
|
|
||||||
// is checked we should activate it
|
|
||||||
if (!value && button.checked) {
|
|
||||||
button.activated = button.checked;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user