Skip to content
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

Feature/create select internal label #486

Merged
merged 12 commits into from
Apr 16, 2024
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ core
package-lock.json
.idea/
*.log
.env
.env

yarn-error.log*
2 changes: 2 additions & 0 deletions src/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createPageExport } from '../../utils/storybook'

const aiqProps = [
'label',
'internalLabel',
'items',
'isOpen',
'variant',
Expand All @@ -35,6 +36,7 @@ const aiqProps = [
export default createPageExport(Select, 'Select', aiqProps, {
argTypes: {
label: { control: 'text' },
internalLabel: { control: 'text' },
items: { control: 'object' },
isOpen: { control: 'boolean' },
variant: {
Expand Down
1 change: 1 addition & 0 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SelectStatic } from './SelectStatic'

export type Props = BoxProps & {
label?: string
internalLabel?: string
items?: Array<string | { id: any; name: any; select?: any }>
isOpen?: boolean
variant?: 'outlined'
Expand Down
25 changes: 23 additions & 2 deletions src/components/Select/SelectFetchable.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { useCombobox } from 'downshift'
import React, { useEffect, useState } from 'react'
import React, { useEffect, useMemo, useState } from 'react'
import { IoIosArrowDown } from 'react-icons/io'
import styled, { css } from 'styled-components'

import { Box, Props as BoxPros } from '../Box'
import { Button, Props as ButtonProps } from '../Button'
import { Input } from '../Input'
import { Loading } from '../Loading'
import { Text } from '../Text'

export type Props = BoxPros & {
label?: string
internalLabel?: string
items?: Array<string | { id: any; name: any; select?: any }>
isOpen?: boolean
variant?: 'outlined'
Expand Down Expand Up @@ -103,6 +105,7 @@ export const SelectFetchable = React.forwardRef<HTMLDivElement, Props>(
{
disabled,
label,
internalLabel,
variant,
items = [],
placeholder,
Expand Down Expand Up @@ -185,6 +188,17 @@ export const SelectFetchable = React.forwardRef<HTMLDivElement, Props>(
}
}

const completePrefix = useMemo(() => {
if (!prefix && !internalLabel) return null

return (
<div style={{ marginRight: internalLabel && '-12px', columnGap: '4px' }}>
{prefix}
<Text ml={prefix && '4px'} color='darkGrey' fontSize='small' >{internalLabel}</Text>
</div>
)
}, [])

return (
<Container
isOpen={isOpen && !disabled}
Expand All @@ -194,6 +208,13 @@ export const SelectFetchable = React.forwardRef<HTMLDivElement, Props>(
{...props}
>
<ul {...getMenuProps()}>
{internalLabel &&
<div style={{ marginTop: '8px', marginBottom: '2px' }}>
<Text p='12px' color='darkGrey' fontSize={12} fontWeight='medium'>
{internalLabel}
</Text>
</div>
}
{isOpen &&
inputItems &&
!isLoading &&
Expand Down Expand Up @@ -239,7 +260,7 @@ export const SelectFetchable = React.forwardRef<HTMLDivElement, Props>(
errorMessage={errorMessage}
errorForm={errorForm}
readOnly={!autoComplete}
prefix={prefix}
prefix={completePrefix}
placeholder={placeholder}
nativeAutoComplete='disabled'
{...boxStyled}
Expand Down
26 changes: 24 additions & 2 deletions src/components/Select/SelectStatic.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useMemo, useState } from 'react'
import styled, { css } from 'styled-components'

import { useCombobox } from 'downshift'
Expand All @@ -8,9 +8,11 @@ import { Box, Props as BoxPros } from '../Box'
import { Button, Props as ButtonProps } from '../Button'
import { Input } from '../Input'
import { Loading } from '../Loading'
import { Text } from '../Text'

export type Props = BoxPros & {
label?: string
internalLabel?: string
items?: Array<string | { id: any; name: any; select?: any }>
isOpen?: boolean
variant?: 'outlined'
Expand Down Expand Up @@ -105,6 +107,7 @@ export const SelectStatic = React.forwardRef<HTMLDivElement, Props>(
{
disabled,
label,
internalLabel,
variant,
items = [],
placeholder,
Expand Down Expand Up @@ -138,6 +141,7 @@ export const SelectStatic = React.forwardRef<HTMLDivElement, Props>(
useEffect(() => setInputItems(items), [items])

const { backgroundColor, border, width, maxWidth } = props

const boxStyled = {
backgroundColor,
border,
Expand Down Expand Up @@ -181,6 +185,17 @@ export const SelectStatic = React.forwardRef<HTMLDivElement, Props>(
}
}

const completePrefix = useMemo(() => {
if (!prefix && !internalLabel) return null

return (
<div style={{ marginRight: internalLabel && '-12px', columnGap: '4px' }}>
{prefix}
<Text ml={prefix && '4px'} color='darkGrey' fontSize='small' >{internalLabel}</Text>
</div>
)
}, [])

return (
<Container
isOpen={isOpen && !disabled}
Expand All @@ -190,6 +205,13 @@ export const SelectStatic = React.forwardRef<HTMLDivElement, Props>(
{...props}
>
<ul {...getMenuProps()}>
{internalLabel &&
<div style={{ marginTop: '8px', marginBottom: '2px' }}>
<Text p='12px' color='darkGrey' fontSize={12} fontWeight='medium'>
{internalLabel}
</Text>
</div>
}
{isOpen &&
inputItems &&
!isDependent &&
Expand Down Expand Up @@ -229,7 +251,7 @@ export const SelectStatic = React.forwardRef<HTMLDivElement, Props>(
errorMessage={errorMessage}
errorForm={errorForm}
readOnly={!autoComplete}
prefix={prefix}
prefix={completePrefix}
placeholder={placeholder}
{...getInputProps({
onClick: () => {
Expand Down
Loading
Loading