mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-02-21 22:54:11 +08:00
fix(editable-list): Fix use of children props in EditableNode + updates
- Fix issue with using this.props.children which was preventing rerendering - Updates styles for list - Updates create item input: - Add key to prevent warning - Add prop for the placeholder - Add onBlur behavior
This commit is contained in:
parent
a1bb98ebf4
commit
1ffd324dde
2 changed files with 27 additions and 11 deletions
|
@ -12,6 +12,7 @@ class EditableList extends Component {
|
||||||
PropTypes.element,
|
PropTypes.element,
|
||||||
])),
|
])),
|
||||||
className: PropTypes.string,
|
className: PropTypes.string,
|
||||||
|
createPlaceholder: PropTypes.string,
|
||||||
onCreateItem: PropTypes.func,
|
onCreateItem: PropTypes.func,
|
||||||
onDeleteItem: PropTypes.func,
|
onDeleteItem: PropTypes.func,
|
||||||
onItemEdited: PropTypes.func,
|
onItemEdited: PropTypes.func,
|
||||||
|
@ -22,6 +23,8 @@ class EditableList extends Component {
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
children: [],
|
children: [],
|
||||||
|
className: '',
|
||||||
|
createPlaceholder: '',
|
||||||
onDeleteItem: ()=> {},
|
onDeleteItem: ()=> {},
|
||||||
onItemEdited: ()=> {},
|
onItemEdited: ()=> {},
|
||||||
onItemSelected: ()=> {},
|
onItemSelected: ()=> {},
|
||||||
|
@ -30,7 +33,6 @@ class EditableList extends Component {
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this._items = this.props.children;
|
|
||||||
this._doubleClickedItem = false;
|
this._doubleClickedItem = false;
|
||||||
this.state = props.initialState || {
|
this.state = props.initialState || {
|
||||||
editing: null,
|
editing: null,
|
||||||
|
@ -60,9 +62,15 @@ class EditableList extends Component {
|
||||||
if (_.includes(['Enter', 'Return'], event.key)) {
|
if (_.includes(['Enter', 'Return'], event.key)) {
|
||||||
this.setState({creatingItem: false});
|
this.setState({creatingItem: false});
|
||||||
this.props.onItemCreated(event.target.value);
|
this.props.onItemCreated(event.target.value);
|
||||||
|
} else if (event.key === 'Escape') {
|
||||||
|
this.setState({creatingItem: false});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_onCreateInputBlur = ()=> {
|
||||||
|
this.setState({creatingItem: false});
|
||||||
|
}
|
||||||
|
|
||||||
_onItemClick = (event, item, idx)=> {
|
_onItemClick = (event, item, idx)=> {
|
||||||
this._selectItem(item, idx);
|
this._selectItem(item, idx);
|
||||||
}
|
}
|
||||||
|
@ -81,14 +89,14 @@ class EditableList extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
_onListKeyDown = (event)=> {
|
_onListKeyDown = (event)=> {
|
||||||
const len = this._items.size;
|
const len = this.props.children.size;
|
||||||
const handle = {
|
const handle = {
|
||||||
'ArrowUp': (sel)=> sel === 0 ? sel : sel - 1,
|
'ArrowUp': (sel)=> sel === 0 ? sel : sel - 1,
|
||||||
'ArrowDown': (sel)=> sel === len - 1 ? sel : sel + 1,
|
'ArrowDown': (sel)=> sel === len - 1 ? sel : sel + 1,
|
||||||
'Escape': ()=> null,
|
'Escape': ()=> null,
|
||||||
};
|
};
|
||||||
const selected = (handle[event.key] || ((sel)=> sel))(this.state.selected);
|
const selected = (handle[event.key] || ((sel)=> sel))(this.state.selected);
|
||||||
this._selectItem(this._items[selected], selected);
|
this._selectItem(this.props.children[selected], selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onCreateItem = ()=> {
|
_onCreateItem = ()=> {
|
||||||
|
@ -101,7 +109,7 @@ class EditableList extends Component {
|
||||||
|
|
||||||
_onDeleteItem = ()=> {
|
_onDeleteItem = ()=> {
|
||||||
const idx = this.state.selected;
|
const idx = this.state.selected;
|
||||||
const item = this._items[idx];
|
const item = this.props.children[idx];
|
||||||
if (item) {
|
if (item) {
|
||||||
this.props.onDeleteItem(item, idx);
|
this.props.onDeleteItem(item, idx);
|
||||||
}
|
}
|
||||||
|
@ -150,19 +158,21 @@ class EditableList extends Component {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_renderCreateItem = (onCreateInputKeyDown = this._onCreateInputKeyDown)=> {
|
_renderCreateItem = ()=> {
|
||||||
return (
|
return (
|
||||||
<div className="create-item">
|
<div className="create-item-input" key="create-item-input">
|
||||||
<input
|
<input
|
||||||
autoFocus
|
autoFocus
|
||||||
type="text"
|
type="text"
|
||||||
onKeyDown={onCreateInputKeyDown} />
|
placeholder={this.props.createPlaceholder}
|
||||||
|
onBlur={this._onCreateInputBlur}
|
||||||
|
onKeyDown={this._onCreateInputKeyDown} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let items = this._items.map((item, idx)=> this._renderItem(item, idx));
|
let items = this.props.children.map((item, idx)=> this._renderItem(item, idx));
|
||||||
if (this.state.creatingItem === true) {
|
if (this.state.creatingItem === true) {
|
||||||
items = items.concat(this._renderCreateItem());
|
items = items.concat(this._renderCreateItem());
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
border: 1px solid @border-secondary-bg;
|
border: 1px solid @border-secondary-bg;
|
||||||
background-color: @background-secondary;
|
background-color: @background-secondary;
|
||||||
|
min-height: 50px;
|
||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
|
|
||||||
.editable-item {
|
.editable-item {
|
||||||
|
@ -32,14 +33,19 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.create-item {
|
.editable-item+.create-item-input {
|
||||||
padding: @padding-small-vertical @padding-small-horizontal;
|
|
||||||
border-top: 1px solid @border-color-divider;
|
border-top: 1px solid @border-color-divider;
|
||||||
|
}
|
||||||
|
|
||||||
|
.create-item-input {
|
||||||
input {
|
input {
|
||||||
|
padding: 0 @padding-small-vertical;
|
||||||
border: none;
|
border: none;
|
||||||
padding: 0;
|
|
||||||
font-size: inherit;
|
font-size: inherit;
|
||||||
}
|
}
|
||||||
|
::-webkit-input-placeholder {
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue