import React from 'react'; import FormControl from 'react-bootstrap/lib/FormControl'; import Checkbox from 'react-bootstrap/lib/Checkbox'; import ArrayEditor from './ArrayEditor'; import BlockSelect from './BlockSelect'; import ShrinkSelect from './ShrinkSelect'; import MultiSelect from './MultiSelect'; import wrapDisplayName from './utils/wrapDisplayName'; import { Options } from './utils/normalizeOptions'; export type Editor = React.FC<{ value: T; onChange: (value: T) => void; disabled?: boolean; }>; /** * Provides creators for basic "editor" components, * which are used with PropertyEditor and ArrayEditor. * Each editor must take "value", "onChange" and "disabled" props. * Other props like "maxLength" and "placeholder" can be bound to the * component using creators in this module. * @module */ const formControl = ( options: { filter?: (input: T) => T; regex?: RegExp; type: string }, baseFilter?: (input: string) => T ) => { const { filter, regex, ...otherOptions } = options; const stringify = (val: any) => typeof val === 'undefined' ? '' : String(val); const process = (value: string, prevValue: T): T => { let outValue = baseFilter ? baseFilter(value) : (value as T); if (filter) outValue = filter(outValue); if (regex instanceof RegExp && !regex.test(value)) outValue = prevValue as T; return outValue; }; const result = (({ onChange, value, disabled }) => ( onChange(process(ev.target.value, value))} disabled={disabled} /> )) as Editor; result.displayName = `FormControl(${options.type})`; return result; }; export const text = (options: any = {}) => formControl({ ...options, type: 'text' }); export const password = (options: any = {}) => formControl({ ...options, type: 'password' }); export const number = (options: any = {}) => formControl({ ...options, type: 'number' }, parseFloat); export const integer = (options: any = {}) => formControl({ ...options, type: 'number' }, parseInt); export const textarea = (options: any = {}) => formControl({ ...options, componentClass: 'textarea' }); export const checkbox = (options: { label: string }) => { return (({ onChange, value, disabled }) => ( onChange(ev.target.checked)} disabled={disabled} > {options && options.label} )) as Editor; }; export const select = ( options: Options, params: any = {} ) => { return (props => ( )) as Editor; }; export const shrinkSelect = ( options: Options, params: any = {} ) => { return (props => ( )) as Editor; }; export const multiSelect = ( options: Options, params: any = {} ) => { return (props => ( )) as Editor; }; /** * Wraps another editor and turns it an ArrayEditor */ export const arrayOf = ( editor: Editor, newItemValue: T | ((values: T[]) => T), inline?: boolean ) => { const result: Editor = props => ( editor={editor} newItemValue={newItemValue} inline={!!inline} value={props.value} onChange={props.onChange} disabled={props.disabled} /> ); result.displayName = wrapDisplayName('arrayOf', editor); return result; }; export const inlineArrayOf = ( editor: Editor, newItemValue: any ) => { return arrayOf(editor, newItemValue, true); };