Skip to content

Commit

Permalink
feat!: Range remove deprecated API for labels of segments (#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov authored and splincode committed Aug 6, 2022
1 parent c4c74e9 commit 46dd750
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 171 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<tui-range
id="range-with-segments"
new
size="m"
class="range"
[min]="min"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<tui-range
id="range-with-key-steps"
new
size="m"
class="range"
[min]="min"
Expand Down
13 changes: 1 addition & 12 deletions projects/demo/src/modules/components/range/range.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {TuiDocExample} from '@taiga-ui/addon-doc';
import {TuiPluralize, TuiSizeS} from '@taiga-ui/core';
import {TuiSizeS} from '@taiga-ui/core';
import {TuiKeySteps} from '@taiga-ui/kit';

@Component({
Expand Down Expand Up @@ -68,17 +68,6 @@ export class ExampleTuiRangeComponent {

steps = this.stepsVariants[0];

readonly pluralizeVariants: ReadonlyArray<TuiPluralize | Record<string, string>> = [
['year', 'years', 'years'],
{one: 'thing', few: 'things', many: 'things', other: 'things'},
{
one: 'year',
other: 'years',
},
];

pluralize: Record<string, string> | TuiPluralize | null = null;

readonly keyStepsVariants: readonly TuiKeySteps[] = [[[50, 1000]]];

keySteps: TuiKeySteps | null = null;
Expand Down
48 changes: 0 additions & 48 deletions projects/demo/src/modules/components/range/range.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@
<ng-template pageTab>
<p i18n>Component allows to choose a part of a range</p>

<tui-notification status="warning">
<p
i18n
class="tui-space_top-0"
>
This component is being refactored. Soon&nbsp;(next&nbsp;major&nbsp;release) you will see the fresh
version of it!
</p>
<p i18n>
Of course, we keep backward compatibility in mind (for 2.x.x). You can still use old version of
<code>Range</code>
.
</p>

<p i18n>
However, if you are going to use this component, we recommend to use new version. To enable the "new
version"-mode, add
<code>new</code>
directive. Example:&nbsp;
<code>&lt;tui&#8209;range&nbsp;new&nbsp;...&gt;</code>
</p>
</tui-notification>

<tui-doc-example
id="base"
i18n-heading
Expand Down Expand Up @@ -92,7 +69,6 @@
[max]="max"
[steps]="steps"
[segments]="segments"
[pluralize]="pluralize"
[keySteps]="keySteps"
[size]="size"
[quantum]="quantum"
Expand Down Expand Up @@ -177,30 +153,6 @@
>
Size
</ng-template>
<ng-template
i18n
documentationPropertyName="pluralize"
documentationPropertyMode="input"
documentationPropertyType="TuiPluralize | Record<string, string>"
[documentationPropertyDeprecated]="true"
[documentationPropertyValues]="pluralizeVariants"
[(documentationPropertyValue)]="pluralize"
>
Plural forms for labels. TuiPluralize array is deprecated. Use object that mimics the
<a
tuiLink
href="https://unicode-org.github.io/icu/userguide/format_parse/messages/"
>
ICU format
</a>
for i18nPlural

<p>
<strong>
See examples how create labels for ticks without this property (outside of the component).
</strong>
</p>
</ng-template>
</tui-doc-documentation>
</ng-template>

Expand Down
57 changes: 29 additions & 28 deletions projects/kit/components/range/range.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ import {Observable} from 'rxjs';
* @deprecated TODO remove me in v3.0 and make `Range` and `InputRange` always "new".
*/
@Directive({
selector: `tui-range[new], tui-input-range[new]`,
selector: 'tui-range[new], tui-input-range[new]',
})
export class TuiNewRangeDirective {}

// @dynamic
@Component({
selector: `tui-range`,
templateUrl: `./range.template.html`,
styleUrls: [`./range.style.less`],
selector: 'tui-range',
templateUrl: './range.template.html',
styleUrls: ['./range.style.less'],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'[attr.tabindex]': `-1`,
'[attr.aria-disabled]': `computedDisabled`,
'[attr.tabindex]': '-1',
'[attr.aria-disabled]': 'computedDisabled',
},
providers: [
{
Expand Down Expand Up @@ -102,17 +102,17 @@ export class TuiRangeComponent
* It can be done after removing backward compatibility code inside {@link computePureKeySteps} in v3.0
* */
@Input()
@tuiDefaultProp(nonNegativeFiniteAssertion, `Quantum must be a non-negative number`)
@tuiDefaultProp(nonNegativeFiniteAssertion, 'Quantum must be a non-negative number')
quantum = 0;

@Input()
@HostBinding(`attr.data-size`)
@HostBinding('attr.data-size')
@tuiDefaultProp()
size: TuiSizeS = `m`;
size: TuiSizeS = 'm';

@Input()
@tuiDefaultProp()
segments = 0;
segments = 1;

@Input()
@tuiDefaultProp()
Expand All @@ -121,7 +121,7 @@ export class TuiRangeComponent
@ViewChildren(TuiSliderComponent, {read: ElementRef})
slidersRefs: QueryList<ElementRef<HTMLInputElement>> = EMPTY_QUERY;

lastActiveThumb: 'right' | 'left' = `right`;
lastActiveThumb: 'right' | 'left' = 'right';

constructor(
@Optional()
Expand All @@ -132,9 +132,6 @@ export class TuiRangeComponent
@Inject(DOCUMENT) documentRef: Document,
@Inject(ElementRef) private readonly elementRef: ElementRef<HTMLElement>,
@Inject(TUI_FROM_TO_TEXTS) fromToTexts$: Observable<[string, string]>,
@Optional()
@Inject(TuiNewRangeDirective)
readonly isNew: TuiNewRangeDirective | null,
) {
super(control, changeDetectorRef, documentRef, fromToTexts$);
}
Expand Down Expand Up @@ -167,34 +164,38 @@ export class TuiRangeComponent
return this.computePureKeySteps(this.keySteps, this.min, this.max);
}

@HostBinding(`style.--left.%`)
get segmentWidthRatio(): number {
return 1 / this.segments;
}

@HostBinding('style.--left.%')
get left(): number {
return this.getPercentageFromValue(this.value[0]);
}

@HostBinding(`style.--right.%`)
@HostBinding('style.--right.%')
get right(): number {
return 100 - this.getPercentageFromValue(this.value[1]);
}

@HostListener(`focusin`, [`true`])
@HostListener(`focusout`, [`false`])
@HostListener('focusin', ['true'])
@HostListener('focusout', ['false'])
onFocused(focused: boolean): void {
this.updateFocused(focused);
}

@HostListener(`keydown.arrowUp.prevent`, [`1`, `$event.target`])
@HostListener(`keydown.arrowRight.prevent`, [`1`, `$event.target`])
@HostListener(`keydown.arrowLeft.prevent`, [`-1`, `$event.target`])
@HostListener(`keydown.arrowDown.prevent`, [`-1`, `$event.target`])
@HostListener('keydown.arrowUp.prevent', ['1', '$event.target'])
@HostListener('keydown.arrowRight.prevent', ['1', '$event.target'])
@HostListener('keydown.arrowLeft.prevent', ['-1', '$event.target'])
@HostListener('keydown.arrowDown.prevent', ['-1', '$event.target'])
changeByStep(coefficient: number, target: HTMLElement): void {
const [sliderLeftRef, sliderRightRef] = this.slidersRefs;
const leftThumbElement = sliderLeftRef.nativeElement;
const rightThumbElement = sliderRightRef.nativeElement;

const isRightThumb =
target === this.elementRef.nativeElement
? this.lastActiveThumb === `right`
? this.lastActiveThumb === 'right'
: target === rightThumbElement;
const activeThumbElement = isRightThumb ? rightThumbElement : leftThumbElement;
const previousValue = isRightThumb ? this.value[1] : this.value[0];
Expand All @@ -218,7 +219,7 @@ export class TuiRangeComponent
this.updateStart(guardedValue);
}

this.lastActiveThumb = right ? `right` : `left`;
this.lastActiveThumb = right ? 'right' : 'left';
}

fractionGuard(fraction: number): number {
Expand Down Expand Up @@ -265,10 +266,10 @@ export class TuiRangeComponent
// TODO replace all function by `return keySteps || [[0, min], [100, max]]` in v3.0
tuiAssert.assert(
!keySteps,
`\n` +
`Input property [keySteps] should contain min and max percents.\n` +
`We have taken [min] and [max] properties of your component for now (but it will not work in v3.0).\n` +
`See example how properly use [keySteps]: https://taiga-ui.dev/components/range#key-steps`,
'\n' +
'Input property [keySteps] should contain min and max percents.\n' +
'We have taken [min] and [max] properties of your component for now (but it will not work in v3.0).\n' +
'See example how properly use [keySteps]: https://taiga-ui.dev/components/range#key-steps',
);

return [[0, min], ...(keySteps || []), [100, max]];
Expand Down
12 changes: 2 additions & 10 deletions projects/kit/components/range/range.module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {TuiFocusableModule, TuiRepeatTimesModule} from '@taiga-ui/cdk';
import {TuiFormatNumberPipeModule} from '@taiga-ui/core';
import {TuiFocusableModule} from '@taiga-ui/cdk';
import {TuiSliderModule} from '@taiga-ui/kit/components/slider';

import {TuiNewRangeDirective, TuiRangeComponent} from './range.component';
import {TuiRangeChangeDirective} from './range-change.directive';

@NgModule({
imports: [
CommonModule,
TuiRepeatTimesModule,
TuiFocusableModule,
TuiFormatNumberPipeModule,
TuiSliderModule,
FormsModule,
],
imports: [CommonModule, TuiFocusableModule, TuiSliderModule, FormsModule],
declarations: [TuiRangeComponent, TuiRangeChangeDirective, TuiNewRangeDirective],
exports: [TuiRangeComponent, TuiRangeChangeDirective, TuiNewRangeDirective],
})
Expand Down
63 changes: 18 additions & 45 deletions projects/kit/components/range/range.style.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

@track-height: 0.125rem;
@extra-click-area-space: 0.4375rem;
@tick-thickness: 0.25rem;

:host {
position: relative;
Expand Down Expand Up @@ -38,7 +39,7 @@
height: 100%;

/* Filled selected range */
&:after {
&:before {
content: '';
position: absolute;
top: 0;
Expand All @@ -48,6 +49,22 @@
background: var(--tui-primary);
margin: 0 (-@thumb-width / 2);
}

/* Ticks */
&:after {
.fullsize(absolute, inset);
content: '';
left: ((@thumb-width - @tick-thickness) / 2);
right: ((@thumb-width + @tick-thickness) / 2);
background-image: repeating-linear-gradient(
to right,
var(--tui-base-07) 0 @tick-thickness,
transparent 0 calc(var(--segment-width) / var(--bg-size-ratio))
);
background-position-x: right;
background-repeat: no-repeat;
background-size: calc(100% * var(--bg-size-ratio));
}
}

.track {
Expand Down Expand Up @@ -128,47 +145,3 @@
opacity: 1; // prevent double overlay for disabled state
}
}

// TODO delete in v3.0
.segments {
.fullsize(absolute, inset);

:host[data-size='s'] & {
.tui-slider-ticks-labels(s);
padding: 0 (@thumb-diameters[ @s] / 2);
}

:host[data-size='m'] & {
.tui-slider-ticks-labels(m);
padding: 0 (@thumb-diameters[ @m] / 2);
}
}

// TODO Use background-image with repeating-linear-gradient to make ticks
.segment:not(:last-of-type):not(:first-of-type):before {
content: '';
position: absolute;
left: 0;
right: 0;
margin: auto;
background: var(--tui-base-07);
width: 0.25rem;
height: 100%;
}

// TODO delete in v3.0
.segment {
&:last-of-type .number {
margin-right: (-@thumb-diameters[ @m] / 2);
}

&:first-of-type .number {
margin-left: (-@thumb-diameters[ @m] / 2);
}
}

// TODO delete in v3.0
.number {
display: inline-block;
margin-top: 0.5rem;
}
Loading

0 comments on commit 46dd750

Please sign in to comment.