-
Notifications
You must be signed in to change notification settings - Fork 77
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
add manual address entry to letter form #1775
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import React from "react"; | ||
import { DynamicListProps } from "./PropTypes"; | ||
import { v1 as uuid } from "uuid"; | ||
import { isFunction } from "lodash"; | ||
|
||
interface DynamicListState<TModel> { | ||
/** An array of objects that represent the state of the child components. */ | ||
listItems: Array<TModel>; | ||
/** An array of UUID keys mapped 1:1 with listItems, used to indicate | ||
* when React should re-render. | ||
*/ | ||
keys: Array<string>; | ||
} | ||
|
||
/** | ||
* A component that can generate an arbitrary number | ||
* of instances of a component provided by the caller. | ||
*/ | ||
export default class DynamicList<TModel> extends React.Component< | ||
DynamicListProps<TModel>, | ||
DynamicListState<TModel> | ||
> { | ||
/** | ||
* Initialize component and its state. | ||
* @param {DynamicListProps<TModel>} props | ||
*/ | ||
constructor(props: DynamicListProps<TModel>) { | ||
super(props); | ||
this.state = { | ||
listItems: [], | ||
keys: [], | ||
}; | ||
} | ||
|
||
/** | ||
* Removes the child component instance at the provided index. | ||
* @param {number} index | ||
*/ | ||
deleteItem(index: number): void { | ||
const updatedListItems = [...this.state.listItems]; | ||
updatedListItems.splice(index, 1); | ||
|
||
const updatedKeys = [...this.state.keys]; | ||
updatedKeys.splice(index, 1); | ||
|
||
this.setState({ listItems: updatedListItems, keys: updatedKeys }); | ||
this.props.updateModel(this.state.listItems); | ||
} | ||
|
||
/** | ||
* Creates a new child component instance. | ||
*/ | ||
addItem(): void { | ||
const updatedListItems = [ | ||
...this.state.listItems, | ||
this.props.modelFactory(), | ||
]; | ||
const updatedKeys = [...this.state.keys, uuid()]; | ||
|
||
this.setState({ listItems: updatedListItems, keys: updatedKeys }); | ||
this.props.updateModel(this.state.listItems); | ||
} | ||
|
||
/** | ||
* Replaces the child component state at the provided | ||
* index with the provided value. | ||
* @param {number} index the index of the child instance to update | ||
* @param {TModel} value the new state value | ||
*/ | ||
updateItem(index: number, value: TModel): void { | ||
const updatedModel = [...this.state.listItems]; | ||
updatedModel[index] = value; | ||
this.setState({ listItems: updatedModel }); | ||
this.props.updateModel(this.state.listItems); | ||
} | ||
|
||
/** | ||
* Renders each child instance using the component provided in the caller. | ||
* @return {React.ReactNode} the rendered child components | ||
*/ | ||
renderChildren(): React.ReactNode { | ||
return this.state.listItems.map((entry: TModel, index: number) => { | ||
return ( | ||
<div key={this.state.keys[index]}> | ||
<button onClick={() => this.deleteItem(index)}>Delete</button> | ||
{this.props.eachRender((value: TModel) => | ||
this.updateItem(index, value) | ||
)} | ||
</div> | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* React render method. | ||
* @return {React.ReactNode} the rendered component | ||
*/ | ||
render(): React.ReactNode { | ||
return ( | ||
<> | ||
{this.renderChildren()} | ||
<button onClick={() => this.addItem()}> | ||
{isFunction(this.props.addText) | ||
? this.props.addText(this.state.listItems) | ||
: this.props.addText} | ||
</button> | ||
</> | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface DynamicListProps<TModel> { | ||
addText: string | ((listItems: Array<TModel>) => string); | ||
modelFactory: () => TModel; | ||
updateModel: (value: Array<TModel>) => void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. onItemsUpdated? onListItemsUpdated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This, I like- it has the potential to be used for more than just updating the model, and I will include it in a new commit 😊 |
||
eachRender: (updateModel: (value: TModel) => void) => JSX.Element; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe "renderChild" or "renderEntry" or "renderListEntry"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updateModel => onItemUpdated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think renderListItem and onListItemUpdated to keep consistent with the rest of it |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { default as Footer } from "./Footer"; | ||
export { default as Header } from "./Header"; | ||
export { default as Layout } from "./Layout"; | ||
export { default as DynamicList } from "./DynamicList"; | ||
export * from "./PropTypes"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import React, { useState, ReactElement } from "react"; | ||
|
||
import { LobAddress } from "./LetterTypes"; | ||
|
||
type AddressInputProps = { | ||
/** callback function for every time the address is updated */ | ||
updateParent: (a: LobAddress) => void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. onAddressUpdated? |
||
}; | ||
|
||
/** Renders input fields for the user's address | ||
* | ||
* @return {ReactElement} the rendered component | ||
*/ | ||
export default function AddressInput({ | ||
updateParent, | ||
}: AddressInputProps): ReactElement { | ||
const [address, setAddress] = useState({} as LobAddress); | ||
|
||
/** Callback for when part of user's address is updated. | ||
* adds it to the full address in our internal state and | ||
* calls the parent's callback | ||
* | ||
* @param {Address} addressPatch the updated address | ||
*/ | ||
function updateAddress(addressPatch: Partial<LobAddress>) { | ||
const updatedAddress = { ...address, ...addressPatch }; | ||
setAddress(updatedAddress); | ||
updateParent(updatedAddress); | ||
} | ||
|
||
return ( | ||
<> | ||
<fieldset className="pure-form-stacked"> | ||
<div className="pure-control-group"> | ||
<label>Name</label> | ||
<input | ||
className="pure-u-1" | ||
placeholder="Name" | ||
type="text" | ||
name="name" | ||
onChange={(e) => | ||
updateAddress({ | ||
name: e.target.value, | ||
}) | ||
} | ||
/> | ||
</div> | ||
</fieldset> | ||
|
||
<fieldset> | ||
<div className="pure-control-group"> | ||
<label>Address</label> | ||
|
||
<input | ||
className="pure-u-1" | ||
placeholder="123 Main St" | ||
type="text" | ||
name="address" | ||
onChange={(e) => | ||
updateAddress({ | ||
address_line1: e.target.value, | ||
}) | ||
} | ||
/> | ||
|
||
<div className="pure-g"> | ||
<div className="right-pad-input pure-u-1 pure-u-md-1-3"> | ||
<input | ||
placeholder="City" | ||
name="city" | ||
onChange={(e) => | ||
updateAddress({ | ||
address_city: e.target.value, | ||
}) | ||
} | ||
/> | ||
</div> | ||
<div className="right-pad-input pure-u-1 pure-u-md-1-3"> | ||
<input | ||
placeholder="State" | ||
type="text" | ||
name="state" | ||
onChange={(e) => | ||
updateAddress({ | ||
address_state: e.target.value, | ||
}) | ||
} | ||
/> | ||
</div> | ||
|
||
<div className="pure-u-1 pure-u-md-1-3"> | ||
<input | ||
className="full-width" | ||
placeholder="Zipcode" | ||
type="text" | ||
name="zip" | ||
onChange={(e) => | ||
updateAddress({ | ||
address_zip: e.target.value, | ||
}) | ||
} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</fieldset> | ||
</> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emptyModelFactory? defaultModelFactory? createEmptyModel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure those names would be appropriate, since the consumer could definitely create a filled or partially-filled model through a closure, and there would not be anything non-default that seems like it would call for using "default". Is there something you're thinking of that I'm missing?