Giter Site home page Giter Site logo

useForm / useFormField about unihooks HOT 5 OPEN

dy avatar dy commented on August 26, 2024
useForm / useFormField

from unihooks.

Comments (5)

dy avatar dy commented on August 26, 2024
// similar to settings-panel
const params = [{ id: 'number', type: Number }]
const params2 = { number: 3, ... }

const [ fields, { set, submit, validate, reset } ] = useForm( params )
const { fieldName: { valid, error, touched, ref, type, validate }, ...rest } = fields

const [ { value, error, touched, ref, input }, { validate, set } ] = useField( 'name', { type: Number, validate: (value) => {} } )

from unihooks.

dy avatar dy commented on August 26, 2024

react-table introduces nice useTable hook approach, able to generate props for target elements. Same technique can be used here.

from unihooks.

dy avatar dy commented on August 26, 2024

3 aspects of useFormFIeld.

  1. Possibly unnecessary separation of useFormField and useInput: separates creation of some data from accessing - not conceptually very much different.

  2. useValidation can be a part of functionality, augmenting useInput. (although it redefines onChange, which may be redundant.

  3. Passing input props into hook, instead of direct element is doubtful practice. It's better to focus on direct generating the UI, instead of passing props through some transforms.

from unihooks.

dy avatar dy commented on August 26, 2024

useValidation is insufficient on itself: it must show error only when the input is blurred, not when focused, so it depends on input element state.
Also, we may want to validate on form submit for example, but validate requires value to check against, since it is not bound to specific input - therefore it is useful to have something that knows about "input store".

So, responding to 1, 2, 3.

  1. No need to separate - it can be used bot for creating and controlling, single hook returns "state" of some input element (null-ish state is allowed - it is not fully null but detached). That "virtual" state can optionally be rendered to real node.
  2. field.validate() is better.
  3. No need to pass input props, pass minimal input config.

from unihooks.

dy avatar dy commented on August 26, 2024

The quesion is: how to use useFormField as aspect, considering that it may create an input?
This is very similar to useStore(id, initObj) - if id is not found, it is created..
There can therefore be useFormField(id|element, initProps).
Conceptually, that is similar to upsert proposal: upsert(id, update: oldValue => newValue, insert: () => initValue), but in case of hooks update and insert functions are single init function.

More generally, resource hook creates a thread (like in erlang) of specific functionality (specific lens to look at data source).

To overcome explicit createStore call, there must be main aspect and auxiliary aspects - main aspect creates/controls entry, aux augments behavior.

from unihooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.