editing-customizations.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package basics
  2. import (
  3. "github.com/qor5/docs/docsrc/examples/e21_presents"
  4. "github.com/qor5/docs/docsrc/generated"
  5. "github.com/qor5/docs/docsrc/utils"
  6. . "github.com/theplant/docgo"
  7. "github.com/theplant/docgo/ch"
  8. . "github.com/theplant/htmlgo"
  9. )
  10. var EditingCustomizations = Doc(
  11. Markdown(`
  12. Editing an object will be always in a drawer popup. select which fields can edit for each model
  13. by using the ~.Only~ func of ~EditingBuilder~, There are different ways to configure the type
  14. of component that is used to do the editing.
  15. `),
  16. utils.Anchor(H2(""), "Configure field for a single model"),
  17. Markdown(`
  18. Use a customized component is as simple as add the extra asset to the preset instance.
  19. And configure the component func on the field:
  20. `),
  21. ch.Code(generated.PresetsEditingCustomizationDescriptionSample).Language("go"),
  22. utils.Demo("Presets Editing Customization Description Field", e21_presents.PresetsEditingCustomizationDescriptionPath+"/customers", "e21_presents/editing.go"),
  23. Markdown(`
  24. - Added the tiptap javascript and css component pack as an extra asset
  25. - Configure the description field to use the component func that returns the ~tiptap.TipTapEditor()~ component
  26. - Set the field name and value of the component
  27. `),
  28. utils.Anchor(H2(""), "Configure field type for all models"),
  29. Markdown(`
  30. Set a global field type to component func like the following:
  31. `),
  32. ch.Code(generated.PresetsEditingCustomizationFileTypeSample).Language("go"),
  33. utils.Demo("Presets Editing Customization File Type", e21_presents.PresetsEditingCustomizationFileTypePath+"/products", "e21_presents/editing.go"),
  34. Markdown(`
  35. - We define ~MyFile~ to actually be a string
  36. - We set ~FieldDefaults~ for writing, which is the editing drawer popup to be a customized component
  37. - The component show an img tag with the string as src if it's not empty
  38. - The component add a file input for user to upload new file
  39. - The ~SetterFunc~ is called before save the object, it uploads the file to transfer.sh, and get the url back,
  40. then set the value to ~MainImage~ field
  41. With ~FieldDefaults~ we can write libraries that add customized type for different models to reuse. It can take care
  42. of how to display the edit controls, and How to save the object.
  43. `),
  44. utils.Anchor(H2(""), "Validation"),
  45. Markdown(`
  46. Field level validation and display on field can be added by implement ~ValidateFunc~,
  47. and set the ~web.ValidationErrors~ result:
  48. `),
  49. ch.Code(generated.PresetsEditingCustomizationValidationSample).Language("go"),
  50. utils.Demo("Presets Editing Customization Validation", e21_presents.PresetsEditingCustomizationValidationPath+"/customers", "e21_presents/editing.go"),
  51. Markdown(`
  52. - We validate the ~Name~ of the customer must be longer than 10
  53. - If the error happens, If will show below the field
  54. `),
  55. ).Title("Editing").
  56. Slug("presets-guide/editing-customizations")