Giter VIP home page Giter VIP logo

ink-form's Introduction

ink-form

ink-form is a Node library for displaying a user-friendly form in a terminal window. It can be used in two ways, either by using the React Ink component Form exported by the package, or by using the imperative API openForm(options).

alt text alt text

Example usage

npm install ink-form react ink
const options = [
  { label: 'Opt 1', value: 'a' },
  { label: 'Opt 2', value: 'b' },
];

const form: FormProps = {
  form: {
    title: 'Form title',
    sections: [
      {
        title: 'Text and Number fields',
        fields: [
          { type: 'string', name: 'field1', label: 'Input with initial value', initialValue: 'Initial value' },
          { type: 'string', name: 'field2', label: 'Masked input', mask: '*' },
          { type: 'integer', name: 'field3', label: 'Integer between -5 and 8, stepsize 2', min: -5, max: 8, step: 2 },
          { type: 'float', name: 'field4', label: 'Float between 0 and 5, stepsize 0.1', min: 0, max: 5, step: 0.1 },
        ],
      },
      {
        title: 'Select and boolean fields',
        fields: [
          { type: 'select', name: 'field5', label: 'Select', options },
          { type: 'multiselect', name: 'field6', label: 'Multi Select', options },
          { type: 'boolean', name: 'field7', label: 'Boolean select', options },
        ],
      },
    ],
  },
};

// either:
(async () => {
  const result = await openForm(form);
  console.log(`Finished with value`, result);
})();

// or:
render(
  <Form
    {...form}
    onSubmit={result => {
      console.log(`Finished with value`, result);
    }}
  />
);

If you want to see how using ink-form feels, you can clone this repo, run yarn to install dependencies and then run one of the demo scripts:

Update Note

This package is now pure ESM as of 2.0.0. (Details: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)

With that upgrade, I had to remove the multi select component as its dependency does not support ESM. Please use the v1x version branch with CJS if you need that.

Documentation

Detailed documentation is available at lukasbach.github.io/ink-form. Install the package with npm install ink-form --save or yarn add ink-form to your project.

Then, render the form by invoking the imperative interface or by rendering the React Ink component. Both take FormProps as parameters.

The most important property is the form-property, which dictates how the form fields are structured. It follows the FormStructure-interface. Look into the example above to see an example.

Custom fields

A form field describes a type of input, i.e. text input, number input etc. Included are:

  • FormFieldString
  • FormFieldInteger
  • FormFieldFloat
  • FormFieldSelect
  • FormFieldMultiSelect
  • FormFieldBoolean

You can add your own form field by extending AbstractFormField and implementing an associated FormFieldManager<CustomFormField>.

alt text

ink-form's People

Contributors

jackhatton avatar lukasbach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ink-form's Issues

Multiselect showing [object object] with selected values

Not sure where the issue is, but your examples work great and my code works in this repo, but the code from npm produces [object object] in the ui for whatever is selected.

DevTools shows the same values in the example vs my code. I found that installing what is in github directly with yalc fixes the issue. Perhaps some changes didn't get deployed to npm in the 1.0.1 version?

Incorrent Semver for ink-4 peerDependency

When running npm install ink-form while in an Ink 4 project, the peerDependency fails to resolve.

Steps to Replicate:
npx create-ink-app --typescript
npm i ink-form

I expected the library to install without issue as of #8, Ink 4 support issue had been closed.

Screenshot 2024-05-09 at 6 50 03 PM

Link to line causing problem:

"ink": ">4",

Custom form field types are currently not possible due to incorrect types.d.ts in the distribution

The declaration of FormField in types.d.ts is the below:

export declare type FormField = FormFieldString | FormFieldInteger | FormFieldFloat | FormFieldSelect | FormFieldMultiSelect | FormFieldBoolean;

From reading the source code, I expected it to contain AbstractFormField<any, any> as one of the unioned types, like this:

export declare type FormField = FormFieldString | FormFieldInteger | FormFieldFloat | FormFieldSelect | FormFieldMultiSelect | FormFieldBoolean | AbstractFormField<any, any>;

This prevents any custom form field types from being declared; all custom form field manager declarations display the error

TS2344: Type 'FormFieldCustom' does not satisfy the constraint 'FormField'.   
Type 'FormFieldCustom' is not assignable to type 'AbstractFormField<"boolean", boolean>'.     
Types of property 'type' are incompatible.       
Type '"custom"' is not assignable to type '"boolean"'.

I have the latest distribution installed, 1.0.1.

onChange for FormField isn't triggered

Describe the bug
When creating a FormField with an onChange handler, the handler isn't called, even though it's defined om the type.

To Reproduce
Steps to reproduce the behavior:

<Form form={{ sections: [ { type: "boolean", title: "a", name: "a", label: "a", onChange: () => { throw new Error("never thrown"); }} ]}} />

Expected behavior
I'd expect the onChange handler to be called.

Additional context
I'm looking to conditionally render fields in a form as requested in #6 but I feel like having a custom manager for a built-in field just to get the onChange handler to work, feels too detailed for such a common task.

Plus, the onChange attribute is already part of the FormField definition so when working with typescript it tells you that it's already part of the API.

Anyway, thanks for this tool, was super easy to create a pretty cool form 🙏

P.S.
if you could support ink@4 and react@18 that would also be amazing!

Customise form components?

This is awesome. 👊

Would it be difficult to modify the <Form> to override some small styling, e.g. colors (blue looks really dark) and the text for the <SubmitButton>?

Property 'sections' is missing in type 'FormProps' but required in type 'FormStructure'.

I'm getting an error, is this a bug or me just being dumb?
Here's the error:

src/Build.tsx:26:4 - error TS2741: Property 'sections' is missing in type 'FormProps' but required in type 'FormStructure'.

26    form={form}
      ~~~~

  node_modules/ink-form/lib/types.d.ts:13:5
    13     sections: FormSection[];
           ~~~~~~~~
    'sections' is declared here.
  node_modules/ink-form/lib/types.d.ts:4:5
    4     form: FormStructure;
          ~~~~
    The expected type comes from property 'form' which is declared here on type 'IntrinsicAttributes & FormProps & { children?: ReactNode; }'

Here's the code:

import { Form, FormProps } from 'ink-form';
import { Box } from 'ink';
import React from 'react';


const form: FormProps = {
	form: {
		title: 'Build',
		sections: [
			{
				title: 'Build Options',
				description: 'Options for building',
				fields: [
					{ type: 'boolean', name: 'installHD', label: 'Install HD?', required: true, initialValue: false },
				]
			}
		]
	}
};

const BuildForm = (): React.ReactElement => {
	return (
		<Form
			form={form}
		/>	
	);
};

export default BuildForm;

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.