Legacy filters

这是一个遗留版本的过滤器组件,which is a composite component that filters the items of a list or table.

Deprecated

This component is no longer supported. The newFilters componentcan be used as a standalone component, but is used primarily within theIndexFiltersfor sorting and filtering IndexTables.

import{ChoiceList,TextField,RangeSlider,LegacyCard,ResourceList,LegacyFilters,Avatar,Text,}from'@shopify/polaris';import{useState,useCallback}from'react';functionResourceListFiltersExample(){const[accountStatus,setAccountStatus]=useState<string[]|undefined>(undefined,);const[moneySpent,setMoneySpent]=useState<[number,number]|undefined>(undefined,);const[taggedWith,setTaggedWith]=useState<string|undefined>(undefined);const[queryValue,setQueryValue]=useState<string|undefined>(undefined);consthandleAccountStatusChange=useCallback((瓦尔ue:string[])=>setAccountStatus(瓦尔ue),[],);consthandleMoneySpentChange=useCallback((瓦尔ue:[number,number])=>setMoneySpent(瓦尔ue),[],);consthandleTaggedWithChange=useCallback((瓦尔ue:string)=>setTaggedWith(瓦尔ue),[],);consthandleFiltersQueryChange=useCallback((瓦尔ue:string)=>setQueryValue(瓦尔ue),[],);consthandleAccountStatusRemove=useCallback(()=>setAccountStatus(undefined),[],);consthandleMoneySpentRemove=useCallback(()=>setMoneySpent(undefined),[],);consthandleTaggedWithRemove=useCallback(()=>setTaggedWith(undefined),[],);consthandleQueryValueRemove=useCallback(()=>setQueryValue(undefined),[],);consthandleFiltersClearAll=useCallback(()=>{handleAccountStatusRemove();handleMoneySpentRemove();handleTaggedWithRemove();handleQueryValueRemove();},[handleAccountStatusRemove,handleMoneySpentRemove,handleQueryValueRemove,handleTaggedWithRemove,]);constfilters=[{key:'accountStatus',label:'Account status',filter:(<ChoiceListtitle="Account status"titleHiddenchoices={[{label:'Enabled',瓦尔ue:'enabled'},{label:'Not invited',瓦尔ue:'not invited'},{label:'Invited',瓦尔ue:'invited'},{label:'Declined',瓦尔ue:'declined'},]}selected={accountStatus||[]}onChange={handleAccountStatusChange}allowMultiple/>),shortcut:true,},{key:'taggedWith',label:'Tagged with',filter:(<TextFieldlabel="Tagged with"瓦尔ue={taggedWith}onChange={handleTaggedWithChange}autoComplete="off"labelHidden/>),shortcut:true,},{key:'moneySpent',label:'Money spent',filter:(<RangeSliderlabel="Money spent is between"labelHidden瓦尔ue={moneySpent||[0,500]}prefix="$"outputmin={0}max={2000}step={1}onChange={handleMoneySpentChange}/>),},];constappliedFilters=[];if(accountStatus&&accountStatus.length>0){appliedFilters.push({key:'accountStatus',label:accountStatus.map((瓦尔)=>`Customer${瓦尔}`).join(', '),onRemove:handleAccountStatusRemove,});}if(moneySpent){appliedFilters.push({key:'moneySpent',label:`Money spent is between $${moneySpent[0]}and $${moneySpent[1]}`,onRemove:handleMoneySpentRemove,});}if(taggedWith&&!isEmpty(taggedWith)){appliedFilters.push({key:'taggedWith',label:`Tagged with${taggedWith}`,onRemove:handleTaggedWithRemove,});}return(<div style={{height:'568px'}}><LegacyCard><ResourceListresourceName={{singular:'customer',plural:'customers'}}filterControl={<LegacyFiltersqueryValue={queryValue}filters={filters}appliedFilters={appliedFilters}onQueryChange={handleFiltersQueryChange}onQueryClear={handleQueryValueRemove}onClearAll={handleFiltersClearAll}/>}items={[{id:'341',url:'#',name:'Mae Jemison',location:'Decatur, USA',},{id:'256',url:'#',name:'Ellen Ochoa',location:'Los Angeles, USA',},]}renderItem={(item)=>{const{id,url,name,location}=item;constmedia=<Avatarcustomer size="medium"name={name}/>;return(<ResourceList.Itemid={id}url={url}media={media}accessibilityLabel={`View details for${name}`}><Textas="h3"variant="bodyMd"fontWeight="bold">{name}</Text><div>{location}</div></ResourceList.Item>);}}/></LegacyCard></div>);functionisEmpty(瓦尔ue:string):boolean{if(Array.isArray(瓦尔ue)){return瓦尔ue.length===0;}else{return瓦尔ue===''||瓦尔ue==null;}}}

Merchants use filters to:

  • view different subsets of items in a list or table
  • filter by typing into a text field
  • filter by selecting filters or promoted filters

The way that merchants interact with filters depends on the components that you decide to incorporate. In its simplest form, filters includes a text field and a set of filters, which can be displayed in different ways. For example, you could show promoted filters and a More button that opens asheetcontaining more filters. What the filters are and how they’re exposed to merchants is flexible.


Accessibility

The filters component relies on the accessibility features of multiple other components:

Maintain accessibility with custom features

Since custom HTML can be passed to the component for additional actions, ensure that the filtering system you build is accessible as a whole.

All merchants must:

  • be able to identify and understand labels for all controls
  • be notified of state changes
  • be able to complete all actions with the keyboard

Best practices

The filters component should:

  • help reduce merchant effort by promoting the filtering categories that are most commonly used
  • include no more than 2 or 3 promoted filters
  • consider small screen sizes when designing the interface for each filter and the total number filters to include
  • use children only for content that’s related or relevant to filtering

Content guidelines

Text field

The text field should be clearly labeled so it’s obvious to merchants what they should enter into the field.

Do

  • Filter orders

Don’t

  • Enter text here

Filter badges

Use the name of the filter if the purpose of the name is clear on its own. For example, when you see a filter badge that readsFulfilled, it’s intuitive that it falls under the Fulfillment status category.

Do

  • Fulfilled, Unfulfilled

Don’t

  • 完成:完成,没有得到满足

If the filter name is ambiguous on its own, add a descriptive word related to the status. For example,Lowdoesn’t make sense out of context. Add the word “risk” so that merchants know it’s from the Risk category.

Do

  • High risk, Low risk

Don’t

  • High, Low

Group tags from the same category together.

Do

  • (Unfulfilled, Fulfilled)

Don’t

  • (Unfulfilled) (fulfilled)

If all tag pills selected: truncate in the middle

Do

  • Paid, par… unpaid

Don’t

  • All payment status filters selected, Paid, unpa…