Comments (4)
Both of the above solutions worked perfectly! I decided to follow the second as it fits in more with the way the other labels have been styled, as you said. Thank you so much for the prompt response and the effort <3
from chakra-react-select.
I can't tell exactly what effect you're going for, do you have a picture of an example? Or possibly a code sample of the same effect working in another context?
from chakra-react-select.
The "place" field is the CreatableSelect field. As you can see above, the floating label remains on top when the field is focused. But when the focus moves away, the label covers the selected input as you can see in the image below.
I would like to be able to implement an effect similar to what is seen in the other fields like "pincode", "street", etc, where the label remains on top of a filled field even when it is not focused.
import { extendTheme } from "@chakra-ui/react"
const activeLabelStyles = {
transform: 'scale(0.85) translateY(-24px)',
}
export const theme = extendTheme({
components: {
Form: {
variants: {
floating: {
container: {
_focusWithin: {
label: {
...activeLabelStyles,
},
},
'input:not(:placeholder-shown) + label, .chakra-select__wrapper + label, .react-date-picker + label':
{
...activeLabelStyles,
},
label: {
top: 0,
left: 0,
zIndex: 2,
position: 'absolute',
backgroundColor: 'white',
pointerEvents: 'none',
mx: 3,
px: 1,
my: 2,
transformOrigin: 'left top'
},
},
},
},
},
},
})
The entire form is wrapped in a Chakra provider that uses the above custom theme
The above code works for all other input fields - eg : the "street" field is shown below
<Box>
<FormControl id="street" variant='floating' isRequired>
<Input placeholder=' ' type="text" style={inputStyle} {...register("street")} />
<FormLabel>Street</FormLabel>
</FormControl>
</Box>
Thanks for the time! :)
from chakra-react-select.
This was an interesting challenge for me so I gave it a shot! I ended up coming up with two ways to do this, one of which involved a small patch I added into the most recent version of chakra-react-select
, 3.2.0
.
First Way
The first way which was simple enough to add in the way chakra-react-select
previously worked was just to check if a value was selected and add the custom style to the label manually. Here's an example of how to do that:
CodeSandbox: https://codesandbox.io/s/chakra-react-select-floating-labels-6eyic1?file=/example.js
import { useState } from "react";
import { FormControl, FormLabel } from "@chakra-ui/react";
import { CreatableSelect } from "chakra-react-select";
const Example = () => {
const [place, setPlace] = useState();
const labelProps = {
...(!!place && { transform: "scale(0.85) translateY(-24px)" })
};
return (
<FormControl id="place" isRequired variant="floating">
<FormLabel {...labelProps}>Place</FormLabel>
<CreatableSelect
name="place"
className="chakra-react-select__wrapper"
classNamePrefix="chakra-react-select"
options={locality}
closeMenuOnSelect={true}
value={place}
tabSelectsValues
onChange={setPlace}
openMenuOnFocus
placeholder=" "
/>
</FormControl>
);
};
Second Way
After messing around with this I realized there was a simple change I could add to my most recent version that was already in progress that would allow you to add this style in a way that already falls into line with the way you have your theme set up. I added the --has-value
className to the Select container when a value is selected so you can do it like you have the rest. In order to use this change, update to version 3.2.0
.
The first step to using this is to add the classNamePrefix
prop to your select, without that the --has-value
class modifier will not be added to your component. So for example, if I put the prop classNamePrefix="chakra-react-select"
on the CreatableSelect
component, then the className chakra-react-select--has-value
will be added to the outermost container when an option is selected. So first I changed your custom selector in your theme:
input:not(:placeholder-shown) + label, .chakra-react-select--has-value + label, .chakra-select__wrapper + label, .react-date-picker + label
And then I modified the select like so:
CodeSandbox: https://codesandbox.io/s/chakra-react-select-floating-labels-alt-vjlmzo?file=/example.js
import { useState } from "react";
import { FormControl, FormLabel } from "@chakra-ui/react";
import { CreatableSelect } from "chakra-react-select";
const Example = () => {
const [place, setPlace] = useState();
return (
<FormControl id="place" isRequired variant="floating" mb={8}>
<CreatableSelect
name="place"
classNamePrefix="chakra-react-select"
// ^ THIS IS REQUIRED, but it can be whatever you want.
// Just make sure its consistent with your custom selector and across
// all select components that you want to have a floating label
options={locality}
value={place}
tabSelectsValues
onChange={setPlace}
openMenuOnFocus
placeholder=" "
chakraStyles={{
menu: (provided) => ({
...provided,
zIndex: 3
})
}}
/>
<FormLabel>Place</FormLabel>
</FormControl>
);
};
I also added the chakraStyles
modifier to change the zIndex
of the Menu, otherwise the menu will go under any form labels that follow it.
Let me know if either of these solutions work for you or you have any questions! If not, don't forget to close the issue!
from chakra-react-select.
Related Issues (20)
- [BUG] Warning: Prop `id` did not match. Server: HOT 1
- [BUG] name and required does not work with standard form validation HOT 2
- [BUG]: `colorScheme` doesn't get passed to input variants HOT 4
- [BUG] `selectedOptionStyle` and `variant` are missing from `Select` types. HOT 16
- selectedOptionStyle with isMulti HOT 1
- [BUG] Breaking Change in chakraui/icon HOT 6
- Property "size" does not exist on type HOT 1
- [BUG] "default" is not exported by "node_modules/@chakra-ui/icon/dist/index.mjs", imported by "node_modules/chakra-react-select/dist/esm/chakra-components/menu.js". HOT 1
- [BUG] useBasicStyles type error on Vite HOT 1
- [BUG] Group Heading props does not work HOT 3
- [Feature request] Allow adding some related keywords to dropdown items so that they can be searched based on those keywords HOT 1
- [BUG] Problem with accessibility of the CreatableSelect (nested interactive elements) HOT 1
- [BUG] peerDependencies to Chakra v2 still show up HOT 5
- [BUG] Cannot start the application when using chakra-react-select with Vite HOT 2
- [BUG] Custom components automatically switch to Chakra style instead of keeping default HOT 1
- [BUG]: No `ValueType` export for the `onChange` handler HOT 11
- property data-testid is not render
- [BUG] Version 4.7.6 breaks colors for selected item HOT 2
- [BUG] LoadOptions doesn't render options when isMulti = True and there is already one selected option in component HOT 1
- [BUG] Incompatible with Chakra UI Pro theme HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chakra-react-select.