When the button is clicked I want an area to write an e-mail. And when the delete icon is clicked, this area is deleted. How can i do
addListItem = () => {
var number=0;
var list = [...this.state.list];
number++;
var ele=
<div id={number}>
<span>{number}
<TextField id="standard-basic" style={{width:"60%"}}></TextField>
</span>
<IconButton>
<Delete></Delete>
</IconButton>
</div>
list.push(ele)
this.setState({list})
}
<Button onClick={this.addListItem} style={{ float : "left" ,marginTop:10,backgroundColor: "#657c9e", borderRadius: 4, boxShadow: "#727272", width: 159, height: 46 , color: Color.AIRPLATFORM.WHITE , fontSize: 15 , fontWeight: 500,marginLeft:0}} variant="contained">ADD</Button><br></br><br></br><br></br>
<div id="list" style={{backgroundColor:"#ffffff",borderRadius:16,padding:10,minHeight:140,width:406,borderRadius:8,marginTop:10,boxShadow: "0px 1px 3px 0px rgba(0, 0, 0, 0.2), 0px 2px 1px -1px rgba(0, 0, 0, 0.12), 0 1px 0 rgba(0, 0, 0, 0.14)"}}>
{this.state.list}
</div>
Here is my code
You can do conditional rendering. So, what conditional rendering is, you render the JSX element based on a condition.
Conditional rendering
{condition ? <p>True</p> : <p>False</p>}
This one will render True paragraph if condition is ture, and False paragraph if condition is false
{condition && <p>True</p>}
This one will render True only if condition is True, and render nothing otherwise
In your case
In your case, first you need a state variable to store the render choice
const [renderDelete, setRenderDelete] = useState(true)
Then, on click you set renderDelete to false:
<button onClick={() => setRenderDelete(false)> Render Delete </button>
Lastly, you conditional render based on the renderDelete variable:
Instead of <Delete /> you render:
{renderDelete && <Delete />}
You can do the same logic to toggle between rendering Edit field or Delete field:
{renderDelete ? <Delete /> : <Edit />}
Related
I am trying to programmatically set the selected index and it works fine, thanks to the [(selectedIndex)]="", but whenever I select index with this, there is no highlight on the selected item.
It is selected because you can see the content of it, but there is no graphical representation of selected tab state.
Is this a bug or it can be done somehow?
Html
<mat-tab-group [(selectedIndex)]="this.dataService.selectedTabs[0]">
<mat-tab *ngFor="let Tab of this.dataService.Tabs">
<ng-template mat-tab-label>
{{Tab.label}}
</ng-template>
<div *ngIf="Tab.childTabs.length !== 0">
//INSIDE OF THIS SECTION THERE ARE MORE NESTED TABS
</div>
</mat-tab>
</mat-tab-group>
And when I open this tab, nested tabs within its content won't be highlighted but they are opened.
Ts
public selectedTabs: any[] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
This solution works but it is mainly a workaround.
Just add this code in the style.css file:
.mat-tab-label-active {
bottom-border-style:solid !important;
opacity: 1 !important;
}
Using Material UI #next v1.0.0 beta 32:
Tabs labels wrap as expected on smaller devices.
But the wrapping makes them change font-size, which in turn, in some screen width removes the need for the text to wrap.
So I end up with this: tabs with non-wrapped labels and different font sizes.
sandbox example: https://codesandbox.io/s/o7worrr32q
In order to see the described result make the window narrow enough for at least 1 tab label to wrap, but not all.
I' have overriddeen the wrapped styles this way:
<Tab
value={value}
label='my label'
classes={{
root: classes.tab,
rootPrimarySelected: classes.selected,
labelWrapped: classes.labelWrapped
}}
/>
and my style:
labelWrapped: {
fontSize: '0.875rem'
},
The problem as illustrated in this gif animation, is that as you click on other tabs, the text wraps and unwraps alternatively, seemingly without reason.
My guess is that a padding changes somewhere, but I can't figure it out.
It was easier than I thought in the end:
I only had to use the MUI provided css override labelWrapped, as documented in the MUI API:
<Tab
aria-label="aria description"
label="Wrapping Label"
icon={<Icon />}
value={x}
classes={{
root: classes.root,
labelContainer: classes.labelContainer,
rootInheritSelected: classes.rootInheritSelected,
labelWrapped: classes.labelWrapped,
}}
component={Link}
to={{
pathname: '/my/router/url',
search: this.props.location.search,
}}
/>
and my overriding styles:
const styles = theme => ({
root: {
minWidth: 60,
height: '100%',
color: 'rgba(255,255,255,0.5)',
alignItems: 'flex-start',
paddingBottom: 5,
wordWrap: 'break-word',
flex: 1,
overflowWrap: 'break-word',
textAlign: 'center',
},
labelContainer: {
paddingLeft: 0,
paddingRight: 0,
},
rootInheritSelected: {
color: '#FFF',
},
labelWrapped: {
fontSize: '0.875rem',
},
})
Good afternoon, I am trying to write a SPA with React.
I get a JSON-Object from a webservice wich validates the Inputfields.
Now i want so set the Style of the Inputfield depending on the answer of the Webservice.
At example the Style shoud be:
style={{borderColor: 'green', boxShadow: '0 0 5px green',}}
when the JSONValue is 0
<Input
style = ???
...
/>
My JSON looks like this:
{
"validate": {
"isValidTest": 0
...
}
}
edit: the JSON can have three diffrent Values witch three different Styles.
This is an example of what you could do
Example 1
<Input
style =
{{borderColor: obj.validate.isValidTest === 0 ? 'red':'green'
boxShadow: `0 0 5px ${obj.validate.isValidTest === 0 ? 'red' : 'green'}`}}
...
/>
What's happening here
By utilizing the shorthand conditionals we can apply a borderColor & boxShadow to the style depending on the value of isValidTest within obj (which will be replaced by the name of your object)(!!)
Have a look at this example to see what is happening here
NOTE
Ofcourse, this could be separated from the inline style attribute by setting a className depending on the outcome of isValidTest or by applying a different style constant to it depending on the same conditional
Example 2
const errorStyles = {
borderColor: 'red',
boxShadow: '0 0 5px red'
}
const validStyles = {
borderColor: 'green',
boxShadow: '0 0 5px green'
}
<Input
style={ obj.validate.isValidTest === 0 ? errorStyles : validStyles }
/>
Again, in this sandbox both examples are shown in the same file, on the same page.
You could also have a look at this question & answer for more examples and information
EDIT
To account for more than two values in isValidTest you could go at it like so:
const errorStyles = {
borderColor: 'red',
boxShadow: '0 0 5px red'
}
const validStyles = {
borderColor: 'green',
boxShadow: '0 0 5px green'
}
const error2Styles = {
borderColor: 'magenta',
boxShadow: '0 0 5px magenta'
}
const valid2Styles = {
borderColor: 'yellow',
boxShadow: '0 0 5px yellow'
}
isValid = (valid) => {
switch(valid) {
case -1:
return errorStyles
break;
case 0:
return error2Styles
break;
case 1:
return validStyles
break;
case 2:
return valid2Styles
break;
}
}
<Input
style={this.isValid(obj.validate.isValidTest)}
/>
What happens in the above example
As seen above the style attribute refers to this.isValid which in turn return a const containing the styles, supplied to this function is the isValidTest property from your obj.validate.
I have updated the sandbox to include the above example
Hope this helps!
I noticed this website has a list of checkboxes, but when I look at the HTML I just see div's with CSS classes on them.
How do you create checkboxes and check them off just by using CSS?
What is the benefit of doing it via CSS?
You certainly can. The advantage is that it gives you way more flexibility. For example, here's a basic React component that uses divs for the checkbox:
https://jsbin.com/xarewiweza/1/edit?html,js,output
const styles = {
outer: {
borderRadius: 5,
border: '2px solid gray',
width: 30,
height: 30,
cursor: 'pointer',
},
inner: checked => ({
borderRadius: '50%',
height: 28,
width: 28,
backgroundColor: checked ? 'red' : 'transparent',
margin: 1,
transition: 'background-color 0.2s ease',
})
}
class Checkbox extends React.Component {
constructor(props) {
super(props)
this.state = {
checked: false,
}
}
render() {
console.log(this.state)
return (
<div
onClick={() => this.setState({ checked: !this.state.checked, })}
style={styles.outer}>
<div style={styles.inner(this.state.checked)} />
</div>
)
}
}
ReactDOM.render(<Checkbox />, document.body)
If you wanted animated checkmarks, or other cool stuff, you'd have to go custom.
The disadvantage is that you don't get some of the default HTML functionality, though I'm not sure if there is anything useful with the checkbox specifically. For example, with the default select on iOS, it automatically uses the carousel selecter, which is a handy feature.
How can I store escaped HTML in an attribute value? If I try to store e.g. > or < in an attribute value on a div, then try to read it back, the browser is unescaping it.
E.g.
HTML
<div id="content" tooltip="Ref: <Unknown>">one fine day in the middle of the night...</div>
<button id="show-tooltip">Show tooltip</button>
JS
var content = document.getElementById('content');
var show = document.getElementById('show-tooltip');
var tooltip;
show.addEventListener('click', function (event) {
if (tooltip) {
document.body.removeChild(tooltip);
}
tooltip = document.createElement('div');
tooltip.classList.add('tooltip');
tooltip.innerHTML = content.getAttribute('tooltip');
tooltip = document.body.appendChild(tooltip);
});
CSS
.tooltip {
width: 100px;
height: 50px;
border: 1px solid rgba(0, 0, 0, .3);
background-color: rgba(255, 255, 0, .3);
}
Fiddle: http://jsfiddle.net/axo25vhy/
I want the tooltip text to display "Ref: <Unknown>", however the text <Unknown> is being converted to an HTML tag so does not appear.
The simplest way is probably to encode the ampersand in < as & => <
http://jsfiddle.net/etoz8o0x/
When your content is appended to the html, the browser decodes & to <. If you encode & correctly as <, the browser decodes < which will then presented visually as a <