Ejemplo n.º 1
0
 /// <summary>
 /// 失去焦点时触发此方法
 /// </summary>
 protected virtual void OnBlur()
 {
     if (FieldIdentifier != null)
     {
         EditContext?.NotifyFieldChanged(FieldIdentifier.Value);
     }
 }
Ejemplo n.º 2
0
        async Task Toggle()
        {
            if (Disabled)
            {
                return;
            }

            if (object.Equals(Value, false))
            {
                if (TriState)
                {
                    Value = default;
                }
                else
                {
                    Value = (TValue)(object)true;
                }
            }
            else if (Value == null)
            {
                Value = (TValue)(object)true;
            }
            else if (object.Equals(Value, true))
            {
                Value = (TValue)(object)false;
            }

            await ValueChanged.InvokeAsync(Value);

            if (FieldIdentifier.FieldName != null)
            {
                EditContext?.NotifyFieldChanged(FieldIdentifier);
            }
            await Change.InvokeAsync(Value);
        }
        async System.Threading.Tasks.Task Clear()
        {
            if (Disabled)
            {
                return;
            }

            searchText    = null;
            internalValue = default(TValue);
            selectedItem  = null;

            selectedItems.Clear();

            await ValueChanged.InvokeAsync((TValue)internalValue);

            if (FieldIdentifier.FieldName != null)
            {
                EditContext?.NotifyFieldChanged(FieldIdentifier);
            }
            await Change.InvokeAsync(internalValue);

            await grid.Reload();

            StateHasChanged();
        }
        private static void NotifyPropertyChanged(
            EditContext editContext,
            object instance,
            string propertyName)
        {
            if (GetFieldStateMethod == null)
            {
                GetFieldStateMethod = editContext.GetType().GetMethod(
                    "GetFieldState",
                    BindingFlags.NonPublic | BindingFlags.Instance);
            }

            var    fieldIdentifier = new FieldIdentifier(instance, propertyName);
            object fieldState      = GetFieldStateMethod.Invoke(editContext, new object[] { fieldIdentifier });

            if (IsModifiedProperty == null && fieldState != null)
            {
                IsModifiedProperty = fieldState.GetType().GetProperty(
                    "IsModified",
                    BindingFlags.Public | BindingFlags.Instance);

                object originalIsModified = IsModifiedProperty?.GetValue(fieldState);
                editContext.NotifyFieldChanged(fieldIdentifier);
                IsModifiedProperty?.SetValue(fieldState, originalIsModified);
            }
        }
