-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FieldList field type with subtype FieldConfigList
- Loading branch information
1 parent
5900185
commit 796d1c2
Showing
4 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as React from 'react'; | ||
import { connect } from '../redux'; | ||
import { FieldBase } from './base'; | ||
import { FieldDropdown } from './dropdown'; | ||
import { FieldDropdownWithInput } from './dropdownwithinput'; | ||
import { FieldString } from './string'; | ||
|
||
// Maybe in the future we can make this generic, but we'd have to make FieldDropdown etc also generic first | ||
type T = string; | ||
|
||
// TODO: Allow reordering of items | ||
|
||
export interface Props<T> { | ||
options?: T[]; | ||
freeText?: boolean; | ||
//displayName?(item: T): string; | ||
displayStyle?(item: T): React.CSSProperties; | ||
} | ||
interface State { | ||
open: boolean; | ||
inputText: string; | ||
} | ||
export class FieldList extends FieldBase<T[] | undefined, Props<T>, State> { | ||
public getInitialSubState(props: Props<string>): State { | ||
return { open: false, inputText: '' }; | ||
} | ||
protected renderNewInputField(): React.ReactNode { | ||
const { inputText } = this.state; | ||
const { freeText, options, displayStyle } = this.props; | ||
const FD = options?.length ? (freeText ? FieldDropdownWithInput : FieldDropdown) : (freeText ? FieldString : undefined); | ||
return FD && <FD value={inputText} values={options} displayStyle={displayStyle} | ||
onChange={t => this.setState({ inputText: t || '' })} />; | ||
} | ||
public renderInput(): React.ReactNode { | ||
const { newValue } = this.state; | ||
const { displayStyle } = this.props; | ||
const newInput = this.renderNewInputField(); | ||
return <div className="FieldList"> | ||
{newInput && <div className="adder">{newInput}<button onClick={this.onAdd}>+</button></div>} | ||
{newValue?.map((item, index) => <li key={index} style={displayStyle?.(item)}> | ||
<p>{item}</p> | ||
<button onClick={this.onRemove.bind(this, index)}>x</button> | ||
</li>)} | ||
</div>; | ||
} | ||
protected onAdd = () => { | ||
const inputText = this.state.inputText?.trim(); | ||
if (!inputText) return; | ||
this.setState({ | ||
inputText: '', | ||
newValue: [...this.state.newValue || [], inputText] | ||
}, () => this.props.onChange(this.state.newValue)); | ||
}; | ||
protected onRemove = (index: number) => { | ||
const newValue = [...this.state.newValue || []]; | ||
newValue.splice(index, 1); | ||
this.setState({ newValue }, () => this.props.onChange(newValue)); | ||
}; | ||
} | ||
|
||
export type FieldConfigListState = { options: string[] }; | ||
export const FieldConfigList = connect(FieldList)<FieldConfigListState>( | ||
state => ({ options: state.data.configs.map(c => c.name).sort() }), | ||
); |