Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form lib] Export internal state instead of raw state #80842

Merged
merged 20 commits into from
Oct 20, 2020

Conversation

sebelga
Copy link
Contributor

@sebelga sebelga commented Oct 16, 2020

Read the issue for context: #78589

This PR replaces the raw (flat object of value) state exposed to the consumer and replaces it with the form internal state (which default to the form "out" interface if no deserializer was provided).

interface MyForm {
  title: string;
  subTitle: string;
}

// Internally we might need to add some field for our UI
interface MyFormInternal extends MyForm {
  _meta: {
    showAdvancedSettings: boolean; // additional field on the form, only for the UI
  }
}

// We provide both interface to the useForm call
const { form } = useForm<MyForm, MyFormInternal>();

// And we can provide them to useFormData().
// We now receive the internal interface providedinstead of the raw flat object
const [formData, serializer] = useFormData<MyFormInternal, MyForm>({ form });

formData // MyFormInternal
const output = serializer(); // MyForm


// If we don't use (de)serializer, then we can pass the "out" interface
const [formData] = useFormData<MyForm>({ form });

Fixes #78589

cc @rylnd @patrykkopycinski

@sebelga sebelga added release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more v7.1.1 v8.0.0 labels Oct 16, 2020
@sebelga sebelga force-pushed the form-lib/remove-raw-state branch from b033959 to f5edadc Compare October 19, 2020 14:44
@sebelga sebelga force-pushed the form-lib/remove-raw-state branch from 48ed86c to 364855c Compare October 19, 2020 16:51
@sebelga sebelga requested a review from jloleysens October 19, 2020 16:51
@sebelga sebelga marked this pull request as ready for review October 19, 2020 16:51
@sebelga sebelga requested review from a team as code owners October 19, 2020 16:51
@elasticmachine
Copy link
Contributor

Pinging @elastic/es-ui (Team:Elasticsearch UI)

Copy link
Contributor

@patrykkopycinski patrykkopycinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks Amazing, Thank you @sebelga for all the hard work! 💪
SIEM/Endpoint LGTM! 🚀

Copy link
Contributor

@jloleysens jloleysens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @sebelga ! I tested proposed changes locally in ingest pipelines plugin and in index lifecycle management.

Left a couple of comments.

Comment on lines +293 to +294
? serializer(unflattenObject<I>(fieldsValue))
: unflattenObject<T>(fieldsValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the form lib, internally still stores paths as my.path[0].to.something? We just don't expose this to consumer code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I thought in changing that but when I started to write the logic to remove a field from an array (e.g. second position) from inside an object (at any depth) I thought... let's keep it flat until the last minute! 😄 It makes the internal logic much simpler.

if (resetValues) {
getFormData$().next(currentFormData as T);
updateFormData$(currentFormData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice update! RIP as!

Comment on lines +58 to +65
/**
* We do want to offer to the consumer a handler to serialize the form data that changes each time
* the formData **state** changes. This is why we added the "formData" dep to the array and added the eslint override.
*/
const serializer = useCallback(() => {
return getFormData();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [getFormData, formData]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Happy for the form lib to guarantee this for consumers

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

async chunks size

id before after diff
indexLifecycleManagement 268.7KB 269.0KB +332.0B
indexManagement 1.6MB 1.6MB +774.0B
ingestPipelines 813.0KB 813.1KB +20.0B
securitySolution 8.1MB 8.1MB +146.0B
total +1.2KB

page load bundle size

id before after diff
esUiShared 304.2KB 303.2KB -1016.0B
ingestPipelines 42.0KB 41.9KB -99.0B
total -1.1KB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@sebelga
Copy link
Contributor Author

sebelga commented Oct 20, 2020

Thanks for the review @jloleysens !

@sebelga sebelga merged commit 702e0c7 into elastic:master Oct 20, 2020
@sebelga sebelga deleted the form-lib/remove-raw-state branch October 20, 2020 11:51
gmmorris added a commit to gmmorris/kibana that referenced this pull request Oct 20, 2020
* master: (64 commits)
  Rename Security Solution Bug Template (elastic#81187)
  Update links (elastic#81125)
  Specify format for date range query (elastic#81025)
  [Alerting] Improve toast when alert is created (elastic#80327)
  [UX] Add empty states (elastic#80904)
  Add TS config for kibana_legacy (elastic#80992)
  [Telemetry] Add method to enable endpoint security data usage example (elastic#80940)
  [Alerting] Add scoped cluster client to alerts and actions services (elastic#80794)
  Fix reactRouterNavigate when used with a string (elastic#80520)
  [Security Solution] [Detections] Read privileges for dependencies (elastic#80852)
  [ML] Fixing exclude frequent in advanced wizard (elastic#81121)
  Fix security solution template label (elastic#80976)
  [DOCS] Update index management docs (elastic#80893)
  [APM] Error rate on service list page is not in sync with the value at the transaction page (elastic#80814)
  skip flaky suite (elastic#81072)
  [Task Manager] Cleans up legacy plugin structure (elastic#80381)
  Support unsigned_long fields (elastic#81115)
  [Form lib] Export internal state instead of raw state (elastic#80842)
  [Lens] Add toast notification when visualization is saved (elastic#80788)
  Index pattern edit field formatter API (elastic#78352)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more v7.1.1 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Form lib] Remove flat "raw" state
5 participants