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

Public interface for custom state #21

Open
ShacharHarshuv opened this issue Oct 31, 2024 · 0 comments
Open

Public interface for custom state #21

ShacharHarshuv opened this issue Oct 31, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@ShacharHarshuv
Copy link
Owner

We probably already have that function in, but we need to think about it we want to expose it and how. We also need to think about whether we want to expose a "writable" version and also options for how these states propagate from child / parent.

Custom State

We can extend the behavior of the form controls with custom state using the function createFormState. Many of the form states described before use it behind the scenes.

function createFormState<T, Args extends any[]>(
  descr: string,
  fallback: T,
  createInitialState: (form: Form<any>, ...args: Args) => T),
): {
  create: (...args: Args) => StateFactory, 
  get: (form: Form<any>) => T, // returns the fallback if doesn't exist
  set: (form: Form<any>, state: T) => void,
}

Here's an example of how you can use it:

// display-name.ts

export const {
  create: displayName, 
  get: getDisplayName,
  set: setDisplayName
} = createFormState(
  "Display Name",
  "",
  (form: Form<any>, displayName: string) => {
    return displayName;
  }
);

// my-form.ts

import { displayName, getDisplayName } from './my-custom-disabled-state';

const myForm = form({
  name: "John",
  age: 42,
}, displayName('Person'));

const displayNamed = getDisplayName(myForm);
@ShacharHarshuv ShacharHarshuv added the enhancement New feature or request label Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant