I'm trying to make an InputGroup that has two icons OR one icon and one button.
One icon should be used to check if the input field is empty or not (got that working). The other icon OR button should be used to "inject" some text into the Input field.
Currently my code looks like this:
import React, { Component } from 'react'
import { Content, List, InputGroup, Input, Icon, Button } from 'native-base'
export default class AddEquipment extends Component {
constructor(props) {
super(props)
this.state = {
parameters: {
one: {value:"", hint: "One"},
two: {value:"", hint: "Two"},
three: {value:"Valid", hint: "Three"}
}
}
this.updateParameter = this.updateParameter.bind(this)
this.validationStyle = this.validationStyle.bind(this)
}
componentDidMount() {
}
updateParameter(key, value) {
newState = {...this.state}
newState.parameters[key].value = value
this.setState = newState;
}
validationStyle(text) {
color = text === "" ? "#b03939" : "#649370"
return (
{ marginRight:25, color
}
)
}
render () {
return (
<Content>
{ Object
.keys(this.state.parameters)
.map( key =>
<InputGroup
key={`${key}_InputGroup`}
iconRight
borderType='regular'
style={{margin:5}}
>
<Input
placeholder={this.state.parameters[key].hint}
onChangeText={(text) => {
console.log(this.state.parameters)
this.updateParameter(key, text)} }
value={key.value}
/>
<Icon
key={`${key}_validIcon`}
name={ this.state.parameters[key].value === "" ? 'ios-alert' : 'ios-checkmark-circle'}
style={ this.validationStyle(this.state.parameters[key].value) }
/>
<Icon
key={`${key}_injectNA`}
name='ios-beer'
onPress={() => this.updateParameter(key, "Cheers!") }/>
</InputGroup>
)
}
</Content>
)
}
}
Which gives me the following result
First issue
As you can see I have trouble getting the other icon appear - it doesn't seem to lie behind the first.
A button would be as good, but it always drops below the Input and not next to it. Styling is not my strongest style - hence why I use the awesome framework NativeBase
Second issue
Another problem I have is that the validation does not seem to change icon and color after the state is being updated. Seems like the style are loaded only once.
on the first issue would you consider validity icon in front? InputGroup can dispay maximum two icons: before and after text input, see code below.
on the second issue it's caused that you assign value to the state, while in React you should call this.setState(newState), see reference
another issue is accessing to the object key's value by key.value, while key is just a string (key name), while real object accessed by this.state.parameters[key]
Following code fixes all mentioned issues (but validity icon is on the left):
import React, { Component } from 'react'
import { Content, List, InputGroup, Input, Icon, Button } from 'native-base'
const errorStyle = {color: "#b03939"}
const validStyle = {color: "#649370"}
export default class AddEquipment extends Component {
constructor(props) {
super(props)
this.state = {
parameters: {
one: {value:"", hint: "One"},
two: {value:"", hint: "Two"},
three: {value:"Valid", hint: "Three"}
}
}
this.updateParameter = this.updateParameter.bind(this)
}
componentDidMount() {
}
updateParameter(key, value) {
let newState = {...this.state}
newState.parameters[key].value = value
this.setState(newState);
}
static validationStyle(text) {
return text === "" ? errorStyle : validStyle;
}
render () {
return (
<Content>
{ Object
.keys(this.state.parameters)
.map( key =>
<InputGroup
key={`${key}_InputGroup`}
borderType='regular'
style={{margin:5}}
>
<Icon
key={`${key}_validIcon`}
name={ this.state.parameters[key].value === "" ? 'ios-alert' : 'ios-checkmark-circle'}
style={ AddEquipment.validationStyle(this.state.parameters[key].value) }
/>
<Input
placeholder={this.state.parameters[key].hint}
onChangeText={(text) => {
console.log(this.state.parameters)
this.updateParameter(key, text)} }
value={this.state.parameters[key].value}
/>
<Icon
key={`${key}_injectNA`}
name='ios-beer'
onPress={() => this.updateParameter(key, "Cheers!") }/>
</InputGroup>
)
}
</Content>
)
}
}
Related
i have created w multi step register form and to do the validation it gives error and want to do the validation with this format export const CreateAccount extends Component like this i wan
You can store the current color of the button in the state and just switch its value when the button is clicked. Something like this:
const DynamicButton = () => {
const [color, setColor] = useState("primary")
const switchColor = () => {
if (color === "primary") setColor("secondary")
else setColor("primary")
}
return (
<Button
color={color}
onClick={switchColor}
variant="contained"
>
Click me to change my color!
</Button>
)
}
This is using React hooks. But if you are using React class components the equivalent code would be like this:
class DynamicButton extends React.Component {
constructor(props) {
super(props)
this.state = {
color: "primary"
}
}
switchColor = () => {
if (this.state.color === "primary") this.setState({ color: "secondary" })
else this.setState({ color: "primary" })
}
render() {
return (
<Button
color={this.state.color}
onClick={this.switchColor}
variant="contained"
>
Click me to change my color!
</Button>
)
}
}
enter image description here
how to do that color and setcolor to get another color for example setColor ("orange") with material ui of course when i do that to setcolor it changes nothing
First timer here on StackOverflow! I'm trying to emulate a terminal interface for one of my portfolio projects. The way I have envisioned the terminal is that each terminal box has a state object with a few key/value pairs. Ideally, when someone enters text into the terminal box input form, the input becomes disabled and a new terminal box is rendered on the screen with a dynamic response based upon the userInput text which has been saved in the state. Where I'm stuck:
Once userInput state has been updated, how do I get a new terminal box to render beneath the prior box on the screen?
Prior to rendering, how do I set the initial state of the newly-rendered terminal box back to default with the exception of the "output" which would be re-valued to an appropriate response that I set?
How do I access the state in the prior terminal box so I can "read" the userInput stored there so I can determine what the appropriate response to that input would be?
I've included copies of each of the components below:
App.js
import React from "react";
import Terminal from "./components/Terminal";
import "./App.css";
class App extends React.Component {
render() {
return (
<div>
<Terminal />
</div>
);
}
}
export default App;
Terminal.js
import React, { Component } from "react";
import Form from "./Form";
import Falcon from "./Falcon";
import Messages from "./Alerts/Messages";
class Terminal extends Component {
state = {
output: Messages.intro,
userInput: "",
isComplete: false,
isDisabled: "",
};
markComplete = () => {
this.setState({
isComplete: true,
});
};
onSubmit = (event, userInput) => {
event.preventDefault();
this.setState({
userInput: userInput,
isDisabled: "disabled",
});
};
render() {
return (
<div>
<Falcon
output={this.state.output}
markComplete={this.markComplete}
isComplete={this.state.isComplete}
/>
<p />
<Form
input={this.state.userInput}
onSubmit={this.onSubmit}
isComplete={this.state.isComplete}
isDisabled={this.state.isDisabled}
/>
<p />
</div>
);
}
}
export default Terminal;
Falcon.js (Note: You'll see that there is a component "Typed" below - that is part of Matt Boldt's Typed.js (of which react-typed is an offshoot package) package which I'm using to simulate typing.)
import React, { Component } from 'react'
import Typed from 'react-typed'
class Falcon extends Component {
state = {
output: this.props.output,
};
render() {
return (
<div>
<Typed
strings={[this.state.output]}
typeSpeed={40}
onComplete={(self) => {
self.cursor.remove();
this.props.markComplete();
}}
/>
</div>
);
}
}
export default Falcon;
Form.js
import React from "react";
class Form extends React.Component {
state = {
input: this.props.input,
};
render() {
return (
<form
style={{
display: this.props.isComplete === false ? "none" : "",
}}
onSubmit={(event) => {
this.props.onSubmit(event, this.state.input);
}}
>
{"> "}
<input
ref={(input) => input && input.focus()}
type="text"
disabled={this.props.isDisabled}
style={{
border: "none",
outline: "none",
backgroundColor: "#FFF",
color: "#000",
}}
value={this.state.input}
onChange={(event) => this.setState({ input: event.target.value })}
/>
</form>
);
}
}
export default Form;
Any insight or guidance you can offer would be much appreciated! Thank you for helping this "first-timer" out!
Welcome to StackOverflow! I made a codesandbox demo with a few changes.
When developing React applications, it's a good practice to model the UI (the DOM elements) as a function of your internal state. You update the state and the UI changes automatically, it reacts to updates.
That said, you probably want to consider using the form only for the actual input element at the bottom of the terminal. The "past buffer" is just an array that only increases its content with user input and program output. Another good practice (actually a commandment!) is to never mutate the state. So, if you want to update the array, you create a new one from scratch, as in:
this.setState((state) => ({
conversation: [
...state.conversation, // we spread the previous state into the new one
{ text: state.userInput, id: faker.random.uuid(), type: "input" } // the last element is appended
]
}));}
Notice how setState (in class components) just schedules an update for the fields that you used. As your app scales, you will probably want to limit the length of this array.
The terminal component could be like:
class Terminal extends Component {
state = {
output: "Messages.intro",
userInput: "",
isComplete: false,
isDisabled: "",
conversation: [] // couldn't think of a nice name :(
};
markComplete = () => {
this.setState({
isComplete: true
});
};
onChange = (event) => {
this.setState({ userInput: event.target.value });
};
onSubmit = (event) => {
event.preventDefault();
this.setState((state) => ({
userInput: "",
conversation: [
...state.conversation,
{ text: state.userInput, id: faker.random.uuid(), type: "input" }
]
}));
};
render() {
const { conversation, userInput, output, isComplete } = this.state;
return (
<div>
<Falcon
output={output}
markComplete={this.markComplete}
isComplete={isComplete}
/>
<p />
// This is not really a form. Should be changed to another readonly element
{conversation.map((item, index) => (
<Form
key={item.id}
input={item.text}
onSubmit={this.onSubmit}
isComplete={isComplete}
isDisabled
/>
))}
<p />
<Form
input={userInput}
onSubmit={this.onSubmit}
isComplete={isComplete}
isDisabled={false}
onChange={this.onChange}
/>
<p />
</div>
);
}
}
As i am new to react i have a question.I have a react component and its properties. And I want to reach one of these properties from the page where i used my component.
type BranchProps = {
SelectedBranch : string
}
class Branch extends React.Component<BranchProps, BranchState> {
constructor(props: BranchProps) {
super(props);
}
render() {
return (
<SelectBox></SelectBox>
)
}
}
export default Branch ;
ParentPage.tsx
import Branch...
class Page extends.... {
ctor..
const test:string = () => {
Branch.SelectedBranch ???
}
}
And i want to get "SelectedBranch" from my ParentPage.
Note: SelectedBranch is changing on change event. Should i make my SelectedBranch a const and export it or what should i do ?
I have created this Input.js child component with different props
const Input = ({ placeholder, label, value, onChangeText, secureTextEntry }) => {
return (
<View >
<Text >{ label }</Text>
<TextInput
secureTextEntry={secureTextEntry}
placeholder={placeholder}
autoCorrect={false}
value={value}
onChangeText={onChangeText}
style={inputStyles}
/>
</View>
);
};
Once I import it to be used on a page, this is how the manipulation of the content is being done. the value is been passed on by simply quoting the specific prop
<Input
secureTextEntry
placeholder={'password'}
label={'Password'}
value={this.state.password}
onChangeText={password => this.setState({ password })}
/>
Here the 'password' is been assigned to the component by using the state of the parent. something like this, you can assign the value as you see fit.
state = { email: '', password: '', error: '', loading: false };
A far better way exist by using the Redux approach. would be advisable to have a look.
Firstly, you should understand the difference between state and props inside a component. Props shouldn't be updated, it's the state's role.
You can't directly access component's props outside of it.
In pure react (without librabry like redux) the right way should be to use callbacks to return the element to the parent.
class Branch extends React.Component<BranchProps, BranchState> {
state = {
'selectedBranch': ''
}
constructor(props: BranchProps) {
super(props);
}
handleOnChange = (e) => {
this.setState({'selectedBranch': e.target.value})
this.props.parentHandleChangeBranch(this.state.selectedBranch);
}
render() {
return (
<SelectBox value={this.state.selectedBranch} onChange="{this.handleOnChange}"></SelectBox>
)
}
}
class Page extends React.Component {
state = {
'branch': null
}
parentHandleChangeBranch = (branch) => {
this.setState({'branch': branch};
}
render () {
<div>
<Branch parentHandleChangeBranch={this.parentHandleChangeBranch} />
</div>
}
}
You can declare a function in the parent component and pass it as prop to the child. Then, call this callback whenever you want inside the child.
I am learning react native right now and new to how it works. I am currently tweeking with a project trying to change it up a little. https://github.com/denodenodeno/employee
The project has a tab bar at the bottom, which I want to change to become a segmented control in the top. This is what I have done and received as a result.
Original Code with Tab Bar
import React, {Component} from 'react';
import {
StyleSheet,
TabBarIOS
} from 'react-native';
import {bind} from '../utils/utils';
import EmployeesTab from './EmployeesTab';
import SearchTab from './SearchTab';
const employeesIcon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEsAAABLCAQAAACSR7JhAAADtUlEQVR4Ac3YA2Bj6QLH0XPT1Fzbtm29tW3btm3bfLZtv7e2ObZnms7d8Uw098tuetPzrxv8wiISrtVudrG2JXQZ4VOv+qUfmqCGGl1mqLhoA52oZlb0mrjsnhKpgeUNEs91Z0pd1kvihA3ULGVHiQO2narKSHKkEMulm9VgUyE60s1aWoMQUbpZOWE+kaqs4eLEjdIlZTcFZB0ndc1+lhB1lZrIuk5P2aib1NBpZaL+JaOGIt0ls47SKzLC7CqrlGF6RZ09HGoNy1lYl2aRSWL5GuzqWU1KafRdoRp0iOQEiDzgZPnG6DbldcomadViflnl/cL93tOoVbsOLVM2jylvdWjXolWX1hmfZbGR/wjypDjFLSZIRov09BgYmtUqPQPlQrPapecLgTIy0jMgPKtTeob2zWtrGH3xvjUkPCtNg/tm1rjwrMa+mdUkPd3hWbH0jArPGiU9ufCsNNWFZ40wpwn+62/66R2RUtoso1OB34tnLOcy7YB1fUdc9e0q3yru8PGM773vXsuZ5YIZX+5xmHwHGVvlrGPN6ZSiP1smOsMMde40wKv2VmwPPVXNut4sVpUreZiLBHi0qln/VQeI/LTMYXpsJtFiclUN+5HVZazim+Ky+7sAvxWnvjXrJFneVtLWLyPJu9K3cXLWeOlbMTlrIelbMDlrLenrjEQOtIF+fuI9xRp9ZBFp6+b6WT8RrxEpdK64BuvHgDk+vUy+b5hYk6zfyfs051gRoNO1usU12WWRWL73/MMEy9pMi9qIrR4ZpV16Rrvduxazmy1FSvuFXRkqTnE7m2kdb5U8xGjLw/spRr1uTov4uOgQE+0N/DvFrG/Jt7i/FzwxbA9kDanhf2w+t4V97G8lrT7wc08aA2QNUkuTfW/KimT01wdlfK4yEw030VfT0RtZbzjeMprNq8m8tnSTASrTLti64oBNdpmMQm0eEwvfPwRbUBywG5TzjPCsdwk3IeAXjQblLCoXnDVeoAz6SfJNk5TTzytCNZk/POtTSV40NwOFWzw86wNJRpubpXsn60NJFlHeqlYRbslqZm2jnEZ3qcSKgm0kTli3zZVS7y/iivZTweYXJ26Y+RTbV1zh3hYkgyFGSTKPfRVbRqWWVReaxYeSLarYv1Qqsmh1s95S7G+eEWK0f3jYKTbV6bOwepjfhtafsvUsqrQvrGC8YhmnO9cSCk3yuY984F1vesdHYhWJ5FvASlacshUsajFt2mUM9pqzvKGcyNJW0arTKN1GGGzQlH0tXwLDgQTurS8eIQAAAABJRU5ErkJggg==';
class App extends Component {
constructor(props, context) {
super(props, context);
this.state = {
selectedTab: 'employees'
};
bind(this)('_searchOnPress', '_employeesOnPress');
}
_employeesOnPress() {
this.setState({
selectedTab: 'employees'
})
}
_searchOnPress() {
this.setState({
selectedTab: 'search'
})
}
render() {
return (
<TabBarIOS
selectedTab={this.state.selectedTab}>
<TabBarIOS.Item
title="Employees"
selected={this.state.selectedTab === 'employees'}
icon={{uri: employeesIcon, scale: 3}}
onPress={this._employeesOnPress}>
<EmployeesTab />
</TabBarIOS.Item>
<TabBarIOS.Item
title="Search"
selected={this.state.selectedTab === 'search'}
systemIcon="search"
onPress={this._searchOnPress}>
<SearchTab />
</TabBarIOS.Item>
</TabBarIOS>
)
}
}
Tab Bar
Attempt at Segmented Control
I changed the segment inside the render and return to this
<SegmentedControlIOS
values={['Employees', 'Search']}
selectedIndex={0}
tintColor={'#D6573D'}
onValueChange={(val) => {
if (val === "Employees"){
this._employeesOnPress;
}
else if (val === "Search") {
this._searchOnPress;
}}}
onChange={(val) => {
if (val === "Employees"){
<EmployeesTab />
}
else if (val === "Search") {
<SearchTab />
}}}
/>
which gave me this segmented control with nothing else rendered
I'm not understanding the issue. Any help would be appreciated! Thank you
You can't render JSX inside of a prop, instead, you could have the components rendered based on the value in the state. Such as:
<SegmentedControlIOS
values={['Employees', 'Search']}
selectedIndex={0}
tintColor={'#D6573D'}
onValueChange={(val) => this.setState({ selectedTab: val})}
/>
{this.state.selectedTab === 'Employees' ? <EmployeesTab /> : <SearchTab /> }
Is there any way, one can delay first validation of components in formsy-material-ui so that validations like isNotEmpty do not fire on first render of the form and mess the UX? I am using controlled components, therefore setting value from state on each render.
<FormsyText
name="name"
value={this.state.name}
floatingLabelText="Name"
onChange={partial(this._changeInputValue, ['name'])}
validations={{ isNotEmpty }}
validationError="Field shoud not be empty"
/>
I needed this solution too. I've been looking into the source code of formsy-material-ui, and it seems that the text field is setting its value right before it's mounted. That's why the field is marked changed (aka not pristine) when the rendering happens, so the validation error is shown.
Anyways, I wrote a hackish solution using a higher order component. I've been testing with text fields only, but should work with any fields having this problem. The core concept: if the formsy field doesn't have a "validationErrors" prop, it's not showing any errors.
import React, { Component, PropTypes } from 'react';
export const preventFirstValidation = (FormsyField) => {
return class extends Component {
static propTypes = { preventFirstValidation: PropTypes.bool };
static defaultProps = { preventFirstValidation: true };
constructor(props) {
super(props);
this.state = { isChanged: false };
}
render() {
const { preventFirstValidation, ...fieldProps } = this.props;
return (
<FormsyField
{...fieldProps}
onChange={(evt, val) => {
if (!this.state.isChanged) this.setState({ isChanged: true });
if (this.props.onChange) this.props.onChange(evt, val);
}}
validationErrors={(this.state.isChanged || !preventFirstValidation) ? this.props.validationErrors : undefined}
/>
);
}
};
};
How to use it:
import { Form } from 'formsy-react';
import FormsyTextField from 'formsy-material-ui/lib/FormsyText';
const TextField = preventFirstValidation(FormsyTextField);
const MyForm = () => (
<Form>
{/* You're using the transformed field, exactly like before */}
<TextField
name = "some_field"
validationErrors={{ isRequired: 'This is really required!' }}
required
preventFirstValidation={ /* you can enable or disable this feature */ }
/>
</Form>
);