Skip to content

feat: add the edit form model and form edit mode to Core (#364) - #509

Open
michaelstonis wants to merge 5 commits into
mainfrom
feat/364-form-edit-mode
Open

michaelstonis wants to merge 5 commits into
mainfrom
feat/364-form-edit-mode

Conversation

@michaelstonis

Copy link
Copy Markdown
Contributor

Closes #364.

Changes

A form edit mode: the grid can edit, or add, a record in a host-native form built from its columns instead of cell by cell. The rules for the form live in Core's editing layer, so the MAUI and Uno hosts share them and only build the native views.

Core (KumikoUI.Core.Editing)

  • KumikoEditMode { InCell, Form } and EditSession.EditMode. In form mode, EditSession.BeginEdit opens no cell editor. It raises EditSession.EditFormRequested (KumikoEditFormRequestedEventArgs: RowIndex, ColumnIndex, Column, Item, DataSource) and returns false. Every existing trigger already goes through BeginEdit (double tap, Enter, F2, typing, the row menu's Edit), so KumikoInputController is unchanged. Boolean cells still toggle in place. Switching to form mode commits an open cell edit.

  • EditSession.CreateEditForm(source, item, isNewItem) returns a KumikoEditForm with these members: Item, IsNewItem, DataSource, Fields, GetField(name) (also exposed as an indexer, [Name]), Title, FirstEditableField, IsOpen, Error, HasErrors, TryCommit() / TryCommit(out invalidFields), Cancel(), SaveCommand, CancelCommand, Closed and PropertyChanged.

  • KumikoEditFormField (INotifyPropertyChanged) has Column, Label, Kind (KumikoEditFormFieldKind: Text, Numeric, Boolean, Date, DateTime, Time, Choice), IsReadOnly, IsThreeState, MinDate/MaxDate, Use24HourTime, Choices (KumikoEditFormChoice: Label, Value), OriginalValue, the pending Value, Text, SelectedChoiceIndex, IsDirty, Error and HasError.

  • How fields are built: one field per visible column, in on-screen order. Image, progress bar, selection and action-button (interactive renderer) columns are skipped, and so are columns with nothing to read or write. Template columns are edited as text. A field is read-only when its column is read-only, when the grid is read-only, when the property has no setter, or when a time column holds a duration.

  • What TryCommit does:

    1. Converts each changed field with the same conversion as an in-cell commit (KumikoValueConverter, in the grid's culture). A date edited alone keeps its time of day, and a time keeps the date of a DateTime.
    2. Runs CellValidator with the item's view row (-1 for a new or hidden item). A warning is shown once and accepted on the second save, as in a cell.
    3. Writes every field in one BeginUpdate batch (one view rebuild), inside IEditableObject.BeginEdit/EndEdit when the item implements it.
    4. Runs the item's own validation (IDataErrorInfo/INotifyDataErrorInfo). On an error, every original value is written back and CancelEdit is called.
    5. Adds a new item to the items source only on success, through a new internal KumikoSource.TryAddItem.
    6. Raises CellValueChanged once per changed field, so apps that save on that event keep working.

    A refused commit sets Error on each refused field and writes nothing. Cancel writes nothing.

  • KumikoEditFormOpeningEventArgs (Form, Handled, Cancel) and KumikoEditFormClosedEventArgs (Form, Committed).

  • New KumikoStrings: EditFormTitle, EditFormNewTitle, EditFormSave, EditFormCancel, EditFormNoValue, EditFormChecked, EditFormUnchecked, EditFormCannotAdd, AnnounceEditFormSaved.

  • Small internal additions: KumikoValuePath.CanWrite(item). EditSession became partial, with the new members in EditSession.Form.cs. TargetType, ValidateItem and MoreSevere went from private to internal so the form can reuse them.

Hosts (same surface on MAUI and Uno)

  • New members: EditMode, EditFormTemplate (DataTemplate), EditFormOpening, EditFormClosed, ActiveEditForm, ShowEditForm(object item) and ShowNewItemForm(object newItem). Both Show methods work in either mode. They return false when the grid is read-only, when another form is open, when an open cell edit is refused, or when the opening event cancels.
  • MAUI: an internal KumikoEditFormPage is pushed modally with the grid's Navigation.
    • Editors: Entry (numeric keyboard for numbers), CheckBox, a three-way Picker for three-state Booleans, DatePicker, TimePicker and Picker.
    • Field messages show under the editors. Return in a text field saves, and the Android back button cancels.
  • Uno: an internal KumikoEditFormDialog (ContentDialog) opens in the grid's XamlRoot.
    • Editors: TextBox (number input scope for numbers), CheckBox (IsThreeState), CalendarDatePicker, TimePicker and ComboBox.
    • Save is the default button, so Enter saves. Escape or Close cancels.
  • Shared host behavior:
    • Accessibility: each editor carries its label (SemanticProperties.Description / AutomationProperties.Name). The first editable field takes focus on open, and focus returns to the grid on close. "Saved", or the first error, is announced.
    • Colors come from KumikoStyle.
    • EditFormTemplate replaces the generated fields; the title, form message and buttons stay. EditFormOpening.Handled replaces the whole UI.

Samples and docs

  • An Edit Form page in both sample apps: form mode toggle, "Edit selected", "Add person", an age validator and a read-only Id column.
  • README "Editing in a form" section, compiled by the snippet projects. CHANGELOG entry under Unreleased.

Acceptance criteria

Verification

  • dotnet build src/KumikoUI.Core: 0 warnings, 0 errors. A Release build passes eng/TypeNameCheck ("No public type name collides").
  • dotnet build src/KumikoUI.SkiaSharp: 0 warnings.
  • dotnet build src/KumikoUI.Maui -f net10.0-maccatalyst: 0 warnings.
  • dotnet build src/KumikoUI.Uno -f net10.0-desktop: no new warnings.
  • dotnet test tests/KumikoUI.Core.Tests: 4383 passed, 5 skipped. New: Editing/EditFormTests (22 tests) and the LocalizationTests table.
  • dotnet test tests/KumikoUI.Maui.Tests: 458 passed, including 9 new KumikoDataGridEditFormTests.
  • dotnet test tests/KumikoUI.Uno.Tests: 328 passed, including 7 new KumikoEditFormDialogTests.
  • dotnet test tests/KumikoUI.SkiaSharp.Tests: 42 passed.
  • dotnet build samples/SampleApp.Uno -f net10.0-desktop: succeeded.
  • dotnet build samples/SampleApp.Maui -f net10.0-maccatalyst -p:KumikoDevFlow=false: succeeded. With DevFlow on, this machine's restore fails with NU1605, a Microsoft.Maui.Controls version conflict. The conflict is the same without this change.
  • dotnet run --project eng/DocsSnippets -c Release, then dotnet build tests/KumikoUI.Docs.Snippets.Uno -f net10.0-desktop -c Release and dotnet build tests/KumikoUI.Docs.Snippets.Maui -f net10.0-maccatalyst -c Release: both succeeded.
  • The forms were not run interactively in the sample apps.

Comment on lines +358 to +369
foreach (var column in dataSource.DisplayColumns)
{
if (!IsFormColumn(column)) continue;
int columnIndex = IndexOf(columns, column);
var kind = KindOf(column);
var value = ReadValue(item, column);
bool readOnly = gridReadOnly || column.IsReadOnly || !CanWrite(item, column) ||
kind == KumikoEditFormFieldKind.Time && value is TimeSpan span && (span < TimeSpan.Zero || span >= TimeSpan.FromDays(1));
var choices = kind == KumikoEditFormFieldKind.Choice ? ChoicesOf(column) : Array.Empty<KumikoEditFormChoice>();
string label = string.IsNullOrEmpty(column.Header) ? column.PropertyName : column.Header;
fields.Add(new KumikoEditFormField(column, columnIndex, kind, label, readOnly, value, choices, culture));
}
Compute the sample join dates in double, drop the always-true pattern in
KumikoEditFormField.Text and make the hosts' first-editor fields readonly.
Accessor, setter and nested-path fields, choice selectors and member
paths, offset dates and times, setters that throw, sources that cannot
take a new record, form commands and notifications, and edit triggers on
rows that are not records. Brings branch coverage back over the ratchet.
public void ANewItem_ACopiedOrTypedSourceCannotTake_IsNotAdded()
{
var title = new KumikoColumn { Header = "Title", PropertyName = nameof(Meeting.Title) };
var copied = SourceOf(new[] { new Meeting() }.Select(m => m), title); // an IEnumerable: the grid keeps a copy

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Editing: add a form edit mode that edits or adds a record in a host-native form built from the columns

2 participants