Ejemplo n.º 5
0
        public void Copy(object savedFormModel, object currentFormModel)
        {
            var savedFormModelProperties   = savedFormModel.GetType().GetProperties();
            var currentFormModelProperties = currentFormModel.GetType().GetProperties();

            foreach (var savedFormModelProperty in savedFormModelProperties)
            {
                foreach (var currentFormModelProperty in currentFormModelProperties)
                {
                    if (savedFormModelProperty.Name == currentFormModelProperty.Name && savedFormModelProperty.PropertyType == currentFormModelProperty.PropertyType)
                    {
                        var childValue  = currentFormModelProperty.GetValue(currentFormModel);
                        var parentValue = savedFormModelProperty.GetValue(savedFormModel);

                        if (childValue == null && parentValue == null)
                        {
                            continue;
                        }

                        currentFormModelProperty.SetValue(currentFormModel, parentValue);

                        var fieldIdentifier = new FieldIdentifier(currentFormModel, currentFormModelProperty.Name);
                        _fixedEditContext.NotifyFieldChanged(fieldIdentifier);

                        break;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        internal void NotifyInputChanged(object newExpressionValue = null, bool overrideNewValue = false)
        {
            var newValidationValue = overrideNewValue
                ? newExpressionValue
                : inputComponent.ValidationValue;

            var valueChanged = newValidationValue is Array newArrayValue
                ? !Comparers.AreArraysEqual(lastValidationValue as Array, newArrayValue)
                : lastValidationValue != newValidationValue;

            if (valueChanged)
            {
                lastValidationValue = newValidationValue;

                if (EditContext != null && hasFieldIdentifier)
                {
                    EditContext.NotifyFieldChanged(fieldIdentifier);
                }

                if (Mode == ValidationMode.Auto)
                {
                    Validate(newValidationValue);
                }
            }
        }
        protected async Task HandleToChangedAsync(ChangeEventArgs changeEventArgs)
        {
            bool parsingFailed;

            validationMessageStore.Clear(toFieldIdentifier);

            if (HxInputDate <DateTime> .TryParseDateTimeOffsetFromString((string)changeEventArgs.Value, null, out var toDate))
            {
                parsingFailed = false;
                CurrentValue  = Value with {
                    EndDate = toDate?.DateTime
                };
                EditContext.NotifyFieldChanged(toFieldIdentifier);
                await CloseDropDownAsync(toInputElement);
            }
            else
            {
                parsingFailed = true;
                validationMessageStore.Add(toFieldIdentifier, ToParsingErrorMessageEffective);
            }

            // We can skip the validation notification if we were previously valid and still are
            if (parsingFailed || toPreviousParsingAttemptFailed)
            {
                EditContext.NotifyValidationStateChanged();
                toPreviousParsingAttemptFailed = parsingFailed;
            }
        }
Ejemplo n.º 8
0
        async System.Threading.Tasks.Task UpdateValueWithStep(bool stepUp)
        {
            if (Disabled || ReadOnly)
            {
                return;
            }

            var step = string.IsNullOrEmpty(Step) || Step == "any" ? 1 : double.Parse(Step.Replace(",", "."), System.Globalization.CultureInfo.InvariantCulture);

            var valueToUpdate = Value != null?Convert.ChangeType(Value, typeof(decimal)) : (decimal)Convert.ChangeType(default(decimal), typeof(decimal));

            var newValue = ((decimal)Convert.ChangeType(valueToUpdate, typeof(decimal))) + (decimal)Convert.ChangeType(stepUp ? step : -step, typeof(decimal));

            if (Max.HasValue && newValue > Max.Value || Min.HasValue && newValue < Min.Value || object.Equals(Value, newValue))
            {
                return;
            }

            Value = (TValue)ConvertType.ChangeType(newValue, typeof(TValue));

            await ValueChanged.InvokeAsync(Value);

            if (FieldIdentifier.FieldName != null)
            {
                EditContext?.NotifyFieldChanged(FieldIdentifier);
            }
            await Change.InvokeAsync(Value);

            StateHasChanged();
        }
        async Task OnChange()
        {
            string uploadValue;

            try
            {
                uploadValue = await JSRuntime.InvokeAsync <string>("Radzen.readFileAsBase64", fileUpload, MaxFileSize);

                if (typeof(TValue) == typeof(byte[]))
                {
                    Value = (TValue)(object)System.Text.Encoding.Default.GetBytes($"{uploadValue}");
                }
                else if (typeof(TValue) == typeof(string))
                {
                    Value = (TValue)(object)uploadValue;
                }

                await ValueChanged.InvokeAsync(Value);

                if (FieldIdentifier.FieldName != null)
                {
                    EditContext?.NotifyFieldChanged(FieldIdentifier);
                }
                await Change.InvokeAsync(Value);

                StateHasChanged();
            }
            catch (Exception ex)
            {
                await Error.InvokeAsync(new UploadErrorEventArgs()
                {
                    Message = $"Unable to read file as base64 string. {ex.Message}"
                });
            }
        }
Ejemplo n.º 10
0
 protected virtual Task OnInputChanged(ChangeEventArgs e)
 {
     CurrentValue = e.Value as string;
     SetValue(CurrentValue);
     EditContext.NotifyFieldChanged(FieldIdentifier);
     return(Filter());
 }
Ejemplo n.º 11
0
        protected async System.Threading.Tasks.Task ClearAll()
        {
            if (Disabled)
            {
                return;
            }

            searchText = null;
            await JSRuntime.InvokeAsync <string>("Radzen.setInputValue", search, "");

            Value        = default(T);
            selectedItem = null;

            selectedItems.Clear();

            selectedIndex = -1;

            await ValueChanged.InvokeAsync((T)Value);

            if (FieldIdentifier.FieldName != null)
            {
                EditContext?.NotifyFieldChanged(FieldIdentifier);
            }
            await Change.InvokeAsync(Value);

            await OnFilter(new ChangeEventArgs());

            StateHasChanged();
        }
Ejemplo n.º 12
0
        protected void OnInput(ChangeEventArgs args, int index = 0)
        {
            if (args == null)
            {
                return;
            }
            var array = Value as Array;

            if (!_duringManualInput)
            {
                _duringManualInput = true;
                _cacheDuringInput  = array.GetValue(index) as DateTime?;
                _pickerValueCache  = PickerValues[index];
            }
            if (FormatAnalyzer.TryPickerStringConvert(args.Value.ToString(), out DateTime changeValue, false))
            {
                array.SetValue(changeValue, index);

                ChangePickerValue(changeValue, index);

                if (_isNotifyFieldChanged && (Form?.ValidateOnChange == true))
                {
                    EditContext?.NotifyFieldChanged(FieldIdentifier);
                }

                StateHasChanged();
            }
        }
Ejemplo n.º 13
0
 protected void OnChangeEvent(string?e)
 {
     CurrentValueAsString = e;
     if (ValidateOnInput && EditContext != null)
     {
         EditContext.NotifyFieldChanged(FieldIdentifier);
     }
 }
Ejemplo n.º 14
0
 private void EditContext_OnFieldChanged(object sender, FieldChangedEventArgs e)
 {
     Console.WriteLine($"EditContext_OnFieldChanged - {e.FieldIdentifier.FieldName}");
     if (e.FieldIdentifier.FieldName == nameof(Model.Date))
     {
         EditContext.NotifyFieldChanged(EditContext.Field(nameof(Model.Summary)));
     }
 }
Ejemplo n.º 15
0
    private async Task OnGatewaySelected(Gateway?gateway)
    {
        Value = gateway;
        await ValueChanged.InvokeAsync(gateway);

        EditContext.NotifyFieldChanged(FieldIdentifier);
        StateHasChanged();
    }
Ejemplo n.º 16
0
 /// <summary>
 /// Triggers field to be validated.
 /// </summary>
 internal void EditFormValidate()
 {
     if (_fieldIdentifier.FieldName == null)
     {
         return;
     }
     EditContext?.NotifyFieldChanged(_fieldIdentifier);
 }
Ejemplo n.º 17
0
        private void OnBlur()
        {
            EditContext.NotifyFieldChanged(FieldIdentifier);

            IsValid = !HasValidationMessages;
            OnAfterValidate();
            StateHasChanged();
        }
Ejemplo n.º 18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="editContext"></param>
        /// <param name="field">The field name that was changed in the process, typically specified using "nameof(YourObject.YourProperty").</param>
        public static void NotifyFieldChanged(this EditContext editContext, string field)
        {
            if (editContext == null || string.IsNullOrWhiteSpace(field))
            {
                return;
            }

            editContext.NotifyFieldChanged(editContext.Field(field));
        }
Ejemplo n.º 19
0
    public void IgnoresFieldChangesThatDoNotCorrespondToAValidatableProperty(string fieldName)
    {
        // Arrange
        var editContext = new EditContext(new TestModel());

        editContext.EnableDataAnnotationsValidation();
        var onValidationStateChangedCount = 0;

        editContext.OnValidationStateChanged += (sender, eventArgs) => onValidationStateChangedCount++;

        // Act/Assert: Ignores field changes that don't correspond to a validatable property
        editContext.NotifyFieldChanged(editContext.Field(fieldName));
        Assert.Equal(0, onValidationStateChangedCount);

        // Act/Assert: For sanity, observe that we would have validated if it was a validatable property
        editContext.NotifyFieldChanged(editContext.Field(nameof(TestModel.RequiredString)));
        Assert.Equal(1, onValidationStateChangedCount);
    }
        protected void HandleDateRangeClick(DateTimeRange value)
        {
            CurrentValue = value;
            EditContext.NotifyFieldChanged(fromFieldIdentifier);
            EditContext.NotifyFieldChanged(toFieldIdentifier);

            ClearPreviousParsingMessage(ref fromPreviousParsingAttemptFailed, fromFieldIdentifier);
            ClearPreviousParsingMessage(ref toPreviousParsingAttemptFailed, toFieldIdentifier);
        }
    public void TracksFieldsAsModifiedWhenValueChanged()
    {
        // Arrange
        var editContext       = new EditContext(new object());
        var fieldOnThisModel1 = editContext.Field("field1");
        var fieldOnThisModel2 = editContext.Field("field2");
        var fieldOnOtherModel = new FieldIdentifier(new object(), "field on other model");

        // Act
        editContext.NotifyFieldChanged(fieldOnThisModel1);
        editContext.NotifyFieldChanged(fieldOnOtherModel);

        // Assert
        Assert.True(editContext.IsModified());
        Assert.True(editContext.IsModified(fieldOnThisModel1));
        Assert.False(editContext.IsModified(fieldOnThisModel2));
        Assert.True(editContext.IsModified(fieldOnOtherModel));
    }
    public void CanClearAllModifications()
    {
        // Arrange
        var editContext = new EditContext(new object());
        var field1      = editContext.Field("field1");
        var field2      = editContext.Field("field2");

        editContext.NotifyFieldChanged(field1);
        editContext.NotifyFieldChanged(field2);

        // Act
        editContext.MarkAsUnmodified();

        // Assert
        Assert.False(editContext.IsModified());
        Assert.False(editContext.IsModified(field1));
        Assert.False(editContext.IsModified(field2));
    }
        private async Task HandleToCalendarValueChanged(DateTime?date)
        {
            CurrentValue = Value with {
                EndDate = date
            };
            EditContext.NotifyFieldChanged(toFieldIdentifier);
            ClearPreviousParsingMessage(ref toPreviousParsingAttemptFailed, toFieldIdentifier);

            await CloseDropDownAsync(toInputElement);
        }
        private async Task HandleFromClearClickAsync()
        {
            CurrentValue = Value with {
                StartDate = null
            };
            EditContext.NotifyFieldChanged(fromFieldIdentifier);
            ClearPreviousParsingMessage(ref fromPreviousParsingAttemptFailed, fromFieldIdentifier);

            await CloseDropDownAsync(fromInputElement);
        }
        private void OnBlur()
        {
            CurrentValueAsString = _editingValue;

            EditContext.NotifyFieldChanged(FieldIdentifier);

            IsValid = !HasValidationMessages;
            OnAfterValidate();
            StateHasChanged();
        }
Ejemplo n.º 26
0
        protected async Task HandleInput(ChangeEventArgs args)
        {
            var value = args.Value.ToString();

            value = ApplyMask(value);

            await ValueChanged.InvokeAsync(value);

            EditContext?.NotifyFieldChanged(_fieldIdentifier);
        }
        /// <summary>
        /// Parses the date.
        /// </summary>
        protected async Task ParseDate()
        {
            DateTime?newValue;
            DateTime value;
            var      inputValue = await JSRuntime.InvokeAsync <string>("Radzen.getInputValue", input);

            var valid    = DateTime.TryParseExact(inputValue, DateFormat, null, DateTimeStyles.None, out value);
            var nullable = Nullable.GetUnderlyingType(typeof(TValue)) != null;

            if (!valid)
            {
                valid = DateTime.TryParse(inputValue, out value);
            }

            if (valid && !DateAttributes(value).Disabled)
            {
                newValue = value;
            }
            else
            {
                newValue = null;

                if (nullable)
                {
                    await JSRuntime.InvokeAsync <string>("Radzen.setInputValue", input, "");
                }
                else
                {
                    await JSRuntime.InvokeAsync <string>("Radzen.setInputValue", input, FormattedValue);
                }
            }

            if (DateTimeValue != newValue && (newValue != null || nullable))
            {
                DateTimeValue = newValue;
                if ((typeof(TValue) == typeof(DateTimeOffset) || typeof(TValue) == typeof(DateTimeOffset?)) && Value != null)
                {
                    DateTimeOffset?offset = DateTime.SpecifyKind((DateTime)Value, DateTimeKind.Utc);
                    await ValueChanged.InvokeAsync((TValue)(object)offset);
                }
                else
                {
                    await ValueChanged.InvokeAsync((TValue)Value);
                }

                if (FieldIdentifier.FieldName != null)
                {
                    EditContext?.NotifyFieldChanged(FieldIdentifier);
                }

                await Change.InvokeAsync(DateTimeValue);

                StateHasChanged();
            }
        }
        private async Task HandleFromCalendarValueChangedAsync(DateTime?date)
        {
            CurrentValue = Value with {
                StartDate = date
            };
            EditContext.NotifyFieldChanged(fromFieldIdentifier);
            ClearPreviousParsingMessage(ref fromPreviousParsingAttemptFailed, fromFieldIdentifier);

            await CloseDropDownAsync(fromInputElement);
            await OpenDropDownAsync(toInputElement);
        }
Ejemplo n.º 29
0
 protected void OnBlurEvent(FocusEventArgs?e)
 {
     if (ValidateOnBlur && EditContext != null)
     {
         EditContext.NotifyFieldChanged(FieldIdentifier);
     }
     if (OnBlur.HasDelegate)
     {
         OnBlur.InvokeAsync(e);
     }
 }
        public async Task Increment()
        {
            CurrentValue++;
            await ValueChanged.InvokeAsync(Value);

            EditContext.NotifyFieldChanged(FieldIdentifier.Create(ValueExpression));
            if (ValueUpdated.HasDelegate)
            {
                ValueUpdated.InvokeAsync();
            }
        }