@@ -3,9 +3,11 @@ import {
33 ChangeDetectionStrategy ,
44 ChangeDetectorRef ,
55 Component ,
6+ EventEmitter ,
67 forwardRef ,
78 inject ,
89 Input ,
10+ Output ,
911 ViewEncapsulation ,
1012} from '@angular/core' ;
1113import { ControlValueAccessor , FormsModule , NG_VALUE_ACCESSOR } from '@angular/forms' ;
@@ -45,8 +47,6 @@ import { parseGradient, stringifyGradient } from './utils';
4547export class GradientPicker implements ControlValueAccessor {
4648 private cdr = inject ( ChangeDetectorRef ) ;
4749
48- @Input ( { transform : booleanAttribute } ) disabled = false ;
49-
5050 types = [
5151 { label : 'Linear' , value : 'linear' } ,
5252 { label : 'Radial' , value : 'radial' } ,
@@ -61,25 +61,36 @@ export class GradientPicker implements ControlValueAccessor {
6161 conic : 'conic-gradient(transparent, #000000)' ,
6262 } ;
6363
64- value = this . gradient [ this . type ] ;
64+ @Input ( )
65+ get value ( ) {
66+ return this . _value ;
67+ }
68+ set value ( newVal : string ) {
69+ if ( newVal !== this . _value ) {
70+ if ( ! newVal ) {
71+ this . type = 'linear' ;
72+ } else if ( newVal . includes ( 'linear' ) ) {
73+ this . type = 'linear' ;
74+ } else if ( newVal . includes ( 'radial' ) ) {
75+ this . type = 'radial' ;
76+ } else if ( newVal . includes ( 'conic' ) ) {
77+ this . type = 'conic' ;
78+ }
79+ this . _value = this . gradient [ this . type ] = newVal || 'linear-gradient(transparent, #000000)' ;
80+ this . cdr . markForCheck ( ) ;
81+ }
82+ }
83+ private _value = this . gradient [ this . type ] ;
84+
85+ @Output ( ) valueChange = new EventEmitter < string > ( ) ;
86+
87+ @Input ( { transform : booleanAttribute } ) disabled = false ;
6588
6689 private onChange : ( value : string ) => void = ( ) => { } ;
6790 private onTouched : ( ) => void = ( ) => { } ;
6891
6992 writeValue ( value : any ) : void {
70- if ( ! value ) {
71- this . type = 'linear' ;
72- } else if ( value . includes ( 'linear' ) ) {
73- this . type = 'linear' ;
74- } else if ( value . includes ( 'radial' ) ) {
75- this . type = 'radial' ;
76- } else if ( value . includes ( 'conic' ) ) {
77- this . type = 'conic' ;
78- }
79- if ( value ) {
80- this . value = this . gradient [ this . type ] = value ;
81- }
82- this . cdr . markForCheck ( ) ;
93+ this . value = value ;
8394 }
8495
8596 registerOnChange ( fn : ( value : string ) => void ) : void {
@@ -96,8 +107,9 @@ export class GradientPicker implements ControlValueAccessor {
96107 }
97108
98109 onValueChange ( ) {
99- this . value = this . gradient [ this . type ] ;
100- this . onChange ( this . value ) ;
110+ this . _value = this . gradient [ this . type ] ;
111+ this . valueChange . emit ( this . _value ) ;
112+ this . onChange ( this . _value ) ;
101113 }
102114
103115 onTypeChange ( ) {
0 commit comments